@@ -11,10 +11,8 @@ const { parseMultipartData, sanitizeEntity } = require("strapi-utils"); | |||||
module.exports = { | module.exports = { | ||||
history: async (ctx) => { | history: async (ctx) => { | ||||
// console.log("history"); | // console.log("history"); | ||||
// ctx.respond = false; | |||||
try { | try { | ||||
const { request } = ctx; | const { request } = ctx; | ||||
let phone = request.body.phoneNumber; | |||||
let sessionID = request.body.id; | let sessionID = request.body.id; | ||||
const sessionData = await strapi.api["session-data"].services[ | const sessionData = await strapi.api["session-data"].services[ | ||||
@@ -29,11 +27,36 @@ module.exports = { | |||||
result = { STATUS: 0, DATA: message }; | result = { STATUS: 0, DATA: message }; | ||||
}); | }); | ||||
if (data["TYPE"] == "READY") { | if (data["TYPE"] == "READY") { | ||||
result = await messageModel.getChatbyPhoneNumber(phone); | |||||
result = await messageModel.getAllChats(); | |||||
} | } | ||||
ctx.body = result; | ctx.body = result; | ||||
} catch (e) { | } catch (e) { | ||||
ctx.throw(400, e.message); | ctx.throw(400, e.message); | ||||
} | } | ||||
// 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 }; | |||||
// }); | |||||
// console.log(data); | |||||
// if (data["TYPE"] == "READY") { | |||||
// result = await messageModel.getChatbyPhoneNumber(phone); | |||||
// } | |||||
// ctx.body = result; | |||||
// } catch (e) { | |||||
// ctx.throw(400, e.message); | |||||
// } | |||||
}, | }, | ||||
}; | }; |
@@ -66,7 +66,7 @@ function callbacks(client, callback, type = "create") { | |||||
// console.log(`state => ${state}`); | // console.log(`state => ${state}`); | ||||
}); | }); | ||||
client.on("authenticated", (session) => { | client.on("authenticated", (session) => { | ||||
// console.log("authenticated"); | |||||
console.log("authenticated"); | |||||
try { | try { | ||||
sessionData = session; | sessionData = session; | ||||
// fs.writeFileSync(SESSION_FILE_PATH, JSON.stringify(session)); | // fs.writeFileSync(SESSION_FILE_PATH, JSON.stringify(session)); | ||||
@@ -132,7 +132,7 @@ var createNewClient = async function (callback) { | |||||
}); | }); | ||||
callbacks(client, callback); | callbacks(client, callback); | ||||
console.log("initialize"); | |||||
client.initialize(); | client.initialize(); | ||||
//var clientInfo = await getClientInfo(); | //var clientInfo = await getClientInfo(); | ||||
//return clientInfo; | //return clientInfo; | ||||
@@ -173,6 +173,18 @@ var getChatbyPhoneNumber = async function (phone_number) { | |||||
} | } | ||||
}; | }; | ||||
var getAllChats = async function () { | |||||
try { | |||||
if (!client.info) { | |||||
throw { message: "Client Is Not Ready !" }; | |||||
} | |||||
let chatList = await client.getChats(); | |||||
return { STATUS: 1, DATA: chatList }; | |||||
} catch (e) { | |||||
return { STATUS: 0, DATA: e.message }; | |||||
} | |||||
}; | |||||
var getClientInfo = function () { | var getClientInfo = function () { | ||||
try { | try { | ||||
var a; | var a; | ||||
@@ -245,4 +257,5 @@ module.exports = { | |||||
getClientState: getClientState, | getClientState: getClientState, | ||||
getChatbyPhoneNumber: getChatbyPhoneNumber, | getChatbyPhoneNumber: getChatbyPhoneNumber, | ||||
asyncLoadClient: asyncLoadClient, | asyncLoadClient: asyncLoadClient, | ||||
getAllChats: getAllChats, | |||||
}; | }; |