fix(bot): pre-fetch group metadata + retry sender on libsignal race
First send to a group after pairing fails with libsignal SessionError "No sessions" because Baileys hasn't yet established encryption sessions with all participants. Force-fetch group metadata before sendMessage so Baileys populates its participant map; if the first send still races, retry once after a 1.5s delay.
This commit is contained in:
parent
3c4eedff03
commit
99cece16c0
@ -5,6 +5,29 @@ export async function sendTextToGroup(
|
|||||||
groupJid: string,
|
groupJid: string,
|
||||||
text: string,
|
text: string,
|
||||||
): Promise<{ messageId: string | undefined }> {
|
): Promise<{ messageId: string | undefined }> {
|
||||||
const result = await socket.sendMessage(groupJid, { text });
|
// Force-fetch group metadata so Baileys populates its internal participant
|
||||||
return { messageId: result?.key?.id ?? undefined };
|
// map and triggers libsignal session establishment for any unknown member.
|
||||||
|
// Without this, the first send to a freshly-paired group fails with
|
||||||
|
// "No sessions" from libsignal-node.
|
||||||
|
try {
|
||||||
|
await socket.groupMetadata(groupJid);
|
||||||
|
} catch {
|
||||||
|
// If metadata fetch itself fails we still try the send — sendMessage will
|
||||||
|
// surface a clearer error than the metadata layer.
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await socket.sendMessage(groupJid, { text });
|
||||||
|
return { messageId: result?.key?.id ?? undefined };
|
||||||
|
} catch (err) {
|
||||||
|
// libsignal session establishment can race on the very first send. Retry
|
||||||
|
// once after a brief delay before giving up.
|
||||||
|
const message = (err as Error)?.message ?? "";
|
||||||
|
if (message.includes("No sessions")) {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
||||||
|
const result = await socket.sendMessage(groupJid, { text });
|
||||||
|
return { messageId: result?.key?.id ?? undefined };
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user