link-stack/apps/metamigo-api/src/app/services/whatsapp.ts

288 lines
8.6 KiB
TypeScript
Raw Normal View History

2023-02-13 12:41:30 +00:00
import { Server } from "@hapi/hapi";
import { Service } from "@hapipal/schmervice";
import { SavedWhatsappBot as Bot } from "@digiresilience/metamigo-db";
2023-03-13 14:42:49 +00:00
import makeWASocket, {
DisconnectReason,
proto,
downloadContentFromMessage,
MediaType,
AnyMessageContent,
WAProto,
MiscMessageGenerationOptions,
} from "@adiwajshing/baileys";
2023-02-13 12:41:30 +00:00
import workerUtils from "../../worker-utils";
import { useDatabaseAuthState } from "../lib/whatsapp-key-store";
export type AuthCompleteCallback = (error?: string) => void;
2023-03-13 14:42:49 +00:00
export type Connection = {
end: (error: Error | undefined) => void;
sendMessage: (
jid: string,
content: AnyMessageContent,
options?: MiscMessageGenerationOptions
) => Promise<WAProto.WebMessageInfo | undefined>;
};
2023-02-13 12:41:30 +00:00
export default class WhatsappService extends Service {
2023-03-13 14:42:49 +00:00
connections: { [key: string]: Connection } = {};
2023-02-13 12:41:30 +00:00
static browserDescription: [string, string, string] = [
"Metamigo",
"Chrome",
"2.0",
];
constructor(server: Server, options: never) {
super(server, options);
}
async initialize(): Promise<void> {
this.updateConnections();
}
async teardown(): Promise<void> {
this.resetConnections();
}
private async sleep(ms: number): Promise<void> {
2023-03-13 14:42:49 +00:00
console.log(`pausing ${ms}`);
return new Promise((resolve) => setTimeout(resolve, ms));
2023-02-13 12:41:30 +00:00
}
private async resetConnections() {
for (const connection of Object.values(this.connections)) {
try {
2023-03-13 14:42:49 +00:00
connection.end(null);
2023-02-13 12:41:30 +00:00
} catch (error) {
console.log(error);
}
}
2023-03-13 14:42:49 +00:00
2023-02-13 12:41:30 +00:00
this.connections = {};
}
2023-03-13 14:42:49 +00:00
private createConnection(
bot: Bot,
server: Server,
options: any,
authCompleteCallback?: any
) {
const { state, saveState } = useDatabaseAuthState(bot, server);
2023-02-13 12:41:30 +00:00
const connection = makeWASocket({ ...options, auth: state });
let pause = 5000;
2023-03-13 14:42:49 +00:00
connection.ev.on("connection.update", async (update) => {
console.log(`Connection updated ${JSON.stringify(update, null, 2)}`);
const {
connection: connectionState,
lastDisconnect,
qr,
isNewLogin,
} = update;
2023-02-13 12:41:30 +00:00
if (qr) {
2023-03-13 14:42:49 +00:00
console.log("got qr code");
2023-02-13 12:41:30 +00:00
await this.server.db().whatsappBots.updateQR(bot, qr);
} else if (isNewLogin) {
2023-03-13 14:42:49 +00:00
console.log("got new login");
} else if (connectionState === "open") {
console.log("opened connection");
2023-02-13 12:41:30 +00:00
} else if (connectionState === "close") {
2023-03-13 14:42:49 +00:00
console.log("connection closed due to", lastDisconnect.error);
const disconnectStatusCode = (lastDisconnect?.error as any)?.output
?.statusCode;
2023-02-13 12:41:30 +00:00
if (disconnectStatusCode === DisconnectReason.restartRequired) {
2023-03-13 14:42:49 +00:00
console.log("reconnecting after got new login");
2023-02-13 12:41:30 +00:00
const updatedBot = await this.findById(bot.id);
2023-03-13 14:42:49 +00:00
this.createConnection(updatedBot, server, options);
authCompleteCallback();
2023-02-13 12:41:30 +00:00
} else if (disconnectStatusCode !== DisconnectReason.loggedOut) {
2023-03-13 14:42:49 +00:00
console.log("reconnecting");
await this.sleep(pause);
pause *= 2;
this.createConnection(bot, server, options);
2023-02-13 12:41:30 +00:00
}
}
2023-03-13 14:42:49 +00:00
});
connection.ev.process(async (events) => {
if (events["messaging-history.set"]) {
const { chats, contacts, messages, isLatest } =
events["messaging-history.set"];
console.log(
`recv ${chats.length} chats, ${contacts.length} contacts, ${messages.length} msgs (is latest: ${isLatest})`
);
}
});
2023-02-13 12:41:30 +00:00
2023-03-13 14:42:49 +00:00
connection.ev.on("messages.upsert", async (m) => {
console.log("messages upsert");
2023-02-13 12:41:30 +00:00
const { messages } = m;
if (messages) {
await this.queueUnreadMessages(bot, messages);
}
2023-03-13 14:42:49 +00:00
});
connection.ev.on("messages.update", (m) => console.log(m));
connection.ev.on("message-receipt.update", (m) => console.log(m));
connection.ev.on("presence.update", (m) => console.log(m));
connection.ev.on("chats.update", (m) => console.log(m));
connection.ev.on("contacts.upsert", (m) => console.log(m));
connection.ev.on("creds.update", saveState);
2023-02-13 12:41:30 +00:00
this.connections[bot.id] = connection;
}
private async updateConnections() {
this.resetConnections();
const bots = await this.server.db().whatsappBots.findAll();
for await (const bot of bots) {
if (bot.isVerified) {
2023-03-13 14:42:49 +00:00
this.createConnection(bot, this.server, {
browser: WhatsappService.browserDescription,
printQRInTerminal: false,
version: [2, 2204, 13],
});
2023-02-13 12:41:30 +00:00
}
}
}
private async queueMessage(bot: Bot, webMessageInfo: proto.WebMessageInfo) {
const { key, message, messageTimestamp } = webMessageInfo;
const { remoteJid } = key;
if (!key.fromMe && message && remoteJid !== "status@broadcast") {
const isMediaMessage =
message.audioMessage ||
message.documentMessage ||
message.imageMessage ||
message.videoMessage;
2023-03-13 14:42:49 +00:00
const messageContent = Object.values(message)[0];
2023-02-13 12:41:30 +00:00
let messageType: MediaType;
let attachment: string;
let filename: string;
let mimetype: string;
if (isMediaMessage) {
if (message.audioMessage) {
messageType = "audio";
filename =
key.id + "." + message.audioMessage.mimetype.split("/").pop();
mimetype = message.audioMessage.mimetype;
} else if (message.documentMessage) {
messageType = "document";
filename = message.documentMessage.fileName;
mimetype = message.documentMessage.mimetype;
} else if (message.imageMessage) {
messageType = "image";
filename =
key.id + "." + message.imageMessage.mimetype.split("/").pop();
mimetype = message.imageMessage.mimetype;
} else if (message.videoMessage) {
2023-03-13 14:42:49 +00:00
messageType = "video";
2023-02-13 12:41:30 +00:00
filename =
key.id + "." + message.videoMessage.mimetype.split("/").pop();
mimetype = message.videoMessage.mimetype;
}
2023-03-13 14:42:49 +00:00
const stream = await downloadContentFromMessage(
messageContent,
messageType
);
let buffer = Buffer.from([]);
2023-02-13 12:41:30 +00:00
for await (const chunk of stream) {
2023-03-13 14:42:49 +00:00
buffer = Buffer.concat([buffer, chunk]);
2023-02-13 12:41:30 +00:00
}
2023-03-13 14:42:49 +00:00
2023-02-13 12:41:30 +00:00
attachment = buffer.toString("base64");
}
if (messageContent || attachment) {
const receivedMessage = {
waMessageId: key.id,
waMessage: JSON.stringify(webMessageInfo),
waTimestamp: new Date((messageTimestamp as number) * 1000),
attachment,
filename,
mimetype,
whatsappBotId: bot.id,
botPhoneNumber: bot.phoneNumber,
};
workerUtils.addJob("whatsapp-message", receivedMessage, {
jobKey: key.id,
});
}
}
}
private async queueUnreadMessages(bot: Bot, messages: any[]) {
for await (const message of messages) {
await this.queueMessage(bot, message);
}
}
async create(
phoneNumber: string,
description: string,
email: string
): Promise<Bot> {
const db = this.server.db();
const user = await db.users.findBy({ email });
const row = await db.whatsappBots.insert({
phoneNumber,
description,
userId: user.id,
});
return row;
}
async findAll(): Promise<Bot[]> {
return this.server.db().whatsappBots.findAll();
}
async findById(id: string): Promise<Bot> {
return this.server.db().whatsappBots.findById({ id });
}
async findByToken(token: string): Promise<Bot> {
return this.server.db().whatsappBots.findBy({ token });
}
async register(bot: Bot, callback: AuthCompleteCallback): Promise<void> {
2023-03-13 14:42:49 +00:00
await this.createConnection(
bot,
this.server,
{ version: [2, 2204, 13] },
callback
);
2023-02-13 12:41:30 +00:00
}
async send(bot: Bot, phoneNumber: string, message: string): Promise<void> {
const connection = this.connections[bot.id];
const recipient = `${phoneNumber.replace(/\D+/g, "")}@s.whatsapp.net`;
await connection.sendMessage(recipient, { text: message });
}
async receiveSince(bot: Bot, lastReceivedDate: Date): Promise<void> {
const connection = this.connections[bot.id];
const messages = await connection.messagesReceivedAfter(
lastReceivedDate,
false
);
for (const message of messages) {
this.queueMessage(bot, message);
}
}
async receive(bot: Bot, lastReceivedDate: Date): Promise<any> {
const connection = this.connections[bot.id];
// const messages = await connection.messagesReceivedAfter(
// lastReceivedDate,
// false
// );
const messages = await connection.loadAllUnreadMessages();
return messages;
}
}