2025-06-10 14:02:21 +02:00
|
|
|
import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
2024-06-05 15:12:48 +02:00
|
|
|
import * as signalApi from "@link-stack/signal-api";
|
2025-06-10 14:02:21 +02:00
|
|
|
const { Configuration, MessagesApi, GroupsApi } = signalApi;
|
2024-04-30 13:13:49 +02:00
|
|
|
|
|
|
|
|
interface SendSignalMessageTaskOptions {
|
2024-06-05 15:12:48 +02:00
|
|
|
token: string;
|
2024-07-18 11:08:01 +02:00
|
|
|
to: string;
|
2024-04-30 13:13:49 +02:00
|
|
|
message: any;
|
2025-06-10 14:02:21 +02:00
|
|
|
conversationId?: string; // Zammad ticket/conversation ID for callback
|
2025-07-07 20:02:54 +02:00
|
|
|
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
|
2024-04-30 13:13:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sendSignalMessageTask = async ({
|
2024-06-05 15:12:48 +02:00
|
|
|
token,
|
2024-07-18 11:08:01 +02:00
|
|
|
to,
|
|
|
|
|
message,
|
2025-06-10 14:02:21 +02:00
|
|
|
conversationId,
|
2025-07-07 20:02:54 +02:00
|
|
|
quoteMessage,
|
|
|
|
|
quoteAuthor,
|
|
|
|
|
quoteTimestamp,
|
2024-06-05 15:12:48 +02:00
|
|
|
}: SendSignalMessageTaskOptions): Promise<void> => {
|
2025-07-07 20:02:54 +02:00
|
|
|
console.log(`[send-signal-message] Processing outgoing message:`, {
|
|
|
|
|
token,
|
|
|
|
|
to,
|
|
|
|
|
conversationId,
|
|
|
|
|
messageLength: message?.length,
|
|
|
|
|
});
|
2024-06-05 15:12:48 +02:00
|
|
|
const bot = await db
|
|
|
|
|
.selectFrom("SignalBot")
|
|
|
|
|
.selectAll()
|
|
|
|
|
.where("token", "=", token)
|
|
|
|
|
.executeTakeFirstOrThrow();
|
|
|
|
|
|
|
|
|
|
const { phoneNumber: number } = bot;
|
|
|
|
|
const config = new Configuration({
|
|
|
|
|
basePath: process.env.BRIDGE_SIGNAL_URL,
|
|
|
|
|
});
|
|
|
|
|
const messagesClient = new MessagesApi(config);
|
2025-06-10 14:02:21 +02:00
|
|
|
const groupsClient = new GroupsApi(config);
|
|
|
|
|
const worker = await getWorkerUtils();
|
|
|
|
|
|
|
|
|
|
let finalTo = to;
|
|
|
|
|
let groupCreated = false;
|
2024-06-05 15:12:48 +02:00
|
|
|
|
2024-07-18 11:08:01 +02:00
|
|
|
try {
|
2025-06-10 14:02:21 +02:00
|
|
|
// Check if 'to' is a group ID (UUID format) vs phone number
|
|
|
|
|
const isGroupId =
|
|
|
|
|
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
|
|
|
|
|
to,
|
|
|
|
|
);
|
|
|
|
|
const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true";
|
2025-07-07 20:02:54 +02:00
|
|
|
|
|
|
|
|
console.log(`[send-signal-message] Recipient analysis:`, {
|
|
|
|
|
to,
|
|
|
|
|
isGroupId,
|
|
|
|
|
enableAutoGroups,
|
|
|
|
|
shouldCreateGroup: enableAutoGroups && !isGroupId && to && conversationId,
|
|
|
|
|
});
|
2025-06-10 14:02:21 +02:00
|
|
|
|
|
|
|
|
// If sending to a phone number and auto-groups is enabled, create a group first
|
|
|
|
|
if (enableAutoGroups && !isGroupId && to && conversationId) {
|
|
|
|
|
try {
|
|
|
|
|
const groupName = `Support: ${to}`;
|
|
|
|
|
const createGroupResponse = await groupsClient.v1GroupsNumberPost({
|
|
|
|
|
number: bot.phoneNumber,
|
|
|
|
|
data: {
|
|
|
|
|
name: groupName,
|
|
|
|
|
members: [to],
|
|
|
|
|
description: "Private support conversation",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (createGroupResponse.id) {
|
2025-07-07 20:02:54 +02:00
|
|
|
// Get the internal_id for this group
|
|
|
|
|
// Sometimes the API needs a moment to return the new group, so retry if needed
|
|
|
|
|
let internalId: string | undefined;
|
|
|
|
|
let retries = 3;
|
|
|
|
|
|
|
|
|
|
while (retries > 0 && !internalId) {
|
|
|
|
|
try {
|
|
|
|
|
const groupsResponse = await groupsClient.v1GroupsNumberGet({
|
|
|
|
|
number: bot.phoneNumber,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Find the group we just created to get its internal_id
|
|
|
|
|
const createdGroup = groupsResponse.find(g => g.id === createGroupResponse.id);
|
|
|
|
|
internalId = createdGroup?.internal_id;
|
|
|
|
|
|
|
|
|
|
if (!internalId && retries > 1) {
|
|
|
|
|
console.log(`[send-signal-message] Group not found in list yet, retrying...`);
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`[send-signal-message] Error fetching groups:`, error);
|
|
|
|
|
}
|
|
|
|
|
retries--;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-10 14:02:21 +02:00
|
|
|
finalTo = createGroupResponse.id;
|
|
|
|
|
groupCreated = true;
|
|
|
|
|
console.log(
|
2025-07-07 20:02:54 +02:00
|
|
|
`[send-signal-message] Created new Signal group:`,
|
|
|
|
|
{
|
|
|
|
|
groupId: finalTo,
|
|
|
|
|
internalId,
|
|
|
|
|
groupName,
|
|
|
|
|
conversationId,
|
|
|
|
|
originalRecipient: to,
|
|
|
|
|
botNumber: bot.phoneNumber,
|
|
|
|
|
},
|
2025-06-10 14:02:21 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Notify Zammad about the new group ID via webhook
|
|
|
|
|
await worker.addJob("common/notify-webhooks", {
|
|
|
|
|
backendId: bot.id,
|
|
|
|
|
payload: {
|
|
|
|
|
event: "group_created",
|
|
|
|
|
conversation_id: conversationId,
|
|
|
|
|
original_recipient: to,
|
|
|
|
|
group_id: finalTo,
|
2025-07-07 20:02:54 +02:00
|
|
|
internal_group_id: internalId,
|
2025-06-10 14:02:21 +02:00
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (groupError) {
|
2025-07-07 20:02:54 +02:00
|
|
|
console.error("[send-signal-message] Error creating Signal group:", {
|
|
|
|
|
error: groupError instanceof Error ? groupError.message : groupError,
|
|
|
|
|
to,
|
|
|
|
|
conversationId,
|
|
|
|
|
});
|
2025-06-10 14:02:21 +02:00
|
|
|
// Continue with original recipient if group creation fails
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 20:02:54 +02:00
|
|
|
console.log(`[send-signal-message] Sending message via API:`, {
|
|
|
|
|
fromNumber: number,
|
|
|
|
|
toRecipient: finalTo,
|
|
|
|
|
originalTo: to,
|
|
|
|
|
recipientChanged: to !== finalTo,
|
|
|
|
|
groupCreated,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Build the message data with optional quote parameters
|
|
|
|
|
const messageData: signalApi.ApiSendMessageV2 = {
|
|
|
|
|
number,
|
|
|
|
|
recipients: [finalTo],
|
|
|
|
|
message,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Add quote parameters if all are provided
|
|
|
|
|
if (quoteMessage && quoteAuthor && quoteTimestamp) {
|
|
|
|
|
messageData.quoteTimestamp = quoteTimestamp;
|
|
|
|
|
messageData.quoteAuthor = quoteAuthor;
|
|
|
|
|
messageData.quoteMessage = quoteMessage;
|
|
|
|
|
|
|
|
|
|
console.log(`[send-signal-message] Including quote in message:`, {
|
|
|
|
|
quoteAuthor,
|
|
|
|
|
quoteMessage: quoteMessage.substring(0, 50) + '...',
|
|
|
|
|
quoteTimestamp,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-18 11:08:01 +02:00
|
|
|
const response = await messagesClient.v2SendPost({
|
2025-07-07 20:02:54 +02:00
|
|
|
data: messageData,
|
2024-07-18 11:08:01 +02:00
|
|
|
});
|
2025-06-10 14:02:21 +02:00
|
|
|
|
|
|
|
|
console.log(
|
2025-07-07 20:02:54 +02:00
|
|
|
`[send-signal-message] Message sent successfully:`,
|
|
|
|
|
{
|
|
|
|
|
to: finalTo,
|
|
|
|
|
groupCreated,
|
|
|
|
|
response: response?.timestamp || 'no timestamp',
|
|
|
|
|
},
|
2025-06-10 14:02:21 +02:00
|
|
|
);
|
2024-07-18 11:08:01 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error({ error });
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2024-06-05 15:12:48 +02:00
|
|
|
};
|
2024-04-30 13:13:49 +02:00
|
|
|
|
|
|
|
|
export default sendSignalMessageTask;
|