feat: Add attachment support for Signal and WhatsApp channels
- Signal: Use base64Attachments field in signal-cli-rest-api - WhatsApp: Implement Baileys attachment sending for images, videos, audio, and documents - Both channels retrieve attachments from Zammad Store model - Support multiple attachments per message
This commit is contained in:
parent
9139c8e8de
commit
d2a3c71bcd
8 changed files with 255 additions and 85 deletions
|
|
@ -17,6 +17,7 @@ const getService = (request: Hapi.Request): WhatsappService => {
|
|||
interface MessageRequest {
|
||||
phoneNumber: string;
|
||||
message: string;
|
||||
attachments?: Array<{ data: string; filename: string; mime_type: string }>;
|
||||
}
|
||||
|
||||
export const SendMessageRoute = withDefaults({
|
||||
|
|
@ -26,10 +27,23 @@ export const SendMessageRoute = withDefaults({
|
|||
description: "Send a message",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { id } = request.params;
|
||||
const { phoneNumber, message } = request.payload as MessageRequest;
|
||||
const { phoneNumber, message, attachments } =
|
||||
request.payload as MessageRequest;
|
||||
const whatsappService = getService(request);
|
||||
await whatsappService.send(id, phoneNumber, message as string);
|
||||
request.logger.info({ id }, "Sent a message at %s", new Date());
|
||||
await whatsappService.send(
|
||||
id,
|
||||
phoneNumber,
|
||||
message as string,
|
||||
attachments,
|
||||
);
|
||||
request.logger.info(
|
||||
{
|
||||
id,
|
||||
attachmentCount: attachments?.length || 0,
|
||||
},
|
||||
"Sent a message at %s",
|
||||
new Date(),
|
||||
);
|
||||
|
||||
return _h
|
||||
.response({
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import makeWASocket, {
|
|||
import fs from "fs";
|
||||
import { createLogger } from "@link-stack/logger";
|
||||
|
||||
const logger = createLogger('bridge-whatsapp-service');
|
||||
const logger = createLogger("bridge-whatsapp-service");
|
||||
|
||||
export type AuthCompleteCallback = (error?: string) => void;
|
||||
|
||||
|
|
@ -60,7 +60,7 @@ export default class WhatsappService extends Service {
|
|||
try {
|
||||
connection.end(null);
|
||||
} catch (error) {
|
||||
logger.error({ error }, 'Connection reset error');
|
||||
logger.error({ error }, "Connection reset error");
|
||||
}
|
||||
}
|
||||
this.connections = {};
|
||||
|
|
@ -95,27 +95,27 @@ export default class WhatsappService extends Service {
|
|||
isNewLogin,
|
||||
} = update;
|
||||
if (qr) {
|
||||
logger.info('got qr code');
|
||||
logger.info("got qr code");
|
||||
const botDirectory = this.getBotDirectory(botID);
|
||||
const qrPath = `${botDirectory}/qr.txt`;
|
||||
fs.writeFileSync(qrPath, qr, "utf8");
|
||||
} else if (isNewLogin) {
|
||||
logger.info('got new login');
|
||||
logger.info("got new login");
|
||||
const botDirectory = this.getBotDirectory(botID);
|
||||
const verifiedFile = `${botDirectory}/verified`;
|
||||
fs.writeFileSync(verifiedFile, "");
|
||||
} else if (connectionState === "open") {
|
||||
logger.info('opened connection');
|
||||
logger.info("opened connection");
|
||||
} else if (connectionState === "close") {
|
||||
logger.info({ lastDisconnect }, 'connection closed');
|
||||
logger.info({ lastDisconnect }, "connection closed");
|
||||
const disconnectStatusCode = (lastDisconnect?.error as any)?.output
|
||||
?.statusCode;
|
||||
if (disconnectStatusCode === DisconnectReason.restartRequired) {
|
||||
logger.info('reconnecting after got new login');
|
||||
logger.info("reconnecting after got new login");
|
||||
await this.createConnection(botID, server, options);
|
||||
authCompleteCallback?.();
|
||||
} else if (disconnectStatusCode !== DisconnectReason.loggedOut) {
|
||||
logger.info('reconnecting');
|
||||
logger.info("reconnecting");
|
||||
await this.sleep(pause);
|
||||
pause *= 2;
|
||||
this.createConnection(botID, server, options);
|
||||
|
|
@ -124,12 +124,12 @@ export default class WhatsappService extends Service {
|
|||
}
|
||||
|
||||
if (events["creds.update"]) {
|
||||
logger.info('creds update');
|
||||
logger.info("creds update");
|
||||
await saveCreds();
|
||||
}
|
||||
|
||||
if (events["messages.upsert"]) {
|
||||
logger.info('messages upsert');
|
||||
logger.info("messages upsert");
|
||||
const upsert = events["messages.upsert"];
|
||||
const { messages } = upsert;
|
||||
if (messages) {
|
||||
|
|
@ -152,7 +152,10 @@ export default class WhatsappService extends Service {
|
|||
const verifiedFile = `${directory}/verified`;
|
||||
if (fs.existsSync(verifiedFile)) {
|
||||
const { version, isLatest } = await fetchLatestBaileysVersion();
|
||||
logger.info({ version: version.join('.'), isLatest }, 'using WA version');
|
||||
logger.info(
|
||||
{ version: version.join("."), isLatest },
|
||||
"using WA version",
|
||||
);
|
||||
|
||||
await this.createConnection(botID, this.server, {
|
||||
browser: WhatsappService.browserDescription,
|
||||
|
|
@ -172,9 +175,12 @@ export default class WhatsappService extends Service {
|
|||
message,
|
||||
messageTimestamp,
|
||||
} = webMessageInfo;
|
||||
logger.info('Message type debug');
|
||||
logger.info("Message type debug");
|
||||
for (const key in message) {
|
||||
logger.info({ key, exists: !!message[key as keyof proto.IMessage] }, 'Message field');
|
||||
logger.info(
|
||||
{ key, exists: !!message[key as keyof proto.IMessage] },
|
||||
"Message field",
|
||||
);
|
||||
}
|
||||
const isValidMessage =
|
||||
message && remoteJid !== "status@broadcast" && !fromMe;
|
||||
|
|
@ -299,10 +305,45 @@ export default class WhatsappService extends Service {
|
|||
botID: string,
|
||||
phoneNumber: string,
|
||||
message: string,
|
||||
attachments?: Array<{ data: string; filename: string; mime_type: string }>,
|
||||
): Promise<void> {
|
||||
const connection = this.connections[botID]?.socket;
|
||||
const recipient = `${phoneNumber.replace(/\D+/g, "")}@s.whatsapp.net`;
|
||||
await connection.sendMessage(recipient, { text: message });
|
||||
|
||||
// Send text message if provided
|
||||
if (message) {
|
||||
await connection.sendMessage(recipient, { text: message });
|
||||
}
|
||||
|
||||
// Send attachments if provided
|
||||
if (attachments && attachments.length > 0) {
|
||||
for (const attachment of attachments) {
|
||||
const buffer = Buffer.from(attachment.data, "base64");
|
||||
|
||||
if (attachment.mime_type.startsWith("image/")) {
|
||||
await connection.sendMessage(recipient, {
|
||||
image: buffer,
|
||||
caption: attachment.filename,
|
||||
});
|
||||
} else if (attachment.mime_type.startsWith("video/")) {
|
||||
await connection.sendMessage(recipient, {
|
||||
video: buffer,
|
||||
caption: attachment.filename,
|
||||
});
|
||||
} else if (attachment.mime_type.startsWith("audio/")) {
|
||||
await connection.sendMessage(recipient, {
|
||||
audio: buffer,
|
||||
mimetype: attachment.mime_type,
|
||||
});
|
||||
} else {
|
||||
await connection.sendMessage(recipient, {
|
||||
document: buffer,
|
||||
fileName: attachment.filename,
|
||||
mimetype: attachment.mime_type,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async receive(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue