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
|
|
@ -1,18 +1,24 @@
|
|||
import { db } from "@link-stack/bridge-common";
|
||||
import { createLogger } from "@link-stack/logger";
|
||||
|
||||
const logger = createLogger('bridge-worker-send-whatsapp-message');
|
||||
const logger = createLogger("bridge-worker-send-whatsapp-message");
|
||||
|
||||
interface SendWhatsappMessageTaskOptions {
|
||||
token: string;
|
||||
to: string;
|
||||
message: any;
|
||||
attachments?: Array<{
|
||||
data: string;
|
||||
filename: string;
|
||||
mime_type: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
const sendWhatsappMessageTask = async ({
|
||||
message,
|
||||
to,
|
||||
token,
|
||||
attachments,
|
||||
}: SendWhatsappMessageTaskOptions): Promise<void> => {
|
||||
const bot = await db
|
||||
.selectFrom("WhatsappBot")
|
||||
|
|
@ -21,13 +27,38 @@ const sendWhatsappMessageTask = async ({
|
|||
.executeTakeFirstOrThrow();
|
||||
|
||||
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 {
|
||||
const result = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
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) {
|
||||
logger.error({ error });
|
||||
throw new Error("Failed to send message");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue