More groups WIP
This commit is contained in:
parent
f20cd5a53c
commit
7be5cb1478
8 changed files with 488 additions and 261 deletions
|
|
@ -7,6 +7,9 @@ interface SendSignalMessageTaskOptions {
|
|||
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
|
||||
}
|
||||
|
||||
const sendSignalMessageTask = async ({
|
||||
|
|
@ -14,7 +17,16 @@ const sendSignalMessageTask = async ({
|
|||
to,
|
||||
message,
|
||||
conversationId,
|
||||
quoteMessage,
|
||||
quoteAuthor,
|
||||
quoteTimestamp,
|
||||
}: SendSignalMessageTaskOptions): Promise<void> => {
|
||||
console.log(`[send-signal-message] Processing outgoing message:`, {
|
||||
token,
|
||||
to,
|
||||
conversationId,
|
||||
messageLength: message?.length,
|
||||
});
|
||||
const bot = await db
|
||||
.selectFrom("SignalBot")
|
||||
.selectAll()
|
||||
|
|
@ -39,6 +51,13 @@ const sendSignalMessageTask = async ({
|
|||
to,
|
||||
);
|
||||
const enableAutoGroups = process.env.BRIDGE_SIGNAL_AUTO_GROUPS === "true";
|
||||
|
||||
console.log(`[send-signal-message] Recipient analysis:`, {
|
||||
to,
|
||||
isGroupId,
|
||||
enableAutoGroups,
|
||||
shouldCreateGroup: enableAutoGroups && !isGroupId && to && conversationId,
|
||||
});
|
||||
|
||||
// If sending to a phone number and auto-groups is enabled, create a group first
|
||||
if (enableAutoGroups && !isGroupId && to && conversationId) {
|
||||
|
|
@ -54,10 +73,43 @@ 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--;
|
||||
}
|
||||
|
||||
finalTo = createGroupResponse.id;
|
||||
groupCreated = true;
|
||||
console.log(
|
||||
`Created new Signal group ${finalTo} for conversation ${conversationId} with ${to}`,
|
||||
`[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
|
||||
|
|
@ -68,26 +120,60 @@ const sendSignalMessageTask = async ({
|
|||
conversation_id: conversationId,
|
||||
original_recipient: to,
|
||||
group_id: finalTo,
|
||||
internal_group_id: internalId,
|
||||
timestamp: new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch (groupError) {
|
||||
console.error("Error creating Signal group:", groupError);
|
||||
console.error("[send-signal-message] Error creating Signal group:", {
|
||||
error: groupError instanceof Error ? groupError.message : groupError,
|
||||
to,
|
||||
conversationId,
|
||||
});
|
||||
// Continue with original recipient if group creation fails
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
const response = await messagesClient.v2SendPost({
|
||||
data: {
|
||||
number,
|
||||
recipients: [finalTo],
|
||||
message,
|
||||
},
|
||||
data: messageData,
|
||||
});
|
||||
|
||||
console.log(
|
||||
`Message sent successfully to ${finalTo}${groupCreated ? " (new group)" : ""}`,
|
||||
`[send-signal-message] Message sent successfully:`,
|
||||
{
|
||||
to: finalTo,
|
||||
groupCreated,
|
||||
response: response?.timestamp || 'no timestamp',
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
console.error({ error });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue