瀏覽代碼

creating robot

master
jefry 4 年之前
父節點
當前提交
0ffb5b759c
共有 11 個檔案被更改,包括 173 行新增11 行删除
  1. +52
    -0
      api/bot/config/routes.json
  2. +8
    -0
      api/bot/controllers/bot.js
  3. +8
    -0
      api/bot/models/bot.js
  4. +49
    -0
      api/bot/models/bot.settings.json
  5. +8
    -0
      api/bot/services/bot.js
  6. +7
    -0
      api/chats/models/chats.settings.json
  7. +4
    -0
      api/message-template/models/message-template.settings.json
  8. +2
    -0
      api/session-data/controllers/session-data.js
  9. +3
    -0
      api/session-data/models/session-data.settings.json
  10. +24
    -11
      util/refreshChat.js
  11. +8
    -0
      util/refreshMessage.js

+ 52
- 0
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": []
}
}
]
}

+ 8
- 0
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 = {};

+ 8
- 0
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 = {};

+ 49
- 0
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"
}
}
}

+ 8
- 0
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 = {};

+ 7
- 0
api/chats/models/chats.settings.json 查看文件

@@ -28,6 +28,13 @@
}, },
"timestamp": { "timestamp": {
"type": "datetime" "type": "datetime"
},
"last_sessionId": {
"type": "string"
},
"bots": {
"via": "apply_to",
"collection": "bot"
} }
} }
} }

+ 4
- 0
api/message-template/models/message-template.settings.json 查看文件

@@ -27,6 +27,10 @@
"message": { "message": {
"via": "template_key", "via": "template_key",
"model": "message" "model": "message"
},
"bot": {
"via": "message_template_key",
"model": "bot"
} }
} }
} }

+ 2
- 0
api/session-data/controllers/session-data.js 查看文件

@@ -41,6 +41,7 @@ module.exports = {
os_version: data["DATA"].phone.os_version, os_version: data["DATA"].phone.os_version,
phonenumber: data["DATA"].wid.user, phonenumber: data["DATA"].wid.user,
session_data: data["DATA"].sessionData, session_data: data["DATA"].sessionData,
sessionId: data["DATA"].wid._serialized,
} }
); );
} else { } else {
@@ -53,6 +54,7 @@ module.exports = {
os_version: data["DATA"].phone.os_version, os_version: data["DATA"].phone.os_version,
phonenumber: data["DATA"].wid.user, phonenumber: data["DATA"].wid.user,
session_data: data["DATA"].sessionData, session_data: data["DATA"].sessionData,
sessionId: data["DATA"].wid._serialized,
}); });
// console.log(entry); // console.log(entry);
} }


+ 3
- 0
api/session-data/models/session-data.settings.json 查看文件

@@ -32,6 +32,9 @@
"phonenumber": { "phonenumber": {
"type": "string", "type": "string",
"required": true "required": true
},
"sessionId": {
"type": "string"
} }
} }
} }

+ 24
- 11
util/refreshChat.js 查看文件

@@ -8,6 +8,7 @@ const refreshChat = async function (client, sessionsId) {
if (client && sessionsId) { if (client && sessionsId) {
var chatId; var chatId;
var result = await messageModel.getAllChats(client); var result = await messageModel.getAllChats(client);
// console.log(result["DATA"]);
if (result["STATUS"] == 1) { if (result["STATUS"] == 1) {
for (var i = 0; i < result["DATA"].length; i++) { for (var i = 0; i < result["DATA"].length; i++) {
var chatsExist = await strapi.api["chats"].services[ var chatsExist = await strapi.api["chats"].services[
@@ -17,22 +18,34 @@ const refreshChat = async function (client, sessionsId) {
phoneNumber: result["DATA"][i]["id"].user, phoneNumber: result["DATA"][i]["id"].user,
}); });
if (!chatsExist) { 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[ chatsExist = await strapi.api["chats"].services[
"chats" "chats"
].findOne({ ].findOne({
session_key: sessionsId,
last_sessionId: client.info.me._serialized,
phoneNumber: result["DATA"][i]["id"].user, 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( var messages = await messageModel.getMessagebyId(
client, client,


+ 8
- 0
util/refreshMessage.js 查看文件

@@ -21,6 +21,14 @@ const refreshMessage = async function (message, chat) {
timestamp: moment.unix(message["timestamp"]).toDate(), timestamp: moment.unix(message["timestamp"]).toDate(),
}); });
// console.log("message added"); // console.log("message added");
} else {
let id = Exist.id;
await strapi.query("message").update(
{ id },
{
chat_key: chat.id,
}
);
} }
}; };




Loading…
取消
儲存