Przeglądaj źródła

creating robot

master
jefry 4 lat temu
rodzic
commit
0ffb5b759c
11 zmienionych plików z 173 dodań i 11 usunięć
  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 Wyświetl plik

@@ -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 Wyświetl plik

@@ -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 Wyświetl plik

@@ -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 Wyświetl plik

@@ -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 Wyświetl plik

@@ -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 Wyświetl plik

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

+ 4
- 0
api/message-template/models/message-template.settings.json Wyświetl plik

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

+ 2
- 0
api/session-data/controllers/session-data.js Wyświetl plik

@@ -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);
}


+ 3
- 0
api/session-data/models/session-data.settings.json Wyświetl plik

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

+ 24
- 11
util/refreshChat.js Wyświetl plik

@@ -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,


+ 8
- 0
util/refreshMessage.js Wyświetl plik

@@ -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,
}
);
}
};



Ładowanie…
Anuluj
Zapisz