Migrate changes from other Metamigo repo
This commit is contained in:
parent
8669b09224
commit
27810142b3
11 changed files with 615 additions and 235 deletions
|
|
@ -141,6 +141,27 @@ export const RegisterBotRoute = Helpers.withDefaults({
|
|||
},
|
||||
});
|
||||
|
||||
export const UnverifyBotRoute = Helpers.withDefaults({
|
||||
method: "post",
|
||||
path: "/api/whatsapp/bots/{id}/unverify",
|
||||
options: {
|
||||
description: "Unverify bot",
|
||||
async handler: (request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { id } = request.params;
|
||||
const { whatsappService } = request.services();
|
||||
|
||||
const bot = await whatsappService.findById(id);
|
||||
|
||||
if (bot) {
|
||||
return whatsappService.unverify(bot);
|
||||
}
|
||||
|
||||
throw Boom.notFound("Bot not found");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
export const RefreshBotRoute = Helpers.withDefaults({
|
||||
method: "get",
|
||||
path: "/api/whatsapp/bots/{id}/refresh",
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { Server } from "@hapi/hapi";
|
||||
import { Service } from "@hapipal/schmervice";
|
||||
import { promises as fs } from "fs";
|
||||
import {
|
||||
SignaldAPI,
|
||||
IncomingMessagev1,
|
||||
|
|
@ -179,6 +180,22 @@ export default class SignaldService extends Service {
|
|||
await this.queueMessage(bot, message);
|
||||
}
|
||||
|
||||
private async getAttachmentInfo(dataMessage: any) {
|
||||
if (dataMessage.attachments?.length > 0) {
|
||||
const attachmentInfo = dataMessage.attachments[0];
|
||||
const buffer = await fs.readFile(attachmentInfo.storedFilename);
|
||||
const attachment = buffer.toString("base64");
|
||||
const mimetype = attachmentInfo.contentType ?? "application/octet-stream";
|
||||
const filename = attachmentInfo.customFilename ?? "unknown-filename";
|
||||
|
||||
|
||||
return { attachment, mimetype, filename }
|
||||
}
|
||||
|
||||
return { attachment: null, mimetype: null, filename: null };
|
||||
}
|
||||
|
||||
|
||||
private async queueMessage(bot: Bot, message: IncomingMessagev1) {
|
||||
const { timestamp, account, data_message: dataMessage } = message;
|
||||
if (!dataMessage?.body && !dataMessage?.attachments) {
|
||||
|
|
@ -190,10 +207,15 @@ export default class SignaldService extends Service {
|
|||
this.server.logger.debug({ message }, "invalid message received");
|
||||
}
|
||||
|
||||
const { attachment, mimetype, filename } = await this.getAttachmentInfo(dataMessage);
|
||||
|
||||
const receivedMessage = {
|
||||
message,
|
||||
botId: bot.id,
|
||||
botPhoneNumber: bot.phoneNumber,
|
||||
attachment,
|
||||
mimetype,
|
||||
filename
|
||||
};
|
||||
|
||||
workerUtils.addJob("signald-message", receivedMessage, {
|
||||
|
|
|
|||
|
|
@ -1,32 +1,17 @@
|
|||
/* eslint-disable unicorn/no-abusive-eslint-disable */
|
||||
/* eslint-disable */
|
||||
import { Server } from "@hapi/hapi";
|
||||
import { Service } from "@hapipal/schmervice";
|
||||
import { SavedWhatsappBot as Bot } from "@digiresilience/metamigo-db";
|
||||
import makeWASocket, {
|
||||
DisconnectReason,
|
||||
proto,
|
||||
downloadContentFromMessage,
|
||||
MediaType,
|
||||
AnyMessageContent,
|
||||
WAProto,
|
||||
UserFacingSocketConfig,
|
||||
MiscMessageGenerationOptions,
|
||||
} from "@adiwajshing/baileys";
|
||||
import makeWASocket, { DisconnectReason, proto, downloadContentFromMessage, MediaType, fetchLatestBaileysVersion, isJidBroadcast, isJidStatusBroadcast, MessageRetryMap, useMultiFileAuthState } from "@adiwajshing/baileys";
|
||||
import fs from "fs";
|
||||
import workerUtils from "../../worker-utils";
|
||||
import { useDatabaseAuthState } from "../lib/whatsapp-key-store";
|
||||
|
||||
export type AuthCompleteCallback = (error?: string) => void;
|
||||
|
||||
export type Connection = {
|
||||
end: (error: Error | undefined) => void;
|
||||
sendMessage: (
|
||||
jid: string,
|
||||
content: AnyMessageContent,
|
||||
options?: MiscMessageGenerationOptions
|
||||
) => Promise<WAProto.WebMessageInfo | undefined>;
|
||||
};
|
||||
|
||||
export default class WhatsappService extends Service {
|
||||
connections: { [key: string]: Connection } = {};
|
||||
connections: { [key: string]: any } = {};
|
||||
loginConnections: { [key: string]: any } = {};
|
||||
|
||||
static browserDescription: [string, string, string] = [
|
||||
"Metamigo",
|
||||
|
|
@ -38,6 +23,10 @@ export default class WhatsappService extends Service {
|
|||
super(server, options);
|
||||
}
|
||||
|
||||
getAuthDirectory(bot: Bot): string {
|
||||
return `/baileys/${bot.id}`;
|
||||
}
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
this.updateConnections();
|
||||
}
|
||||
|
|
@ -47,93 +36,82 @@ export default class WhatsappService extends Service {
|
|||
}
|
||||
|
||||
private async sleep(ms: number): Promise<void> {
|
||||
console.log(`pausing ${ms}`);
|
||||
// eslint-disable-next-line no-new
|
||||
new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
});
|
||||
console.log(`pausing ${ms}`)
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
private async resetConnections() {
|
||||
for (const connection of Object.values(this.connections)) {
|
||||
try {
|
||||
connection.end(undefined);
|
||||
connection.end(null)
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
this.connections = {};
|
||||
}
|
||||
|
||||
private createConnection(
|
||||
bot: Bot,
|
||||
server: Server,
|
||||
options: Omit<UserFacingSocketConfig, "auth">,
|
||||
authCompleteCallback?: () => void
|
||||
) {
|
||||
const { state, saveState } = useDatabaseAuthState(bot, server);
|
||||
const connection = makeWASocket({ ...options, auth: state });
|
||||
|
||||
private async createConnection(bot: Bot, server: Server, options: any, authCompleteCallback?: any) {
|
||||
const directory = this.getAuthDirectory(bot);
|
||||
const { state, saveCreds } = await useMultiFileAuthState(directory);
|
||||
const msgRetryCounterMap: MessageRetryMap = {}
|
||||
const socket = makeWASocket({
|
||||
...options,
|
||||
auth: state,
|
||||
msgRetryCounterMap,
|
||||
shouldIgnoreJid: jid => isJidBroadcast(jid) || isJidStatusBroadcast(jid),
|
||||
});
|
||||
let pause = 5000;
|
||||
connection.ev.on("connection.update", async (update) => {
|
||||
console.log(`Connection updated ${JSON.stringify(update, undefined, 2)}`);
|
||||
const {
|
||||
connection: connectionState,
|
||||
lastDisconnect,
|
||||
qr,
|
||||
isNewLogin,
|
||||
} = update;
|
||||
if (qr) {
|
||||
console.log("got qr code");
|
||||
await this.server.db().whatsappBots.updateQR(bot, qr);
|
||||
} else if (isNewLogin) {
|
||||
console.log("got new login");
|
||||
} else if (connectionState === "open") {
|
||||
console.log("opened connection");
|
||||
} else if (connectionState === "close") {
|
||||
console.log("connection closed due to", lastDisconnect.error);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const disconnectStatusCode = (lastDisconnect?.error as any)?.output
|
||||
?.statusCode;
|
||||
if (disconnectStatusCode === DisconnectReason.restartRequired) {
|
||||
console.log("reconnecting after got new login");
|
||||
const updatedBot = await this.findById(bot.id);
|
||||
this.createConnection(updatedBot, server, options);
|
||||
authCompleteCallback();
|
||||
} else if (disconnectStatusCode !== DisconnectReason.loggedOut) {
|
||||
console.log("reconnecting");
|
||||
await this.sleep(pause);
|
||||
pause *= 2;
|
||||
this.createConnection(bot, server, options);
|
||||
|
||||
socket.ev.process(
|
||||
async (events) => {
|
||||
if (events['connection.update']) {
|
||||
const update = events['connection.update']
|
||||
const { connection: connectionState, lastDisconnect, qr, isNewLogin } = update
|
||||
if (qr) {
|
||||
console.log('got qr code')
|
||||
await this.server.db().whatsappBots.updateQR(bot, qr);
|
||||
} else if (isNewLogin) {
|
||||
console.log("got new login")
|
||||
await this.server.db().whatsappBots.updateVerified(bot, true);
|
||||
} else if (connectionState === 'open') {
|
||||
console.log('opened connection')
|
||||
} else if (connectionState === "close") {
|
||||
console.log('connection closed due to ', lastDisconnect.error)
|
||||
const disconnectStatusCode = (lastDisconnect?.error as any)?.output?.statusCode
|
||||
|
||||
if (disconnectStatusCode === DisconnectReason.restartRequired) {
|
||||
console.log('reconnecting after got new login')
|
||||
const updatedBot = await this.findById(bot.id);
|
||||
await this.createConnection(updatedBot, server, options)
|
||||
authCompleteCallback?.()
|
||||
} else if (disconnectStatusCode !== DisconnectReason.loggedOut) {
|
||||
console.log('reconnecting')
|
||||
await this.sleep(pause)
|
||||
pause *= 2
|
||||
this.createConnection(bot, server, options)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (events['creds.update']) {
|
||||
console.log("creds update")
|
||||
await saveCreds()
|
||||
}
|
||||
|
||||
if (events['messages.upsert']) {
|
||||
console.log("messages upsert")
|
||||
const upsert = events['messages.upsert']
|
||||
const { messages } = upsert
|
||||
if (messages) {
|
||||
await this.queueUnreadMessages(bot, messages);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
)
|
||||
|
||||
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})`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
connection.ev.on("messages.upsert", async (m) => {
|
||||
console.log("messages upsert");
|
||||
const { messages } = m;
|
||||
if (messages) {
|
||||
await this.queueUnreadMessages(bot, messages);
|
||||
}
|
||||
});
|
||||
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);
|
||||
|
||||
this.connections[bot.id] = connection;
|
||||
this.connections[bot.id] = { socket, msgRetryCounterMap };
|
||||
}
|
||||
|
||||
private async updateConnections() {
|
||||
|
|
@ -142,66 +120,69 @@ export default class WhatsappService extends Service {
|
|||
const bots = await this.server.db().whatsappBots.findAll();
|
||||
for await (const bot of bots) {
|
||||
if (bot.isVerified) {
|
||||
this.createConnection(bot, this.server, {
|
||||
browser: WhatsappService.browserDescription,
|
||||
printQRInTerminal: false,
|
||||
version: [2, 2204, 13],
|
||||
});
|
||||
const { version, isLatest } = await fetchLatestBaileysVersion()
|
||||
console.log(`using WA v${version.join('.')}, isLatest: ${isLatest}`)
|
||||
|
||||
await this.createConnection(
|
||||
bot,
|
||||
this.server,
|
||||
{
|
||||
browser: WhatsappService.browserDescription,
|
||||
printQRInTerminal: false,
|
||||
version
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async queueMessage(bot: Bot, webMessageInfo: proto.IWebMessageInfo) {
|
||||
const { key, message, messageTimestamp } = webMessageInfo;
|
||||
const { remoteJid } = key;
|
||||
private async queueMessage(bot: Bot, webMessageInfo: proto.WebMessageInfo) {
|
||||
const { key: { id, fromMe }, message, messageTimestamp } = webMessageInfo;
|
||||
|
||||
const {fromMe, id: keyId} = key;
|
||||
|
||||
if (!fromMe && message && remoteJid !== "status@broadcast") {
|
||||
const { audioMessage, documentMessage, imageMessage, videoMessage } =
|
||||
message;
|
||||
if (!fromMe && message) {
|
||||
const isMediaMessage =
|
||||
audioMessage || documentMessage || imageMessage || videoMessage;
|
||||
message.audioMessage ||
|
||||
message.documentMessage ||
|
||||
message.imageMessage ||
|
||||
message.videoMessage;
|
||||
|
||||
const messageContent = Object.values(message)[0];
|
||||
const messageContent = Object.values(message)[0]
|
||||
let messageType: MediaType;
|
||||
let attachment: string;
|
||||
let filename: string;
|
||||
let mimetype: string;
|
||||
if (isMediaMessage) {
|
||||
if (audioMessage) {
|
||||
if (message.audioMessage) {
|
||||
messageType = "audio";
|
||||
filename = keyId + "." + audioMessage.mimetype.split("/").pop();
|
||||
mimetype = audioMessage.mimetype;
|
||||
} else if (documentMessage) {
|
||||
filename =
|
||||
id + "." + message.audioMessage.mimetype.split("/").pop();
|
||||
mimetype = message.audioMessage.mimetype;
|
||||
} else if (message.documentMessage) {
|
||||
messageType = "document";
|
||||
filename = documentMessage.fileName;
|
||||
mimetype = documentMessage.mimetype;
|
||||
} else if (imageMessage) {
|
||||
filename = message.documentMessage.fileName;
|
||||
mimetype = message.documentMessage.mimetype;
|
||||
} else if (message.imageMessage) {
|
||||
messageType = "image";
|
||||
filename = keyId + "." + imageMessage.mimetype.split("/").pop();
|
||||
mimetype = imageMessage.mimetype;
|
||||
} else if (videoMessage) {
|
||||
messageType = "video";
|
||||
filename = keyId + "." + videoMessage.mimetype.split("/").pop();
|
||||
mimetype = videoMessage.mimetype;
|
||||
filename =
|
||||
id + "." + message.imageMessage.mimetype.split("/").pop();
|
||||
mimetype = message.imageMessage.mimetype;
|
||||
} else if (message.videoMessage) {
|
||||
messageType = "video"
|
||||
filename =
|
||||
id + "." + message.videoMessage.mimetype.split("/").pop();
|
||||
mimetype = message.videoMessage.mimetype;
|
||||
}
|
||||
|
||||
const stream = await downloadContentFromMessage(
|
||||
messageContent,
|
||||
messageType
|
||||
);
|
||||
let buffer = Buffer.from([]);
|
||||
const stream = await downloadContentFromMessage(messageContent, messageType)
|
||||
let buffer = Buffer.from([])
|
||||
for await (const chunk of stream) {
|
||||
buffer = Buffer.concat([buffer, chunk]);
|
||||
buffer = Buffer.concat([buffer, chunk])
|
||||
}
|
||||
|
||||
attachment = buffer.toString("base64");
|
||||
}
|
||||
|
||||
if (messageContent || attachment) {
|
||||
const receivedMessage = {
|
||||
waMessageId: keyId,
|
||||
waMessageId: id,
|
||||
waMessage: JSON.stringify(webMessageInfo),
|
||||
waTimestamp: new Date((messageTimestamp as number) * 1000),
|
||||
attachment,
|
||||
|
|
@ -212,13 +193,13 @@ export default class WhatsappService extends Service {
|
|||
};
|
||||
|
||||
workerUtils.addJob("whatsapp-message", receivedMessage, {
|
||||
jobKey: keyId,
|
||||
jobKey: id,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async queueUnreadMessages(bot: Bot, messages: proto.IWebMessageInfo[]) {
|
||||
private async queueUnreadMessages(bot: Bot, messages: any[]) {
|
||||
for await (const message of messages) {
|
||||
await this.queueMessage(bot, message);
|
||||
}
|
||||
|
|
@ -239,6 +220,18 @@ export default class WhatsappService extends Service {
|
|||
return row;
|
||||
}
|
||||
|
||||
async unverify(bot: Bot): Promise<Bot> {
|
||||
const directory = this.getAuthDirectory(bot);
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
return this.server.db().whatsappBots.updateVerified(bot, false);
|
||||
}
|
||||
|
||||
async remove(bot: Bot): Promise<number> {
|
||||
const directory = this.getAuthDirectory(bot);
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
return this.server.db().whatsappBots.remove(bot);
|
||||
}
|
||||
|
||||
async findAll(): Promise<Bot[]> {
|
||||
return this.server.db().whatsappBots.findAll();
|
||||
}
|
||||
|
|
@ -252,22 +245,18 @@ export default class WhatsappService extends Service {
|
|||
}
|
||||
|
||||
async register(bot: Bot, callback: AuthCompleteCallback): Promise<void> {
|
||||
await this.createConnection(
|
||||
bot,
|
||||
this.server,
|
||||
{ version: [2, 2204, 13] },
|
||||
callback
|
||||
);
|
||||
const { version } = await fetchLatestBaileysVersion()
|
||||
await this.createConnection(bot, this.server, { version }, callback);
|
||||
}
|
||||
|
||||
async send(bot: Bot, phoneNumber: string, message: string): Promise<void> {
|
||||
const connection = this.connections[bot.id];
|
||||
const connection = this.connections[bot.id]?.socket;
|
||||
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 connection = this.connections[bot.id]?.socket;
|
||||
const messages = await connection.messagesReceivedAfter(
|
||||
lastReceivedDate,
|
||||
false
|
||||
|
|
@ -277,12 +266,8 @@ export default class WhatsappService extends Service {
|
|||
}
|
||||
}
|
||||
|
||||
async receive(bot: Bot, _lastReceivedDate: Date): Promise<proto.IWebMessageInfo[]> {
|
||||
const connection = this.connections[bot.id];
|
||||
// const messages = await connection.messagesReceivedAfter(
|
||||
// lastReceivedDate,
|
||||
// false
|
||||
// );
|
||||
async receive(bot: Bot, lastReceivedDate: Date): Promise<any> {
|
||||
const connection = this.connections[bot.id]?.socket;
|
||||
|
||||
const messages = await connection.loadAllUnreadMessages();
|
||||
return messages;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue