logger.debug({createdGroup},'Found created group details');
}
if(createdGroup&&createdGroup.internalId){
createdInternalId=createdGroup.internalId;
logger.debug({createdInternalId},'Got actual internalId');
}else{
// Fallback: extract base64 part from ID
if(finalTo.startsWith("group.")){
createdInternalId=finalTo.substring(6);
}
}
}catch(fetchError){
logger.debug('Could not fetch group details, using ID base64 part');
// Fallback: extract base64 part from ID
if(finalTo.startsWith("group.")){
createdInternalId=finalTo.substring(6);
}
}
logger.debug({
fullGroupId: finalTo,
internalId: createdInternalId,
},'Group created successfully');
logger.debug({
groupId: finalTo,
internalId: createdInternalId,
groupName,
forPhoneNumber: from,
botNumber: row.phoneNumber,
response: createGroupResponse,
},'Created new Signal group');
}
// Now handle notifications and message forwarding for both new and existing groups
if(finalTo&&finalTo.startsWith("group.")){
// Forward the user's initial message to the group using quote feature
try{
logger.debug('Forwarding initial message to group using quote feature');
constattributionMessage=`Message from ${from}:\n"${message}"\n\n---\nSupport team: Your request has been received. An agent will respond shortly.`;
awaitworker.addJob("signal/send-signal-message",{
token: row.token,
to: finalTo,
message: attributionMessage,
conversationId: null,
quoteMessage: message,
quoteAuthor: from,
quoteTimestamp: Date.parse(sentAt),
});
logger.debug({finalTo},'Successfully forwarded initial message to group');
}catch(forwardError){
logger.error({error: forwardError},'Error forwarding message to group');
}
// Send a response to the original DM informing about the group
try{
logger.debug('Sending group notification to original DM');
constdmNotification=`Hello! A private support group has been created for your conversation.\n\nGroup name: ${groupName}\n\nPlease look for the new group in your Signal app to continue the conversation. Our support team will respond there shortly.\n\nThank you for contacting support!`;
awaitworker.addJob("signal/send-signal-message",{
token: row.token,
to: from,
message: dmNotification,
conversationId: null,
});
logger.debug('Successfully sent group notification to user DM');