mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 20:59:16 +00:00
feat: starting handling messages sent from phone
This commit is contained in:
@@ -9,7 +9,7 @@ const Ticket = require("../models/Ticket");
|
|||||||
const Message = require("../models/Message");
|
const Message = require("../models/Message");
|
||||||
|
|
||||||
const { getIO } = require("../libs/socket");
|
const { getIO } = require("../libs/socket");
|
||||||
const { getWbot, init } = require("../libs/wbot");
|
const { getWbot } = require("../libs/wbot");
|
||||||
|
|
||||||
const verifyContact = async (msgContact, profilePicUrl) => {
|
const verifyContact = async (msgContact, profilePicUrl) => {
|
||||||
let contact = await Contact.findOne({
|
let contact = await Contact.findOne({
|
||||||
@@ -136,8 +136,8 @@ const wbotMessageListener = () => {
|
|||||||
const wbot = getWbot();
|
const wbot = getWbot();
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
|
||||||
wbot.on("message", async msg => {
|
wbot.on("message_create", async msg => {
|
||||||
// console.log(msg);
|
console.log(msg);
|
||||||
if (
|
if (
|
||||||
msg.from === "status@broadcast" ||
|
msg.from === "status@broadcast" ||
|
||||||
msg.type === "location" ||
|
msg.type === "location" ||
|
||||||
@@ -147,7 +147,22 @@ const wbotMessageListener = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const msgContact = await msg.getContact();
|
let msgContact;
|
||||||
|
|
||||||
|
if (msg.fromMe) {
|
||||||
|
const alreadyExists = await Message.findOne({
|
||||||
|
where: { id: msg.id.id },
|
||||||
|
});
|
||||||
|
// return if message was already created by messagesController
|
||||||
|
if (alreadyExists) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
msgContact = await wbot.getContactById(msg.to);
|
||||||
|
} else {
|
||||||
|
msgContact = await msg.getContact();
|
||||||
|
}
|
||||||
|
|
||||||
const profilePicUrl = await msgContact.getProfilePicUrl();
|
const profilePicUrl = await msgContact.getProfilePicUrl();
|
||||||
const contact = await verifyContact(msgContact, profilePicUrl);
|
const contact = await verifyContact(msgContact, profilePicUrl);
|
||||||
const ticket = await verifyTicket(contact);
|
const ticket = await verifyTicket(contact);
|
||||||
@@ -164,12 +179,11 @@ const wbotMessageListener = () => {
|
|||||||
const messageToUpdate = await Message.findOne({
|
const messageToUpdate = await Message.findOne({
|
||||||
where: { id: msg.id.id },
|
where: { id: msg.id.id },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!messageToUpdate) {
|
if (!messageToUpdate) {
|
||||||
// will throw an error if msg was sent from cellphone
|
return;
|
||||||
const error = new Error("No message with this ID found in database");
|
|
||||||
error.statusCode = 404;
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await messageToUpdate.update({ ack: ack });
|
await messageToUpdate.update({ ack: ack });
|
||||||
|
|
||||||
io.to(messageToUpdate.ticketId).emit("appMessage", {
|
io.to(messageToUpdate.ticketId).emit("appMessage", {
|
||||||
|
|||||||
Reference in New Issue
Block a user