diff --git a/api/bot/config/routes.json b/api/bot/config/routes.json new file mode 100644 index 0000000..28c1177 --- /dev/null +++ b/api/bot/config/routes.json @@ -0,0 +1,52 @@ +{ + "routes": [ + { + "method": "GET", + "path": "/bots", + "handler": "bot.find", + "config": { + "policies": [] + } + }, + { + "method": "GET", + "path": "/bots/count", + "handler": "bot.count", + "config": { + "policies": [] + } + }, + { + "method": "GET", + "path": "/bots/:id", + "handler": "bot.findOne", + "config": { + "policies": [] + } + }, + { + "method": "POST", + "path": "/bots", + "handler": "bot.create", + "config": { + "policies": [] + } + }, + { + "method": "PUT", + "path": "/bots/:id", + "handler": "bot.update", + "config": { + "policies": [] + } + }, + { + "method": "DELETE", + "path": "/bots/:id", + "handler": "bot.delete", + "config": { + "policies": [] + } + } + ] +} diff --git a/api/bot/controllers/bot.js b/api/bot/controllers/bot.js new file mode 100644 index 0000000..556f4fc --- /dev/null +++ b/api/bot/controllers/bot.js @@ -0,0 +1,8 @@ +'use strict'; + +/** + * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/controllers.html#core-controllers) + * to customize this controller + */ + +module.exports = {}; diff --git a/api/bot/models/bot.js b/api/bot/models/bot.js new file mode 100644 index 0000000..dd847ef --- /dev/null +++ b/api/bot/models/bot.js @@ -0,0 +1,8 @@ +'use strict'; + +/** + * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/models.html#lifecycle-hooks) + * to customize this model + */ + +module.exports = {}; diff --git a/api/bot/models/bot.settings.json b/api/bot/models/bot.settings.json new file mode 100644 index 0000000..3dd1c2f --- /dev/null +++ b/api/bot/models/bot.settings.json @@ -0,0 +1,49 @@ +{ + "kind": "collectionType", + "collectionName": "bots", + "info": { + "name": "Bot", + "description": "" + }, + "options": { + "increments": true, + "timestamps": true, + "draftAndPublish": true + }, + "attributes": { + "conditions": { + "type": "enumeration", + "enum": [ + "time_based", + "query_based" + ] + }, + "trigger": { + "type": "string" + }, + "start_date": { + "type": "datetime" + }, + "end_date": { + "type": "datetime" + }, + "periodic": { + "type": "boolean" + }, + "message_template_key": { + "model": "message-template", + "via": "bot" + }, + "template_query": { + "type": "json" + }, + "apply_to": { + "collection": "chats", + "via": "bots", + "dominant": true + }, + "name": { + "type": "string" + } + } +} diff --git a/api/bot/services/bot.js b/api/bot/services/bot.js new file mode 100644 index 0000000..6bc4168 --- /dev/null +++ b/api/bot/services/bot.js @@ -0,0 +1,8 @@ +'use strict'; + +/** + * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/services.html#core-services) + * to customize this service + */ + +module.exports = {}; diff --git a/api/chats/models/chats.settings.json b/api/chats/models/chats.settings.json index b33ea01..b77dd21 100644 --- a/api/chats/models/chats.settings.json +++ b/api/chats/models/chats.settings.json @@ -28,6 +28,13 @@ }, "timestamp": { "type": "datetime" + }, + "last_sessionId": { + "type": "string" + }, + "bots": { + "via": "apply_to", + "collection": "bot" } } } diff --git a/api/message-template/models/message-template.settings.json b/api/message-template/models/message-template.settings.json index 1770c24..3a5f969 100644 --- a/api/message-template/models/message-template.settings.json +++ b/api/message-template/models/message-template.settings.json @@ -27,6 +27,10 @@ "message": { "via": "template_key", "model": "message" + }, + "bot": { + "via": "message_template_key", + "model": "bot" } } } diff --git a/api/session-data/controllers/session-data.js b/api/session-data/controllers/session-data.js index f2b7e30..0b7f10b 100644 --- a/api/session-data/controllers/session-data.js +++ b/api/session-data/controllers/session-data.js @@ -41,6 +41,7 @@ module.exports = { os_version: data["DATA"].phone.os_version, phonenumber: data["DATA"].wid.user, session_data: data["DATA"].sessionData, + sessionId: data["DATA"].wid._serialized, } ); } else { @@ -53,6 +54,7 @@ module.exports = { os_version: data["DATA"].phone.os_version, phonenumber: data["DATA"].wid.user, session_data: data["DATA"].sessionData, + sessionId: data["DATA"].wid._serialized, }); // console.log(entry); } diff --git a/api/session-data/models/session-data.settings.json b/api/session-data/models/session-data.settings.json index 92723d9..9cf3ba8 100644 --- a/api/session-data/models/session-data.settings.json +++ b/api/session-data/models/session-data.settings.json @@ -32,6 +32,9 @@ "phonenumber": { "type": "string", "required": true + }, + "sessionId": { + "type": "string" } } } diff --git a/util/refreshChat.js b/util/refreshChat.js index 3707010..446f2ec 100644 --- a/util/refreshChat.js +++ b/util/refreshChat.js @@ -8,6 +8,7 @@ const refreshChat = async function (client, sessionsId) { if (client && sessionsId) { var chatId; var result = await messageModel.getAllChats(client); + // console.log(result["DATA"]); if (result["STATUS"] == 1) { for (var i = 0; i < result["DATA"].length; i++) { var chatsExist = await strapi.api["chats"].services[ @@ -17,22 +18,34 @@ const refreshChat = async function (client, sessionsId) { phoneNumber: result["DATA"][i]["id"].user, }); if (!chatsExist) { - await strapi.query("chats").create({ - session_key: sessionsId, - name: result["DATA"][i]["name"], - unreadCount: result["DATA"][i]["unreadCount"], - timestamp: moment - .unix(result["DATA"][i]["timestamp"]) - .toDate(), - chatId: result["DATA"][i]["id"], - phoneNumber: result["DATA"][i]["id"].user, - }); + //check again with last active session chatsExist = await strapi.api["chats"].services[ "chats" ].findOne({ - session_key: sessionsId, + last_sessionId: client.info.me._serialized, phoneNumber: result["DATA"][i]["id"].user, }); + if (!chatsExist) { + await strapi.query("chats").create({ + session_key: sessionsId, + name: result["DATA"][i]["name"], + unreadCount: result["DATA"][i]["unreadCount"], + timestamp: moment + .unix(result["DATA"][i]["timestamp"]) + .toDate(), + chatId: result["DATA"][i]["id"], + phoneNumber: result["DATA"][i]["id"].user, + last_sessionId: client.info.me._serialized, + }); + } else { + let id = chatsExist.id; + await strapi.query("chats").update( + { id }, + { + session_key: sessionsId, + } + ); + } } var messages = await messageModel.getMessagebyId( client, diff --git a/util/refreshMessage.js b/util/refreshMessage.js index a42fbdd..193ff45 100644 --- a/util/refreshMessage.js +++ b/util/refreshMessage.js @@ -21,6 +21,14 @@ const refreshMessage = async function (message, chat) { timestamp: moment.unix(message["timestamp"]).toDate(), }); // console.log("message added"); + } else { + let id = Exist.id; + await strapi.query("message").update( + { id }, + { + chat_key: chat.id, + } + ); } };