Add Signal auto-group creation
This commit is contained in:
parent
a83907b4be
commit
c8ccee7ada
7 changed files with 393 additions and 8 deletions
120
apps/bridge-worker/scripts/test-signal-groups.ts
Executable file
120
apps/bridge-worker/scripts/test-signal-groups.ts
Executable file
|
|
@ -0,0 +1,120 @@
|
|||
#!/usr/bin/env node
|
||||
import { config } from "dotenv";
|
||||
import * as signalApi from "@link-stack/signal-api";
|
||||
|
||||
// Load environment variables
|
||||
config();
|
||||
|
||||
const { Configuration, GroupsApi, MessagesApi } = signalApi;
|
||||
|
||||
async function testSignalGroups() {
|
||||
console.log("Signal Groups Test Script");
|
||||
console.log("=========================\n");
|
||||
|
||||
// Check environment
|
||||
const autoGroupsEnabled = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true";
|
||||
console.log(`Auto-groups enabled: ${autoGroupsEnabled}`);
|
||||
console.log(`Signal API URL: ${process.env.BRIDGE_SIGNAL_URL}\n`);
|
||||
|
||||
if (!process.env.BRIDGE_SIGNAL_URL) {
|
||||
console.error("Error: BRIDGE_SIGNAL_URL not set");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
const config = new Configuration({
|
||||
basePath: process.env.BRIDGE_SIGNAL_URL,
|
||||
});
|
||||
const groupsClient = new GroupsApi(config);
|
||||
const messagesClient = new MessagesApi(config);
|
||||
|
||||
// List existing groups
|
||||
console.log("Listing existing groups...");
|
||||
const groups = await groupsClient.v1GroupsNumberGet({
|
||||
number: process.env.BRIDGE_SIGNAL_BOT_NUMBER,
|
||||
});
|
||||
|
||||
console.log(`Found ${groups.length} groups:`);
|
||||
groups.forEach((group, i) => {
|
||||
console.log(` ${i + 1}. ${group.name} (${group.id})`);
|
||||
console.log(` Members: ${group.members?.length || 0}`);
|
||||
});
|
||||
console.log();
|
||||
|
||||
// Test creating a group (if requested via command line)
|
||||
if (process.argv.includes("--create-group")) {
|
||||
const testNumber = process.argv[process.argv.indexOf("--create-group") + 1];
|
||||
if (!testNumber) {
|
||||
console.error("Error: Please provide a phone number after --create-group");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`Creating test group with ${testNumber}...`);
|
||||
const createResponse = await groupsClient.v1GroupsNumberPost({
|
||||
number: process.env.BRIDGE_SIGNAL_BOT_NUMBER,
|
||||
data: {
|
||||
name: `Test Support: ${testNumber}`,
|
||||
members: [testNumber],
|
||||
description: "Test private support conversation",
|
||||
},
|
||||
});
|
||||
console.log(`Created group: ${createResponse.id}\n`);
|
||||
|
||||
// Send a test message to the group
|
||||
if (process.argv.includes("--send-message")) {
|
||||
console.log("Sending test message to group...");
|
||||
await messagesClient.v2SendPost({
|
||||
data: {
|
||||
number: process.env.BRIDGE_SIGNAL_BOT_NUMBER,
|
||||
recipients: [createResponse.id!],
|
||||
message: "Hello! This is a test message to the new Signal group.",
|
||||
isGroup: true,
|
||||
},
|
||||
});
|
||||
console.log("Message sent successfully!\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Test the receive flow simulation
|
||||
if (process.argv.includes("--simulate-receive")) {
|
||||
const fromNumber = process.argv[process.argv.indexOf("--simulate-receive") + 1];
|
||||
if (!fromNumber) {
|
||||
console.error("Error: Please provide a phone number after --simulate-receive");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`Simulating message receive from ${fromNumber}...`);
|
||||
console.log("This would trigger the following in production:");
|
||||
console.log("1. Message received from individual");
|
||||
console.log("2. Auto-group creation (if enabled)");
|
||||
console.log("3. Webhook notification to Zammad with group ID in 'to' field\n");
|
||||
|
||||
if (autoGroupsEnabled) {
|
||||
console.log("Since auto-groups is enabled, a new group would be created.");
|
||||
console.log(`Group name would be: Support: ${fromNumber}`);
|
||||
} else {
|
||||
console.log("Auto-groups is disabled, so message would be processed normally.");
|
||||
}
|
||||
}
|
||||
|
||||
console.log("\nTest script completed successfully!");
|
||||
} catch (error) {
|
||||
console.error("Error during test:", error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Show usage if no arguments
|
||||
if (process.argv.length === 2) {
|
||||
console.log("Usage:");
|
||||
console.log(" List groups: ts-node test-signal-groups.ts");
|
||||
console.log(" Create group: ts-node test-signal-groups.ts --create-group +1234567890");
|
||||
console.log(" Create & send: ts-node test-signal-groups.ts --create-group +1234567890 --send-message");
|
||||
console.log(" Simulate receive: ts-node test-signal-groups.ts --simulate-receive +1234567890");
|
||||
console.log("\nEnvironment variables:");
|
||||
console.log(" BRIDGE_SIGNAL_AUTO_GROUPS=true Enable auto-group creation");
|
||||
console.log(" BRIDGE_SIGNAL_URL=http://... Signal CLI REST API URL");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
testSignalGroups().catch(console.error);
|
||||
|
|
@ -48,6 +48,12 @@ const processMessage = async ({
|
|||
const { envelope } = msg;
|
||||
const { source, sourceUuid, dataMessage } = envelope;
|
||||
|
||||
const isGroup = !!(
|
||||
dataMessage?.groupV2 ||
|
||||
dataMessage?.groupContext ||
|
||||
dataMessage?.groupInfo
|
||||
);
|
||||
|
||||
if (!dataMessage) return [];
|
||||
|
||||
const { attachments } = dataMessage;
|
||||
|
|
@ -67,6 +73,7 @@ const processMessage = async ({
|
|||
attachment: primaryAttachment.attachment,
|
||||
filename: primaryAttachment.filename,
|
||||
mimeType: primaryAttachment.mimeType,
|
||||
isGroup,
|
||||
};
|
||||
const formattedMessages = [primaryMessage];
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
import { db, getWorkerUtils } from "@link-stack/bridge-common";
|
||||
import * as signalApi from "@link-stack/signal-api";
|
||||
const { Configuration, GroupsApi } = signalApi;
|
||||
|
||||
interface ReceiveSignalMessageTaskOptions {
|
||||
token: string;
|
||||
|
|
@ -10,6 +12,7 @@ interface ReceiveSignalMessageTaskOptions {
|
|||
attachment?: string;
|
||||
filename?: string;
|
||||
mimeType?: string;
|
||||
isGroup?: boolean;
|
||||
}
|
||||
|
||||
const receiveSignalMessageTask = async ({
|
||||
|
|
@ -22,6 +25,7 @@ const receiveSignalMessageTask = async ({
|
|||
attachment,
|
||||
filename,
|
||||
mimeType,
|
||||
isGroup,
|
||||
}: ReceiveSignalMessageTaskOptions): Promise<void> => {
|
||||
const worker = await getWorkerUtils();
|
||||
const row = await db
|
||||
|
|
@ -31,8 +35,43 @@ const receiveSignalMessageTask = async ({
|
|||
.executeTakeFirstOrThrow();
|
||||
|
||||
const backendId = row.id;
|
||||
let finalTo = to;
|
||||
|
||||
// Check if auto-group creation is enabled and this is NOT already a group message
|
||||
const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true";
|
||||
|
||||
if (enableAutoGroups && !isGroup && from && to) {
|
||||
try {
|
||||
const config = new Configuration({
|
||||
basePath: process.env.BRIDGE_SIGNAL_URL,
|
||||
});
|
||||
const groupsClient = new GroupsApi(config);
|
||||
|
||||
// Create new group for this conversation
|
||||
const groupName = `Support: ${from}`;
|
||||
const createGroupResponse = await groupsClient.v1GroupsNumberPost({
|
||||
number: row.phoneNumber,
|
||||
data: {
|
||||
name: groupName,
|
||||
members: [from],
|
||||
description: "Private support conversation",
|
||||
},
|
||||
});
|
||||
|
||||
if (createGroupResponse.id) {
|
||||
finalTo = createGroupResponse.id;
|
||||
console.log(
|
||||
`Created new Signal group ${finalTo} for conversation with ${from}`,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error creating Signal group:", error);
|
||||
// Continue with original 'to' if group creation fails
|
||||
}
|
||||
}
|
||||
|
||||
const payload = {
|
||||
to,
|
||||
to: finalTo,
|
||||
from,
|
||||
message_id: messageId,
|
||||
sent_at: sentAt,
|
||||
|
|
|
|||
|
|
@ -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