WhatsApp/Signal/Formstack/admin updates

This commit is contained in:
Darren Clarke 2025-11-21 14:55:28 +01:00
parent bcecf61a46
commit d0cc5a21de
451 changed files with 16139 additions and 39623 deletions

View file

@ -1,4 +1,9 @@
import { db, getWorkerUtils } from "@link-stack/bridge-common";
import { createLogger } from "@link-stack/logger";
import * as signalApi from "@link-stack/signal-api";
const { Configuration, GroupsApi } = signalApi;
const logger = createLogger('bridge-worker-receive-signal-message');
interface ReceiveSignalMessageTaskOptions {
token: string;
@ -10,6 +15,7 @@ interface ReceiveSignalMessageTaskOptions {
attachment?: string;
filename?: string;
mimeType?: string;
isGroup?: boolean;
}
const receiveSignalMessageTask = async ({
@ -22,8 +28,17 @@ const receiveSignalMessageTask = async ({
attachment,
filename,
mimeType,
isGroup,
}: ReceiveSignalMessageTaskOptions): Promise<void> => {
console.log({ token, to, from });
logger.debug({
messageId,
from,
to,
isGroup,
hasMessage: !!message,
hasAttachment: !!attachment,
token,
}, 'Processing incoming message');
const worker = await getWorkerUtils();
const row = await db
.selectFrom("SignalBot")
@ -32,8 +47,170 @@ const receiveSignalMessageTask = async ({
.executeTakeFirstOrThrow();
const backendId = row.id;
let finalTo = to;
let createdInternalId: string | undefined;
// Check if auto-group creation is enabled and this is NOT already a group message
const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true";
logger.debug({
enableAutoGroups,
isGroup,
shouldCreateGroup: enableAutoGroups && !isGroup && from && to,
}, 'Auto-groups config');
// If this is already a group message and auto-groups is enabled,
// use group provided in 'to'
if (enableAutoGroups && isGroup && to) {
// Signal sends the internal ID (base64) in group messages
// We should NOT add "group." prefix - that's for sending messages, not receiving
logger.debug('Message is from existing group with internal ID');
finalTo = to;
} else if (enableAutoGroups && !isGroup && from && to) {
try {
const config = new Configuration({
basePath: process.env.BRIDGE_SIGNAL_URL,
});
const groupsClient = new GroupsApi(config);
// Always create a new group for direct messages to the helpdesk
// This ensures each conversation gets its own group/ticket
logger.info({ from }, 'Creating new group for user');
// Include timestamp to make each group unique
const timestamp = new Date()
.toISOString()
.replace(/[:.]/g, "-")
.substring(0, 19);
const groupName = `Support: ${from} (${timestamp})`;
// Create new group for this conversation
const createGroupResponse = await groupsClient.v1GroupsNumberPost({
number: row.phoneNumber,
data: {
name: groupName,
members: [from],
description: "Private support conversation",
},
});
logger.debug({ createGroupResponse }, 'Group creation response from Signal API');
if (createGroupResponse.id) {
// The createGroupResponse.id already contains the full group identifier (group.BASE64)
finalTo = createGroupResponse.id;
// Fetch the group details to get the actual internalId
// The base64 part of the ID is NOT the same as the internalId!
try {
logger.debug('Fetching group details to get internalId');
const groups = await groupsClient.v1GroupsNumberGet({
number: row.phoneNumber,
});
logger.debug({ groupsSample: groups.slice(0, 3) }, 'Groups for bot');
const createdGroup = groups.find((g) => g.id === finalTo);
if (createdGroup) {
logger.debug({ createdGroup }, 'Found created group details');
}
if (createdGroup && createdGroup.internalId) {
createdInternalId = createdGroup.internalId;
logger.debug({ createdInternalId }, 'Got actual internalId');
} else {
// Fallback: extract base64 part from ID
if (finalTo.startsWith("group.")) {
createdInternalId = finalTo.substring(6);
}
}
} catch (fetchError) {
logger.debug('Could not fetch group details, using ID base64 part');
// Fallback: extract base64 part from ID
if (finalTo.startsWith("group.")) {
createdInternalId = finalTo.substring(6);
}
}
logger.debug({
fullGroupId: finalTo,
internalId: createdInternalId,
}, 'Group created successfully');
logger.debug({
groupId: finalTo,
internalId: createdInternalId,
groupName,
forPhoneNumber: from,
botNumber: row.phoneNumber,
response: createGroupResponse,
}, 'Created new Signal group');
}
// Now handle notifications and message forwarding for both new and existing groups
if (finalTo && finalTo.startsWith("group.")) {
// Forward the user's initial message to the group using quote feature
try {
logger.debug('Forwarding initial message to group using quote feature');
const attributionMessage = `Message from ${from}:\n"${message}"\n\n---\nSupport team: Your request has been received. An agent will respond shortly.`;
await worker.addJob("signal/send-signal-message", {
token: row.token,
to: finalTo,
message: attributionMessage,
conversationId: null,
quoteMessage: message,
quoteAuthor: from,
quoteTimestamp: Date.parse(sentAt),
});
logger.debug({ finalTo }, 'Successfully forwarded initial message to group');
} catch (forwardError) {
logger.error({ error: forwardError }, 'Error forwarding message to group');
}
// Send a response to the original DM informing about the group
try {
logger.debug('Sending group notification to original DM');
const dmNotification = `Hello! A private support group has been created for your conversation.\n\nGroup name: ${groupName}\n\nPlease look for the new group in your Signal app to continue the conversation. Our support team will respond there shortly.\n\nThank you for contacting support!`;
await worker.addJob("signal/send-signal-message", {
token: row.token,
to: from,
message: dmNotification,
conversationId: null,
});
logger.debug('Successfully sent group notification to user DM');
} catch (dmError) {
logger.error({ error: dmError }, 'Error sending DM notification');
}
}
} catch (error: any) {
// Check if error is because group already exists
const errorMessage =
error?.response?.data?.error || error?.message || error;
const isAlreadyExists =
errorMessage?.toString().toLowerCase().includes("already") ||
errorMessage?.toString().toLowerCase().includes("exists");
if (isAlreadyExists) {
logger.debug({ from }, 'Group might already exist, continuing with original recipient');
} else {
logger.error({
error: errorMessage,
from,
to,
botNumber: row.phoneNumber,
}, 'Error creating Signal group');
}
}
}
const payload = {
to,
to: finalTo,
from,
message_id: messageId,
sent_at: sentAt,
@ -41,6 +218,7 @@ const receiveSignalMessageTask = async ({
attachment,
filename,
mime_type: mimeType,
is_group: finalTo.startsWith("group"),
};
await worker.addJob("common/notify-webhooks", { backendId, payload });

View file

@ -1,19 +1,51 @@
import { db } from "@link-stack/bridge-common";
import {
db,
getWorkerUtils,
getMaxAttachmentSize,
getMaxTotalAttachmentSize,
MAX_ATTACHMENTS,
buildSignalGroupName,
} from "@link-stack/bridge-common";
import { createLogger } from "@link-stack/logger";
import * as signalApi from "@link-stack/signal-api";
const { Configuration, MessagesApi } = signalApi;
const { Configuration, MessagesApi, GroupsApi } = signalApi;
const logger = createLogger("bridge-worker-send-signal-message");
interface SendSignalMessageTaskOptions {
token: string;
to: string;
message: any;
conversationId?: string; // Zammad ticket/conversation ID for callback
quoteMessage?: string; // Optional: message text to quote
quoteAuthor?: string; // Optional: author of quoted message (phone number)
quoteTimestamp?: number; // Optional: timestamp of quoted message in milliseconds
attachments?: Array<{
data: string; // base64
filename: string;
mime_type: string;
}>;
}
const sendSignalMessageTask = async ({
token,
to,
message,
conversationId,
quoteMessage,
quoteAuthor,
quoteTimestamp,
attachments,
}: SendSignalMessageTaskOptions): Promise<void> => {
console.log({ token, to });
logger.debug(
{
token,
to,
conversationId,
messageLength: message?.length,
},
"Processing outgoing message",
);
const bot = await db
.selectFrom("SignalBot")
.selectAll()
@ -25,18 +57,255 @@ const sendSignalMessageTask = async ({
basePath: process.env.BRIDGE_SIGNAL_URL,
});
const messagesClient = new MessagesApi(config);
const groupsClient = new GroupsApi(config);
const worker = await getWorkerUtils();
let finalTo = to;
let groupCreated = false;
try {
const response = await messagesClient.v2SendPost({
data: {
number,
recipients: [to],
message,
// Check if 'to' is a group ID (UUID format, group.base64 format, or base64) vs phone number
const isUUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
to,
);
const isGroupPrefix = to.startsWith("group.");
const isBase64 = /^[A-Za-z0-9+/]+=*$/.test(to) && to.length > 20; // Base64 internal_id
const isGroupId = isUUID || isGroupPrefix || isBase64;
const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true";
logger.debug(
{
to,
isGroupId,
enableAutoGroups,
shouldCreateGroup: enableAutoGroups && !isGroupId && to && conversationId,
},
"Recipient analysis",
);
// If sending to a phone number and auto-groups is enabled, create a group first
if (enableAutoGroups && !isGroupId && to && conversationId) {
try {
const groupName = buildSignalGroupName(conversationId);
const createGroupResponse = await groupsClient.v1GroupsNumberPost({
number: bot.phoneNumber,
data: {
name: groupName,
members: [to],
description: "Private support conversation",
},
});
if (createGroupResponse.id) {
// The createGroupResponse.id already contains the full group identifier (group.BASE64)
finalTo = createGroupResponse.id;
groupCreated = true;
// Fetch the group details to get the actual internalId
let internalId: string | undefined;
try {
const groups = await groupsClient.v1GroupsNumberGet({
number: bot.phoneNumber,
});
const createdGroup = groups.find((g) => g.id === finalTo);
if (createdGroup && createdGroup.internalId) {
internalId = createdGroup.internalId;
logger.debug({ internalId }, "Got actual internalId");
} else {
// Fallback: extract base64 part from ID
if (finalTo.startsWith("group.")) {
internalId = finalTo.substring(6);
}
}
} catch (fetchError) {
logger.debug("Could not fetch group details, using ID base64 part");
// Fallback: extract base64 part from ID
if (finalTo.startsWith("group.")) {
internalId = finalTo.substring(6);
}
}
logger.debug(
{
groupId: finalTo,
internalId,
groupName,
conversationId,
originalRecipient: to,
botNumber: bot.phoneNumber,
},
"Created new Signal group",
);
// Notify Zammad about the new group ID via webhook
// Set group_joined: false initially - will be updated when user accepts invitation
await worker.addJob("common/notify-webhooks", {
backendId: bot.id,
payload: {
event: "group_created",
conversation_id: conversationId,
original_recipient: to,
group_id: finalTo,
internal_group_id: internalId,
group_joined: false,
timestamp: new Date().toISOString(),
},
});
}
} catch (groupError) {
logger.error(
{
error: groupError instanceof Error ? groupError.message : groupError,
to,
conversationId,
},
"Error creating Signal group",
);
// Continue with original recipient if group creation fails
}
}
logger.debug(
{
fromNumber: number,
toRecipient: finalTo,
originalTo: to,
recipientChanged: to !== finalTo,
groupCreated,
isGroupRecipient: finalTo.startsWith("group."),
},
"Sending message via API",
);
// Build the message data with optional quote parameters
const messageData: signalApi.ApiSendMessageV2 = {
number,
recipients: [finalTo],
message,
};
logger.debug(
{
number,
recipients: [finalTo],
messageLength: message?.length,
hasQuoteParams: !!(quoteMessage && quoteAuthor && quoteTimestamp),
},
"Message data being sent",
);
// Add quote parameters if all are provided
if (quoteMessage && quoteAuthor && quoteTimestamp) {
messageData.quoteTimestamp = quoteTimestamp;
messageData.quoteAuthor = quoteAuthor;
messageData.quoteMessage = quoteMessage;
logger.debug(
{
quoteAuthor,
quoteMessageLength: quoteMessage?.length,
quoteTimestamp,
},
"Including quote in message",
);
}
// Add attachments if provided with size validation
if (attachments && attachments.length > 0) {
const MAX_ATTACHMENT_SIZE = getMaxAttachmentSize();
const MAX_TOTAL_SIZE = getMaxTotalAttachmentSize();
if (attachments.length > MAX_ATTACHMENTS) {
throw new Error(
`Too many attachments: ${attachments.length} (max ${MAX_ATTACHMENTS})`,
);
}
let totalSize = 0;
const validatedAttachments = [];
for (const attachment of attachments) {
// Calculate size from base64 string (rough estimate: length * 3/4)
const estimatedSize = (attachment.data.length * 3) / 4;
if (estimatedSize > MAX_ATTACHMENT_SIZE) {
logger.warn(
{
filename: attachment.filename,
size: estimatedSize,
maxSize: MAX_ATTACHMENT_SIZE,
},
"Attachment exceeds size limit, skipping",
);
continue;
}
totalSize += estimatedSize;
if (totalSize > MAX_TOTAL_SIZE) {
logger.warn(
{
totalSize,
maxTotalSize: MAX_TOTAL_SIZE,
},
"Total attachment size exceeds limit, skipping remaining",
);
break;
}
validatedAttachments.push(attachment.data);
}
if (validatedAttachments.length > 0) {
messageData.base64Attachments = validatedAttachments;
logger.debug(
{
attachmentCount: validatedAttachments.length,
attachmentNames: attachments
.slice(0, validatedAttachments.length)
.map((att) => att.filename),
totalSizeBytes: totalSize,
},
"Including attachments in message",
);
}
}
const response = await messagesClient.v2SendPost({
data: messageData,
});
console.log({ response });
} catch (error) {
console.error({ error });
logger.debug(
{
to: finalTo,
groupCreated,
response: response?.timestamp || "no timestamp",
},
"Message sent successfully",
);
} catch (error: any) {
// Try to get the actual error message from the response
if (error.response) {
try {
const errorBody = await error.response.text();
logger.error(
{
status: error.response.status,
statusText: error.response.statusText,
body: errorBody,
sentTo: finalTo,
messageDetails: {
fromNumber: number,
toRecipients: [finalTo],
hasQuote: !!quoteMessage,
},
},
"Signal API error",
);
} catch (e) {
logger.error("Could not parse error response");
}
}
logger.error({ error }, "Full error details");
throw error;
}
};