| @@ -47,6 +47,14 @@ | |||||
| "config": { | "config": { | ||||
| "policies": [] | "policies": [] | ||||
| } | } | ||||
| }, | |||||
| { | |||||
| "method": "POST", | |||||
| "path": "/message/history", | |||||
| "handler": "message.history", | |||||
| "config": { | |||||
| "policies": [] | |||||
| } | |||||
| } | } | ||||
| ] | ] | ||||
| } | } | ||||
| @@ -1,8 +1,39 @@ | |||||
| 'use strict'; | |||||
| "use strict"; | |||||
| const messageModel = require("../../../model/messageModel"); | |||||
| const { parseMultipartData, sanitizeEntity } = require("strapi-utils"); | |||||
| /** | /** | ||||
| * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/controllers.html#core-controllers) | * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/controllers.html#core-controllers) | ||||
| * to customize this controller | * to customize this controller | ||||
| */ | */ | ||||
| module.exports = {}; | |||||
| module.exports = { | |||||
| history: async (ctx) => { | |||||
| // console.log("history"); | |||||
| // ctx.respond = false; | |||||
| try { | |||||
| const { request } = ctx; | |||||
| let phone = request.body.phoneNumber; | |||||
| let sessionID = request.body.id; | |||||
| const sessionData = await strapi.api["session-data"].services[ | |||||
| "session-data" | |||||
| ].findOne({ | |||||
| id: sessionID, | |||||
| }); | |||||
| var result; | |||||
| var data = await messageModel | |||||
| .asyncLoadClient(sessionData) | |||||
| .catch((message) => { | |||||
| result = { STATUS: 0, DATA: message }; | |||||
| }); | |||||
| if (data["TYPE"] == "READY") { | |||||
| result = await messageModel.getChatbyPhoneNumber(phone); | |||||
| } | |||||
| ctx.body = result; | |||||
| } catch (e) { | |||||
| ctx.throw(400, e.message); | |||||
| } | |||||
| }, | |||||
| }; | |||||
| @@ -1,6 +1,5 @@ | |||||
| const messageModel = require("../../../model/messageModel"); | const messageModel = require("../../../model/messageModel"); | ||||
| const { parseMultipartData, sanitizeEntity } = require("strapi-utils"); | const { parseMultipartData, sanitizeEntity } = require("strapi-utils"); | ||||
| const { Client } = require("whatsapp-web.js"); | |||||
| /** | /** | ||||
| * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/controllers.html#core-controllers) | * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/controllers.html#core-controllers) | ||||
| * to customize this controller | * to customize this controller | ||||
| @@ -14,18 +14,6 @@ let clients = {}; | |||||
| let taskClientLoading = false; | let taskClientLoading = false; | ||||
| let taskSendingMessage = false; | let taskSendingMessage = false; | ||||
| function loadClient(i) { | |||||
| return new Promise((resolve, reject) => { | |||||
| try { | |||||
| messageModel.loadClient(i["session_data"], async (data) => { | |||||
| return resolve(data); | |||||
| }); | |||||
| } catch (e) { | |||||
| return reject(e.message); | |||||
| } | |||||
| }); | |||||
| } | |||||
| module.exports = { | module.exports = { | ||||
| "*/10 * * * * *": async () => { | "*/10 * * * * *": async () => { | ||||
| if (!taskClientLoading) { | if (!taskClientLoading) { | ||||
| @@ -41,9 +29,11 @@ module.exports = { | |||||
| if (!Object.keys(clients).includes(`${i["id"]}`)) { | if (!Object.keys(clients).includes(`${i["id"]}`)) { | ||||
| taskClientLoading = true; | taskClientLoading = true; | ||||
| clients[i["id"]] = null; | clients[i["id"]] = null; | ||||
| var data = await loadClient(i).catch((message) => { | |||||
| console.log(`error loading clients ${message}`); | |||||
| }); | |||||
| var data = await messageModel | |||||
| .asyncLoadClient(i) | |||||
| .catch((message) => { | |||||
| console.log(`error loading clients ${message}`); | |||||
| }); | |||||
| if (data["TYPE"] == "READY") { | if (data["TYPE"] == "READY") { | ||||
| clients[i["id"]] = data["DATA"]; | clients[i["id"]] = data["DATA"]; | ||||
| } else if (data["TYPE"] == "AUTH_FAILURE") { | } else if (data["TYPE"] == "AUTH_FAILURE") { | ||||
| @@ -109,7 +99,7 @@ module.exports = { | |||||
| message["send_to"], | message["send_to"], | ||||
| message["raw_text"] | message["raw_text"] | ||||
| ); | ); | ||||
| console.log(send); | |||||
| // console.log(send); | |||||
| if (send["STATUS"] == 1) { | if (send["STATUS"] == 1) { | ||||
| await strapi.api.message.services.message.update( | await strapi.api.message.services.message.update( | ||||
| { id: message.id }, | { id: message.id }, | ||||
| @@ -2,7 +2,7 @@ const qrcode = require("qrcode-terminal"); | |||||
| const fs = require("fs"); | const fs = require("fs"); | ||||
| // const path = require("path"); | // const path = require("path"); | ||||
| const { Client } = require("whatsapp-web.js"); | |||||
| const { Client, Chat, Message } = require("whatsapp-web.js"); | |||||
| // const SESSION_FOLDER = path.join(__dirname, "../data/"); | // const SESSION_FOLDER = path.join(__dirname, "../data/"); | ||||
| // const SESSION_FILE_PATH = path.join(__dirname, "../data/session.json"); | // const SESSION_FILE_PATH = path.join(__dirname, "../data/session.json"); | ||||
| // const sessions = require("../api/session_data"); | // const sessions = require("../api/session_data"); | ||||
| @@ -111,6 +111,18 @@ var loadClient = async function (clientSessionData, callback) { | |||||
| } | } | ||||
| }; | }; | ||||
| function asyncLoadClient(i) { | |||||
| return new Promise((resolve, reject) => { | |||||
| try { | |||||
| loadClient(i["session_data"], async (data) => { | |||||
| return resolve(data); | |||||
| }); | |||||
| } catch (e) { | |||||
| return reject(e.message); | |||||
| } | |||||
| }); | |||||
| } | |||||
| var createNewClient = async function (callback) { | var createNewClient = async function (callback) { | ||||
| try { | try { | ||||
| client = new Client({ | client = new Client({ | ||||
| @@ -137,10 +149,26 @@ var sendMessage = async function (client, phone_number, textMessage) { | |||||
| var phoneNumber = reformatMobilePhone(phone_number); | var phoneNumber = reformatMobilePhone(phone_number); | ||||
| // var state = await client.isRegisteredUser(phoneNumber); | // var state = await client.isRegisteredUser(phoneNumber); | ||||
| // console.log(state); | // console.log(state); | ||||
| await client.sendMessage(phoneNumber, textMessage); | await client.sendMessage(phoneNumber, textMessage); | ||||
| return { STATUS: 1, DATA: "Messages Succesfully send !" }; | return { STATUS: 1, DATA: "Messages Succesfully send !" }; | ||||
| } catch (e) { | } catch (e) { | ||||
| console.log(e); | |||||
| return { STATUS: 0, DATA: e.message }; | |||||
| } | |||||
| }; | |||||
| var getChatbyPhoneNumber = async function (phone_number) { | |||||
| try { | |||||
| if (!client.info) { | |||||
| throw { message: "Client Is Not Ready !" }; | |||||
| } | |||||
| let phoneNumber = reformatMobilePhone(phone_number); | |||||
| let chats = await client.getChatById(phoneNumber); | |||||
| let messageList = await chats.fetchMessages(); | |||||
| return { STATUS: 1, DATA: messageList }; | |||||
| } catch (e) { | |||||
| return { STATUS: 0, DATA: e.message }; | return { STATUS: 0, DATA: e.message }; | ||||
| } | } | ||||
| }; | }; | ||||
| @@ -215,4 +243,6 @@ module.exports = { | |||||
| destroySession: destroySession, | destroySession: destroySession, | ||||
| getClientState: getClientState, | getClientState: getClientState, | ||||
| getClientState: getClientState, | getClientState: getClientState, | ||||
| getChatbyPhoneNumber: getChatbyPhoneNumber, | |||||
| asyncLoadClient: asyncLoadClient, | |||||
| }; | }; | ||||