Groups WIP #2

This commit is contained in:
Darren Clarke 2025-07-08 18:03:01 +02:00
parent 7be5cb1478
commit a55e939592
4 changed files with 265 additions and 270 deletions

View file

@ -45,13 +45,13 @@ const sendSignalMessageTask = async ({
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,
);
// 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";
console.log(`[send-signal-message] Recipient analysis:`, {
to,
isGroupId,
@ -73,44 +73,46 @@ const sendSignalMessageTask = async ({
});
if (createGroupResponse.id) {
// 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--;
}
// The createGroupResponse.id already contains the full group identifier (group.BASE64)
finalTo = createGroupResponse.id;
groupCreated = true;
console.log(
`[send-signal-message] Created new Signal group:`,
{
groupId: finalTo,
internalId,
groupName,
conversationId,
originalRecipient: to,
botNumber: bot.phoneNumber,
},
);
// 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;
console.log(
`[send-signal-message] Got actual internalId: ${internalId}`,
);
} else {
// Fallback: extract base64 part from ID
if (finalTo.startsWith('group.')) {
internalId = finalTo.substring(6);
}
}
} catch (fetchError) {
console.log(
`[send-signal-message] Could not fetch group details, using ID base64 part`,
);
// Fallback: extract base64 part from ID
if (finalTo.startsWith('group.')) {
internalId = finalTo.substring(6);
}
}
console.log(`[send-signal-message] Created new Signal group:`, {
groupId: finalTo,
internalId,
groupName,
conversationId,
originalRecipient: to,
botNumber: bot.phoneNumber,
});
// Notify Zammad about the new group ID via webhook
await worker.addJob("common/notify-webhooks", {
@ -141,24 +143,32 @@ const sendSignalMessageTask = async ({
originalTo: to,
recipientChanged: to !== finalTo,
groupCreated,
isGroupRecipient: finalTo.startsWith('group.'),
});
// Build the message data with optional quote parameters
const messageData: signalApi.ApiSendMessageV2 = {
number,
recipients: [finalTo],
message,
};
console.log(`[send-signal-message] Message data being sent:`, {
number,
recipients: [finalTo],
message: message.substring(0, 50) + '...',
hasQuoteParams: !!(quoteMessage && quoteAuthor && quoteTimestamp),
});
// 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) + '...',
quoteMessage: quoteMessage.substring(0, 50) + "...",
quoteTimestamp,
});
}
@ -167,16 +177,32 @@ const sendSignalMessageTask = async ({
data: messageData,
});
console.log(
`[send-signal-message] Message sent successfully:`,
{
to: finalTo,
groupCreated,
response: response?.timestamp || 'no timestamp',
},
);
} catch (error) {
console.error({ error });
console.log(`[send-signal-message] Message sent successfully:`, {
to: finalTo,
groupCreated,
response: response?.timestamp || "no timestamp",
});
} catch (error: any) {
// Try to get the actual error message from the response
if (error.response) {
try {
const errorBody = await error.response.text();
console.error(`[send-signal-message] Signal API error:`, {
status: error.response.status,
statusText: error.response.statusText,
body: errorBody,
sentTo: finalTo,
messageDetails: {
fromNumber: number,
toRecipients: [finalTo],
hasQuote: !!(quoteMessage),
},
});
} catch (e) {
console.error(`[send-signal-message] Could not parse error response`);
}
}
console.error(`[send-signal-message] Full error:`, error);
throw error;
}
};