@@ -23,6 +23,10 @@ | |||||
}, | }, | ||||
"body_vars": { | "body_vars": { | ||||
"type": "string" | "type": "string" | ||||
}, | |||||
"message": { | |||||
"via": "template_key", | |||||
"model": "message" | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -53,8 +53,8 @@ | |||||
"timestamp": { | "timestamp": { | ||||
"type": "datetime" | "type": "datetime" | ||||
}, | }, | ||||
"session_datum_key": { | |||||
"model": "session-data", | |||||
"template_key": { | |||||
"model": "message-template", | |||||
"via": "message" | "via": "message" | ||||
} | } | ||||
} | } | ||||
@@ -1,4 +1,4 @@ | |||||
'use strict'; | |||||
"use strict"; | |||||
/** | /** | ||||
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/services.html#core-services) | * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/services.html#core-services) | ||||
@@ -6,12 +6,28 @@ | |||||
*/ | */ | ||||
module.exports = { | module.exports = { | ||||
distinctbyKey: async (params) =>{ | |||||
const results = await strapi.query('message').find(params); | |||||
distinctbyKey: async (params) => { | |||||
const results = await strapi.query("message").find(params); | |||||
let arrKeys = []; | let arrKeys = []; | ||||
results.forEach((i)=>{ | |||||
if(!arrKeys.includes(i["session_datum_key"])) arrKeys.push(i["session_datum_key"]); | |||||
results.forEach((i) => { | |||||
if (!arrKeys.includes(i["session_datum_key"])) | |||||
arrKeys.push(i["session_datum_key"]); | |||||
}); | }); | ||||
return arrKeys | |||||
} | |||||
return arrKeys; | |||||
}, | |||||
getQueueMessage: async (sessionId) => { | |||||
var chat = await strapi.api["chats"].services["chats"].find({ | |||||
session_key: sessionId, | |||||
}); | |||||
var arrayChat = chat.map((i) => i.id); | |||||
var messageList = []; | |||||
if (chat && chat.length != 0) { | |||||
messageList = await strapi.api.message.services.message.find({ | |||||
_sort: "chat_key:desc", | |||||
state: "QUEUE", | |||||
chat_key_in: arrayChat, | |||||
}); | |||||
} | |||||
return messageList; | |||||
}, | |||||
}; | }; |
@@ -32,10 +32,6 @@ | |||||
"phonenumber": { | "phonenumber": { | ||||
"type": "string", | "type": "string", | ||||
"required": true | "required": true | ||||
}, | |||||
"message": { | |||||
"via": "session_datum_key", | |||||
"model": "message" | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -1,6 +1,7 @@ | |||||
"use strict"; | "use strict"; | ||||
const messageModel = require("../../model/messageModel"); | const messageModel = require("../../model/messageModel"); | ||||
const utils = require("../../util/util"); | |||||
const refreshChat = require("../../util/refreshChat"); | |||||
const sendQueueMessage = require("../../util/sendQueueMessage"); | |||||
const { parseMultipartData, sanitizeEntity } = require("strapi-utils"); | const { parseMultipartData, sanitizeEntity } = require("strapi-utils"); | ||||
/** | /** | ||||
* Cron config that gives you an opportunity | * Cron config that gives you an opportunity | ||||
@@ -78,73 +79,8 @@ module.exports = { | |||||
if (!sessionsValid) { | if (!sessionsValid) { | ||||
delete clients[parseInt(i)]; | delete clients[parseInt(i)]; | ||||
} else { | } else { | ||||
const messages = await strapi.api.message.services.message.find( | |||||
{ | |||||
_sort: "session_datum_key:desc", | |||||
state: "QUEUE", | |||||
session_datum_key: parseInt(i), | |||||
} | |||||
); | |||||
// console.log(messages); | |||||
for (const message of messages) { | |||||
// console.log(clients[i] != null, i, clients); | |||||
try { | |||||
if (clients[i] != null) { | |||||
await strapi.api.message.services.message.update( | |||||
{ id: message.id }, | |||||
{ state: "SENDING" } | |||||
); | |||||
let send = await messageModel.sendMessage( | |||||
clients[i], | |||||
message["chat_key"]["chatId"] | |||||
._serialized, | |||||
message["body"] | |||||
); | |||||
if (send["STATUS"] == 1) { | |||||
// console.log(send["DATA"]); | |||||
await strapi.api.message.services.message.update( | |||||
{ id: message.id }, | |||||
{ | |||||
state: "SENT", | |||||
messageId: | |||||
send["DATA"]["id"]["id"], | |||||
from: send["DATA"]["from"], | |||||
isForwarded: | |||||
send["DATA"][ | |||||
"isForwarded" | |||||
] == undefined | |||||
? false | |||||
: send["DATA"][ | |||||
"isForwarded" | |||||
], | |||||
timestamp: moment | |||||
.unix( | |||||
send["DATA"][ | |||||
"timestamp" | |||||
] | |||||
) | |||||
.toDate(), | |||||
} | |||||
); | |||||
} else { | |||||
await strapi.api.message.services.message.update( | |||||
{ id: message.id }, | |||||
{ | |||||
state: "ERROR", | |||||
keterangan: send["DATA"], | |||||
} | |||||
); | |||||
} | |||||
} | |||||
} catch (e) { | |||||
// console.log("err", e); | |||||
await strapi.api.message.services.message.update( | |||||
{ id: message.id }, | |||||
{ state: "ERROR", keterangan: e.message } | |||||
); | |||||
} | |||||
} | |||||
await utils.refreshChat(clients[i], parseInt(i)); | |||||
await sendQueueMessage(clients[i], parseInt(i)); | |||||
await refreshChat(clients[i], parseInt(i)); | |||||
} | } | ||||
} | } | ||||
taskSendingMessage = false; | taskSendingMessage = false; | ||||
@@ -1,6 +1,6 @@ | |||||
const qrcode = require("qrcode-terminal"); | const qrcode = require("qrcode-terminal"); | ||||
const fs = require("fs"); | const fs = require("fs"); | ||||
const refreshMessage = require("../util/refreshMessage"); | |||||
// const path = require("path"); | // const path = require("path"); | ||||
const { Client, Chat, Message } = 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/"); | ||||
@@ -27,11 +27,11 @@ function callbacks(client, callback, type = "create") { | |||||
if (session == "UNPAIRED" || session == "CONFLICT") { | if (session == "UNPAIRED" || session == "CONFLICT") { | ||||
try { | try { | ||||
if (type == "load" && client && client.info) { | if (type == "load" && client && client.info) { | ||||
console.log(getID); | |||||
// console.log(getID); | |||||
getID = await strapi | getID = await strapi | ||||
.query("session-data") | .query("session-data") | ||||
.findOne({ phonenumber: client.info.wid.user }); | .findOne({ phonenumber: client.info.wid.user }); | ||||
console.log(getID); | |||||
// console.log(getID); | |||||
if (getID) { | if (getID) { | ||||
let id = getID["id"]; | let id = getID["id"]; | ||||
await strapi.api["session-data"].services[ | await strapi.api["session-data"].services[ | ||||
@@ -56,8 +56,16 @@ function callbacks(client, callback, type = "create") { | |||||
}); | }); | ||||
}); | }); | ||||
client.on("message", async (msg) => { | client.on("message", async (msg) => { | ||||
// console.log("MESSAGE RECEIVED", msg.body); | |||||
var phoneNumber = msg.from; | |||||
// console.log(phoneNumber); | |||||
var chat = await strapi.api["chats"].services["chats"].findOne({ | |||||
phoneNumber: phoneNumber.split("@")[0], | |||||
}); | |||||
await refreshMessage(msg, chat); | |||||
}); | }); | ||||
// client.on("message_create", async (msg) => { | |||||
// }); | |||||
client.on("qr", (qr) => { | client.on("qr", (qr) => { | ||||
return callback({ STATUS: 1, TYPE: "QR_AUTH", DATA: qr }); | return callback({ STATUS: 1, TYPE: "QR_AUTH", DATA: qr }); | ||||
}); | }); | ||||
@@ -1,7 +1,7 @@ | |||||
"use strict"; | "use strict"; | ||||
const messageModel = require("../model/messageModel"); | const messageModel = require("../model/messageModel"); | ||||
const { parseMultipartData, sanitizeEntity } = require("strapi-utils"); | |||||
const moment = require("moment"); | const moment = require("moment"); | ||||
const refreshMessage = require("./refreshMessage"); | |||||
const refreshChat = async function (client, sessionsId) { | const refreshChat = async function (client, sessionsId) { | ||||
try { | try { | ||||
@@ -34,41 +34,16 @@ const refreshChat = async function (client, sessionsId) { | |||||
phoneNumber: result["DATA"][i]["id"].user, | phoneNumber: result["DATA"][i]["id"].user, | ||||
}); | }); | ||||
} | } | ||||
chatId = chatsExist.id; | |||||
var messages = await messageModel.getMessagebyId( | var messages = await messageModel.getMessagebyId( | ||||
client, | client, | ||||
result["DATA"][i]["id"] | result["DATA"][i]["id"] | ||||
); | ); | ||||
if (messages["STATUS"] == 1) { | if (messages["STATUS"] == 1) { | ||||
for (var j = 0; j < messages["DATA"].length; j++) { | for (var j = 0; j < messages["DATA"].length; j++) { | ||||
var Exist = await strapi.api["message"].services[ | |||||
"message" | |||||
].findOne({ | |||||
messageId: messages["DATA"][j]["id"].id, | |||||
}); | |||||
if (!Exist) { | |||||
// console.log(chatId); | |||||
await strapi.query("message").create({ | |||||
messageId: messages["DATA"][j]["id"].id, | |||||
to: messages["DATA"][j]["to"], | |||||
from: messages["DATA"][j]["from"], | |||||
body: messages["DATA"][j]["body"], | |||||
state: messages["DATA"][j]["fromMe"] | |||||
? "SENT" | |||||
: "RECIEVED", | |||||
isForwarded: | |||||
messages["DATA"][j]["isForwarded"] == | |||||
undefined | |||||
? false | |||||
: messages["DATA"][j][ | |||||
"isForwarded" | |||||
], | |||||
chat_key: chatId, | |||||
timestamp: moment | |||||
.unix(messages["DATA"][j]["timestamp"]) | |||||
.toDate(), | |||||
}); | |||||
} | |||||
await refreshMessage( | |||||
messages["DATA"][j], | |||||
chatsExist | |||||
); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
@@ -84,6 +59,4 @@ const refreshChat = async function (client, sessionsId) { | |||||
} | } | ||||
}; | }; | ||||
module.exports = { | |||||
refreshChat: refreshChat, | |||||
}; | |||||
module.exports = refreshChat; |
@@ -0,0 +1,27 @@ | |||||
"use strict"; | |||||
const moment = require("moment"); | |||||
const refreshMessage = async function (message, chat) { | |||||
var Exist = await strapi.api["message"].services["message"].findOne({ | |||||
messageId: message["id"].id, | |||||
}); | |||||
if (!Exist) { | |||||
// console.log(chatId); | |||||
await strapi.query("message").create({ | |||||
messageId: message["id"].id, | |||||
to: message["to"], | |||||
from: message["from"], | |||||
body: message["body"], | |||||
state: message["fromMe"] ? "SENT" : "RECIEVED", | |||||
isForwarded: | |||||
message["isForwarded"] == undefined | |||||
? false | |||||
: message["isForwarded"], | |||||
chat_key: chat.id, | |||||
timestamp: moment.unix(message["timestamp"]).toDate(), | |||||
}); | |||||
// console.log("message added"); | |||||
} | |||||
}; | |||||
module.exports = refreshMessage; |
@@ -0,0 +1,55 @@ | |||||
const messageModel = require("../model/messageModel"); | |||||
const moment = require("moment"); | |||||
module.exports = async function (client, sessionId) { | |||||
const messages = await strapi.api.message.services.message.getQueueMessage( | |||||
sessionId | |||||
); | |||||
for (const message of messages) { | |||||
try { | |||||
if (client != null) { | |||||
await strapi.api.message.services.message.update( | |||||
{ id: message.id }, | |||||
{ state: "SENDING" } | |||||
); | |||||
let send = await messageModel.sendMessage( | |||||
client, | |||||
message["chat_key"]["chatId"]._serialized, | |||||
message["body"] | |||||
); | |||||
if (send["STATUS"] == 1) { | |||||
// console.log(send["DATA"]); | |||||
await strapi.api.message.services.message.update( | |||||
{ id: message.id }, | |||||
{ | |||||
state: "SENT", | |||||
messageId: send["DATA"]["id"]["id"], | |||||
from: send["DATA"]["from"], | |||||
isForwarded: | |||||
send["DATA"]["isForwarded"] == undefined | |||||
? false | |||||
: send["DATA"]["isForwarded"], | |||||
timestamp: moment | |||||
.unix(send["DATA"]["timestamp"]) | |||||
.toDate(), | |||||
} | |||||
); | |||||
} else { | |||||
await strapi.api.message.services.message.update( | |||||
{ id: message.id }, | |||||
{ | |||||
state: "ERROR", | |||||
keterangan: send["DATA"], | |||||
} | |||||
); | |||||
} | |||||
} | |||||
} catch (e) { | |||||
// console.log("err", e); | |||||
await strapi.api.message.services.message.update( | |||||
{ id: message.id }, | |||||
{ state: "ERROR", keterangan: e.message } | |||||
); | |||||
} | |||||
} | |||||
}; |