Procházet zdrojové kódy

sampek keluari contact

master
jefry před 4 roky
rodič
revize
536413332e
5 změnil soubory, kde provedl 78 přidání a 20 odebrání
  1. +8
    -0
      api/message/config/routes.json
  2. +33
    -2
      api/message/controllers/message.js
  3. +0
    -1
      api/session-data/controllers/session-data.js
  4. +6
    -16
      config/functions/cron.js
  5. +31
    -1
      model/messageModel.js

+ 8
- 0
api/message/config/routes.json Zobrazit soubor

@@ -47,6 +47,14 @@
"config": {
"policies": []
}
},
{
"method": "POST",
"path": "/message/history",
"handler": "message.history",
"config": {
"policies": []
}
}
]
}

+ 33
- 2
api/message/controllers/message.js Zobrazit soubor

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

+ 0
- 1
api/session-data/controllers/session-data.js Zobrazit soubor

@@ -1,6 +1,5 @@
const messageModel = require("../../../model/messageModel");
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)
* to customize this controller


+ 6
- 16
config/functions/cron.js Zobrazit soubor

@@ -14,18 +14,6 @@ let clients = {};
let taskClientLoading = 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 = {
"*/10 * * * * *": async () => {
if (!taskClientLoading) {
@@ -41,9 +29,11 @@ module.exports = {
if (!Object.keys(clients).includes(`${i["id"]}`)) {
taskClientLoading = true;
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") {
clients[i["id"]] = data["DATA"];
} else if (data["TYPE"] == "AUTH_FAILURE") {
@@ -109,7 +99,7 @@ module.exports = {
message["send_to"],
message["raw_text"]
);
console.log(send);
// console.log(send);
if (send["STATUS"] == 1) {
await strapi.api.message.services.message.update(
{ id: message.id },


+ 31
- 1
model/messageModel.js Zobrazit soubor

@@ -2,7 +2,7 @@ const qrcode = require("qrcode-terminal");
const fs = require("fs");

// 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_FILE_PATH = path.join(__dirname, "../data/session.json");
// 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) {
try {
client = new Client({
@@ -137,10 +149,26 @@ var sendMessage = async function (client, phone_number, textMessage) {
var phoneNumber = reformatMobilePhone(phone_number);
// var state = await client.isRegisteredUser(phoneNumber);
// console.log(state);

await client.sendMessage(phoneNumber, textMessage);

return { STATUS: 1, DATA: "Messages Succesfully send !" };
} 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 };
}
};
@@ -215,4 +243,6 @@ module.exports = {
destroySession: destroySession,
getClientState: getClientState,
getClientState: getClientState,
getChatbyPhoneNumber: getChatbyPhoneNumber,
asyncLoadClient: asyncLoadClient,
};

Načítá se…
Zrušit
Uložit