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:
Darren Clarke 2025-08-21 10:58:33 +02:00
parent 9139c8e8de
commit d2a3c71bcd
8 changed files with 255 additions and 85 deletions

View file

@ -17,6 +17,7 @@ const getService = (request: Hapi.Request): WhatsappService => {
interface MessageRequest { interface MessageRequest {
phoneNumber: string; phoneNumber: string;
message: string; message: string;
attachments?: Array<{ data: string; filename: string; mime_type: string }>;
} }
export const SendMessageRoute = withDefaults({ export const SendMessageRoute = withDefaults({
@ -26,10 +27,23 @@ export const SendMessageRoute = withDefaults({
description: "Send a message", description: "Send a message",
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) { async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
const { id } = request.params; const { id } = request.params;
const { phoneNumber, message } = request.payload as MessageRequest; const { phoneNumber, message, attachments } =
request.payload as MessageRequest;
const whatsappService = getService(request); const whatsappService = getService(request);
await whatsappService.send(id, phoneNumber, message as string); await whatsappService.send(
request.logger.info({ id }, "Sent a message at %s", new Date()); id,
phoneNumber,
message as string,
attachments,
);
request.logger.info(
{
id,
attachmentCount: attachments?.length || 0,
},
"Sent a message at %s",
new Date(),
);
return _h return _h
.response({ .response({

View file

@ -13,7 +13,7 @@ import makeWASocket, {
import fs from "fs"; import fs from "fs";
import { createLogger } from "@link-stack/logger"; import { createLogger } from "@link-stack/logger";
const logger = createLogger('bridge-whatsapp-service'); const logger = createLogger("bridge-whatsapp-service");
export type AuthCompleteCallback = (error?: string) => void; export type AuthCompleteCallback = (error?: string) => void;
@ -60,7 +60,7 @@ export default class WhatsappService extends Service {
try { try {
connection.end(null); connection.end(null);
} catch (error) { } catch (error) {
logger.error({ error }, 'Connection reset error'); logger.error({ error }, "Connection reset error");
} }
} }
this.connections = {}; this.connections = {};
@ -95,27 +95,27 @@ export default class WhatsappService extends Service {
isNewLogin, isNewLogin,
} = update; } = update;
if (qr) { if (qr) {
logger.info('got qr code'); logger.info("got qr code");
const botDirectory = this.getBotDirectory(botID); const botDirectory = this.getBotDirectory(botID);
const qrPath = `${botDirectory}/qr.txt`; const qrPath = `${botDirectory}/qr.txt`;
fs.writeFileSync(qrPath, qr, "utf8"); fs.writeFileSync(qrPath, qr, "utf8");
} else if (isNewLogin) { } else if (isNewLogin) {
logger.info('got new login'); logger.info("got new login");
const botDirectory = this.getBotDirectory(botID); const botDirectory = this.getBotDirectory(botID);
const verifiedFile = `${botDirectory}/verified`; const verifiedFile = `${botDirectory}/verified`;
fs.writeFileSync(verifiedFile, ""); fs.writeFileSync(verifiedFile, "");
} else if (connectionState === "open") { } else if (connectionState === "open") {
logger.info('opened connection'); logger.info("opened connection");
} else if (connectionState === "close") { } else if (connectionState === "close") {
logger.info({ lastDisconnect }, 'connection closed'); logger.info({ lastDisconnect }, "connection closed");
const disconnectStatusCode = (lastDisconnect?.error as any)?.output const disconnectStatusCode = (lastDisconnect?.error as any)?.output
?.statusCode; ?.statusCode;
if (disconnectStatusCode === DisconnectReason.restartRequired) { if (disconnectStatusCode === DisconnectReason.restartRequired) {
logger.info('reconnecting after got new login'); logger.info("reconnecting after got new login");
await this.createConnection(botID, server, options); await this.createConnection(botID, server, options);
authCompleteCallback?.(); authCompleteCallback?.();
} else if (disconnectStatusCode !== DisconnectReason.loggedOut) { } else if (disconnectStatusCode !== DisconnectReason.loggedOut) {
logger.info('reconnecting'); logger.info("reconnecting");
await this.sleep(pause); await this.sleep(pause);
pause *= 2; pause *= 2;
this.createConnection(botID, server, options); this.createConnection(botID, server, options);
@ -124,12 +124,12 @@ export default class WhatsappService extends Service {
} }
if (events["creds.update"]) { if (events["creds.update"]) {
logger.info('creds update'); logger.info("creds update");
await saveCreds(); await saveCreds();
} }
if (events["messages.upsert"]) { if (events["messages.upsert"]) {
logger.info('messages upsert'); logger.info("messages upsert");
const upsert = events["messages.upsert"]; const upsert = events["messages.upsert"];
const { messages } = upsert; const { messages } = upsert;
if (messages) { if (messages) {
@ -152,7 +152,10 @@ export default class WhatsappService extends Service {
const verifiedFile = `${directory}/verified`; const verifiedFile = `${directory}/verified`;
if (fs.existsSync(verifiedFile)) { if (fs.existsSync(verifiedFile)) {
const { version, isLatest } = await fetchLatestBaileysVersion(); 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, { await this.createConnection(botID, this.server, {
browser: WhatsappService.browserDescription, browser: WhatsappService.browserDescription,
@ -172,9 +175,12 @@ export default class WhatsappService extends Service {
message, message,
messageTimestamp, messageTimestamp,
} = webMessageInfo; } = webMessageInfo;
logger.info('Message type debug'); logger.info("Message type debug");
for (const key in message) { 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 = const isValidMessage =
message && remoteJid !== "status@broadcast" && !fromMe; message && remoteJid !== "status@broadcast" && !fromMe;
@ -299,10 +305,45 @@ export default class WhatsappService extends Service {
botID: string, botID: string,
phoneNumber: string, phoneNumber: string,
message: string, message: string,
attachments?: Array<{ data: string; filename: string; mime_type: string }>,
): Promise<void> { ): Promise<void> {
const connection = this.connections[botID]?.socket; const connection = this.connections[botID]?.socket;
const recipient = `${phoneNumber.replace(/\D+/g, "")}@s.whatsapp.net`; 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( async receive(

View file

@ -3,7 +3,7 @@ import { createLogger } from "@link-stack/logger";
import * as signalApi from "@link-stack/signal-api"; import * as signalApi from "@link-stack/signal-api";
const { Configuration, MessagesApi, GroupsApi } = signalApi; const { Configuration, MessagesApi, GroupsApi } = signalApi;
const logger = createLogger('bridge-worker-send-signal-message'); const logger = createLogger("bridge-worker-send-signal-message");
interface SendSignalMessageTaskOptions { interface SendSignalMessageTaskOptions {
token: string; token: string;
@ -13,6 +13,11 @@ interface SendSignalMessageTaskOptions {
quoteMessage?: string; // Optional: message text to quote quoteMessage?: string; // Optional: message text to quote
quoteAuthor?: string; // Optional: author of quoted message (phone number) quoteAuthor?: string; // Optional: author of quoted message (phone number)
quoteTimestamp?: number; // Optional: timestamp of quoted message in milliseconds quoteTimestamp?: number; // Optional: timestamp of quoted message in milliseconds
attachments?: Array<{
data: string; // base64
filename: string;
mime_type: string;
}>;
} }
const sendSignalMessageTask = async ({ const sendSignalMessageTask = async ({
@ -23,13 +28,17 @@ const sendSignalMessageTask = async ({
quoteMessage, quoteMessage,
quoteAuthor, quoteAuthor,
quoteTimestamp, quoteTimestamp,
attachments,
}: SendSignalMessageTaskOptions): Promise<void> => { }: SendSignalMessageTaskOptions): Promise<void> => {
logger.debug({ logger.debug(
token, {
to, token,
conversationId, to,
messageLength: message?.length, conversationId,
}, 'Processing outgoing message'); messageLength: message?.length,
},
"Processing outgoing message",
);
const bot = await db const bot = await db
.selectFrom("SignalBot") .selectFrom("SignalBot")
.selectAll() .selectAll()
@ -58,12 +67,16 @@ const sendSignalMessageTask = async ({
const isGroupId = isUUID || isGroupPrefix || isBase64; const isGroupId = isUUID || isGroupPrefix || isBase64;
const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true"; const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true";
logger.debug({ logger.debug(
to, {
isGroupId, to,
enableAutoGroups, isGroupId,
shouldCreateGroup: enableAutoGroups && !isGroupId && to && conversationId, enableAutoGroups,
}, 'Recipient analysis'); shouldCreateGroup:
enableAutoGroups && !isGroupId && to && conversationId,
},
"Recipient analysis",
);
// If sending to a phone number and auto-groups is enabled, create a group first // If sending to a phone number and auto-groups is enabled, create a group first
if (enableAutoGroups && !isGroupId && to && conversationId) { if (enableAutoGroups && !isGroupId && to && conversationId) {
@ -93,7 +106,7 @@ const sendSignalMessageTask = async ({
const createdGroup = groups.find((g) => g.id === finalTo); const createdGroup = groups.find((g) => g.id === finalTo);
if (createdGroup && createdGroup.internalId) { if (createdGroup && createdGroup.internalId) {
internalId = createdGroup.internalId; internalId = createdGroup.internalId;
logger.debug({ internalId }, 'Got actual internalId'); logger.debug({ internalId }, "Got actual internalId");
} else { } else {
// Fallback: extract base64 part from ID // Fallback: extract base64 part from ID
if (finalTo.startsWith("group.")) { if (finalTo.startsWith("group.")) {
@ -101,20 +114,23 @@ const sendSignalMessageTask = async ({
} }
} }
} catch (fetchError) { } catch (fetchError) {
logger.debug('Could not fetch group details, using ID base64 part'); logger.debug("Could not fetch group details, using ID base64 part");
// Fallback: extract base64 part from ID // Fallback: extract base64 part from ID
if (finalTo.startsWith("group.")) { if (finalTo.startsWith("group.")) {
internalId = finalTo.substring(6); internalId = finalTo.substring(6);
} }
} }
logger.debug({ logger.debug(
groupId: finalTo, {
internalId, groupId: finalTo,
groupName, internalId,
conversationId, groupName,
originalRecipient: to, conversationId,
botNumber: bot.phoneNumber, originalRecipient: to,
}, 'Created new Signal group'); botNumber: bot.phoneNumber,
},
"Created new Signal group",
);
// Notify Zammad about the new group ID via webhook // Notify Zammad about the new group ID via webhook
await worker.addJob("common/notify-webhooks", { await worker.addJob("common/notify-webhooks", {
@ -130,23 +146,30 @@ const sendSignalMessageTask = async ({
}); });
} }
} catch (groupError) { } catch (groupError) {
logger.error({ logger.error(
error: groupError instanceof Error ? groupError.message : groupError, {
to, error:
conversationId, groupError instanceof Error ? groupError.message : groupError,
}, 'Error creating Signal group'); to,
conversationId,
},
"Error creating Signal group",
);
// Continue with original recipient if group creation fails // Continue with original recipient if group creation fails
} }
} }
logger.debug({ logger.debug(
fromNumber: number, {
toRecipient: finalTo, fromNumber: number,
originalTo: to, toRecipient: finalTo,
recipientChanged: to !== finalTo, originalTo: to,
groupCreated, recipientChanged: to !== finalTo,
isGroupRecipient: finalTo.startsWith("group."), groupCreated,
}, 'Sending message via API'); isGroupRecipient: finalTo.startsWith("group."),
},
"Sending message via API",
);
// Build the message data with optional quote parameters // Build the message data with optional quote parameters
const messageData: signalApi.ApiSendMessageV2 = { const messageData: signalApi.ApiSendMessageV2 = {
@ -155,12 +178,15 @@ const sendSignalMessageTask = async ({
message, message,
}; };
logger.debug({ logger.debug(
number, {
recipients: [finalTo], number,
message: message.substring(0, 50) + "...", recipients: [finalTo],
hasQuoteParams: !!(quoteMessage && quoteAuthor && quoteTimestamp), message: message.substring(0, 50) + "...",
}, 'Message data being sent'); hasQuoteParams: !!(quoteMessage && quoteAuthor && quoteTimestamp),
},
"Message data being sent",
);
// Add quote parameters if all are provided // Add quote parameters if all are provided
if (quoteMessage && quoteAuthor && quoteTimestamp) { if (quoteMessage && quoteAuthor && quoteTimestamp) {
@ -168,43 +194,64 @@ const sendSignalMessageTask = async ({
messageData.quoteAuthor = quoteAuthor; messageData.quoteAuthor = quoteAuthor;
messageData.quoteMessage = quoteMessage; messageData.quoteMessage = quoteMessage;
logger.debug({ logger.debug(
quoteAuthor, {
quoteMessage: quoteMessage.substring(0, 50) + "...", quoteAuthor,
quoteTimestamp, quoteMessage: quoteMessage.substring(0, 50) + "...",
}, 'Including quote in message'); quoteTimestamp,
},
"Including quote in message",
);
}
// Add attachments if provided
if (attachments && attachments.length > 0) {
messageData.base64Attachments = attachments.map((att) => att.data);
logger.debug(
{
attachmentCount: attachments.length,
attachmentNames: attachments.map((att) => att.filename),
},
"Including attachments in message",
);
} }
const response = await messagesClient.v2SendPost({ const response = await messagesClient.v2SendPost({
data: messageData, data: messageData,
}); });
logger.debug({ logger.debug(
to: finalTo, {
groupCreated, to: finalTo,
response: response?.timestamp || "no timestamp", groupCreated,
}, 'Message sent successfully'); response: response?.timestamp || "no timestamp",
},
"Message sent successfully",
);
} catch (error: any) { } catch (error: any) {
// Try to get the actual error message from the response // Try to get the actual error message from the response
if (error.response) { if (error.response) {
try { try {
const errorBody = await error.response.text(); const errorBody = await error.response.text();
logger.error({ logger.error(
status: error.response.status, {
statusText: error.response.statusText, status: error.response.status,
body: errorBody, statusText: error.response.statusText,
sentTo: finalTo, body: errorBody,
messageDetails: { sentTo: finalTo,
fromNumber: number, messageDetails: {
toRecipients: [finalTo], fromNumber: number,
hasQuote: !!quoteMessage, toRecipients: [finalTo],
hasQuote: !!quoteMessage,
},
}, },
}, 'Signal API error'); "Signal API error",
);
} catch (e) { } catch (e) {
logger.error('Could not parse error response'); logger.error("Could not parse error response");
} }
} }
logger.error({ error }, 'Full error details'); logger.error({ error }, "Full error details");
throw error; throw error;
} }
}; };

View file

@ -1,18 +1,24 @@
import { db } from "@link-stack/bridge-common"; import { db } from "@link-stack/bridge-common";
import { createLogger } from "@link-stack/logger"; import { createLogger } from "@link-stack/logger";
const logger = createLogger('bridge-worker-send-whatsapp-message'); const logger = createLogger("bridge-worker-send-whatsapp-message");
interface SendWhatsappMessageTaskOptions { interface SendWhatsappMessageTaskOptions {
token: string; token: string;
to: string; to: string;
message: any; message: any;
attachments?: Array<{
data: string;
filename: string;
mime_type: string;
}>;
} }
const sendWhatsappMessageTask = async ({ const sendWhatsappMessageTask = async ({
message, message,
to, to,
token, token,
attachments,
}: SendWhatsappMessageTaskOptions): Promise<void> => { }: SendWhatsappMessageTaskOptions): Promise<void> => {
const bot = await db const bot = await db
.selectFrom("WhatsappBot") .selectFrom("WhatsappBot")
@ -21,13 +27,38 @@ const sendWhatsappMessageTask = async ({
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow();
const url = `${process.env.BRIDGE_WHATSAPP_URL}/api/bots/${bot.id}/send`; const url = `${process.env.BRIDGE_WHATSAPP_URL}/api/bots/${bot.id}/send`;
const params = { message, phoneNumber: to }; const params: any = { message, phoneNumber: to };
if (attachments && attachments.length > 0) {
params.attachments = attachments;
logger.debug(
{
attachmentCount: attachments.length,
attachmentNames: attachments.map((att) => att.filename),
},
"Sending WhatsApp message with attachments",
);
}
try { try {
const result = await fetch(url, { const result = await fetch(url, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify(params), body: JSON.stringify(params),
}); });
if (!result.ok) {
const errorText = await result.text();
logger.error(
{
status: result.status,
errorText,
url,
},
"WhatsApp send failed",
);
throw new Error(`Failed to send message: ${result.status}`);
}
} catch (error) { } catch (error) {
logger.error({ error }); logger.error({ error });
throw new Error("Failed to send message"); throw new Error("Failed to send message");

View file

@ -334,6 +334,20 @@ class CdrSignal
options = {} options = {}
options[:conversationId] = ticket.number if ticket options[:conversationId] = ticket.number if ticket
# Get attachments from the article
attachments = Store.list(object: 'Ticket::Article', o_id: article.id)
if attachments.any?
attachment_data = attachments.map do |attachment|
{
data: Base64.strict_encode64(attachment.content),
filename: attachment.filename,
mime_type: attachment.preferences['Mime-Type'] || attachment.preferences['Content-Type'] || 'application/octet-stream'
}
end
options[:attachments] = attachment_data
Rails.logger.debug { "Sending #{attachment_data.length} attachment(s) with message" }
end
@api.send_message(recipient, article[:body], options) @api.send_message(recipient, article[:body], options)
end end
end end

View file

@ -41,6 +41,10 @@ class CdrSignalApi
params[:conversationId] = options[:conversationId] params[:conversationId] = options[:conversationId]
options.delete(:conversationId) options.delete(:conversationId)
end end
if options[:attachments]
params[:attachments] = options[:attachments]
options.delete(:attachments)
end
post('send', params.merge(parse_hash(options))) post('send', params.merge(parse_hash(options)))
end end
end end

View file

@ -323,6 +323,22 @@ class CdrWhatsapp
Rails.logger.debug { "Sending to recipient: '#{recipient}'" } Rails.logger.debug { "Sending to recipient: '#{recipient}'" }
@api.send_message(recipient, article[:body]) options = {}
# Get attachments from the article
attachments = Store.list(object: 'Ticket::Article', o_id: article.id)
if attachments.any?
attachment_data = attachments.map do |attachment|
{
data: Base64.strict_encode64(attachment.content),
filename: attachment.filename,
mime_type: attachment.preferences['Mime-Type'] || attachment.preferences['Content-Type'] || 'application/octet-stream'
}
end
options[:attachments] = attachment_data
Rails.logger.debug { "Sending #{attachment_data.length} attachment(s) with message" }
end
@api.send_message(recipient, article[:body], options)
end end
end end

View file

@ -35,6 +35,9 @@ class CdrWhatsappApi
end end
def send_message(recipient, text, options = {}) def send_message(recipient, text, options = {})
post('send', { to: recipient.to_s, message: text }.merge(parse_hash(options))) params = { to: recipient.to_s, message: text }
params[:attachments] = options[:attachments] if options[:attachments]
options.delete(:attachments) if options[:attachments]
post('send', params.merge(parse_hash(options)))
end end
end end