projeto-hit/backend/src/services/WbotServices/StartWhatsAppSession.ts

32 lines
896 B
TypeScript
Raw Normal View History

import { initWbot } from "../../libs/wbot";
import Whatsapp from "../../models/Whatsapp";
import { wbotMessageListener } from "./wbotMessageListener";
import { getIO } from "../../libs/socket";
import wbotMonitor from "./wbotMonitor";
import { logger } from "../../utils/logger";
2022-11-17 14:40:37 +00:00
import { insertOrUpeateWhatsCache } from "../../helpers/WhatsCache";
2022-07-12 12:37:34 +00:00
export const StartWhatsAppSession = async (whatsapp: Whatsapp, backupSession: boolean = false): Promise<void> => {
await whatsapp.update({ status: "OPENING" });
2022-11-17 14:40:37 +00:00
await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, {
status: "OPENING",
})
const io = getIO();
io.emit("whatsappSession", {
action: "update",
session: whatsapp
});
try {
2022-07-12 12:37:34 +00:00
const wbot = await initWbot(whatsapp, backupSession);
wbotMessageListener(wbot);
wbotMonitor(wbot, whatsapp);
} catch (err) {
logger.error(`${err}`);
}
};