Add Signal auto-group creation
This commit is contained in:
parent
a83907b4be
commit
c8ccee7ada
7 changed files with 393 additions and 8 deletions
|
|
@ -1,17 +1,19 @@
|
|||
import { db } from "@link-stack/bridge-common";
|
||||
import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
||||
import * as signalApi from "@link-stack/signal-api";
|
||||
const { Configuration, MessagesApi } = signalApi;
|
||||
const { Configuration, MessagesApi, GroupsApi } = signalApi;
|
||||
|
||||
interface SendSignalMessageTaskOptions {
|
||||
token: string;
|
||||
to: string;
|
||||
message: any;
|
||||
conversationId?: string; // Zammad ticket/conversation ID for callback
|
||||
}
|
||||
|
||||
const sendSignalMessageTask = async ({
|
||||
token,
|
||||
to,
|
||||
message,
|
||||
conversationId,
|
||||
}: SendSignalMessageTaskOptions): Promise<void> => {
|
||||
const bot = await db
|
||||
.selectFrom("SignalBot")
|
||||
|
|
@ -24,15 +26,69 @@ 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 {
|
||||
// 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";
|
||||
|
||||
// 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) {
|
||||
finalTo = createGroupResponse.id;
|
||||
groupCreated = true;
|
||||
console.log(
|
||||
`Created new Signal group ${finalTo} for conversation ${conversationId} with ${to}`,
|
||||
);
|
||||
|
||||
// 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,
|
||||
timestamp: new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch (groupError) {
|
||||
console.error("Error creating Signal group:", groupError);
|
||||
// Continue with original recipient if group creation fails
|
||||
}
|
||||
}
|
||||
|
||||
const response = await messagesClient.v2SendPost({
|
||||
data: {
|
||||
number,
|
||||
recipients: [to],
|
||||
recipients: [finalTo],
|
||||
message,
|
||||
},
|
||||
});
|
||||
|
||||
console.log(
|
||||
`Message sent successfully to ${finalTo}${groupCreated ? " (new group)" : ""}`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error({ error });
|
||||
throw error;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue