wa_strapi
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.2 KiB

  1. const messageModel = require("../../../model/messageModel");
  2. const { parseMultipartData, sanitizeEntity } = require("strapi-utils");
  3. const { Client } = require("whatsapp-web.js");
  4. /**
  5. * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/controllers.html#core-controllers)
  6. * to customize this controller
  7. */
  8. module.exports = {
  9. initiate: async (ctx) => {
  10. ctx.respond = false;
  11. const { req, res } = ctx;
  12. res.setHeader("Content-Type", "text/html; charset=UTF-8");
  13. res.setHeader("Transfer-Encoding", "chunked");
  14. res.writeHead(200);
  15. messageModel.createNewClient(async function (data) {
  16. // console.log(data);
  17. var json = JSON.parse(data);
  18. if (json["STATUS"] == 1 && json["TYPE"] == "READY") {
  19. // console.log(json["DATA"]);
  20. let checkExist = await strapi
  21. .query("session-data")
  22. .findOne({ phonenumber: json["DATA"].wid.user });
  23. if (checkExist) {
  24. // messageModel.loadClient(async function(data){
  25. // let state = JSON.parse(data);
  26. // if (state["STATUS"] == 1 && state["TYPE"] == "READY"){
  27. // let loaded = new Client();
  28. // loaded = state["DATA"];
  29. // await loaded.destroy();
  30. // }
  31. // });
  32. let id = checkExist.id;
  33. await strapi.services["session-data"].update({ id }, {
  34. name:json["DATA"].pushname,
  35. device_manufacturer:json["DATA"].phone.device_manufacturer,
  36. device_model:json["DATA"].phone.device_model,
  37. platform:json["DATA"].platform,
  38. os_version:json["DATA"].phone.os_version,
  39. phonenumber:json["DATA"].wid.user,
  40. session_data:json["DATA"].sessionData,
  41. });
  42. }
  43. else {
  44. await strapi.services["session-data"].create({
  45. name:json["DATA"].pushname,
  46. device_manufacturer:json["DATA"].phone.device_manufacturer,
  47. device_model:json["DATA"].phone.device_model,
  48. platform:json["DATA"].platform,
  49. os_version:json["DATA"].phone.os_version,
  50. phonenumber:json["DATA"].wid.user,
  51. session_data:json["DATA"].sessionData,
  52. });
  53. // console.log(entry);
  54. }
  55. }
  56. res.write(data);
  57. if (
  58. json["STATUS"] == 0 ||
  59. (json["STATUS"] == 1 && json["TYPE"] == "READY")
  60. ) {
  61. return res.end();
  62. }
  63. });
  64. },
  65. };