Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

feat(baileys): honor chat disappearing-messages timer on outbound sends (#503)

Yudhi Armyndharisbf3dc44a5227
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 326deead8..2ccf769c8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Added
+
+- **Disappearing-messages support (Baileys engine).** Outbound messages now honor a chat's disappearing-messages timer: the Baileys adapter reads the chat's cached `ephemeralExpiration` and sets it on each send (text, media, and replies), so recipients no longer see _"This message won't disappear β€” the sender may be using an older version of WhatsApp."_ The timer is applied only when a positive value is known for the chat; when it's unknown, disabled, or not yet synced, the per-message expiration is omitted, exactly as before. Reactions, deletes/revokes, and status posts are unaffected. Thanks @ulises2k for the report. (#473)
+
## [0.7.10] - 2026-06-28
### Added
diff --git a/src/engine/adapters/baileys-session-store.spec.ts b/src/engine/adapters/baileys-session-store.spec.ts
index 3207f8219..664b8c7c6 100644
--- a/src/engine/adapters/baileys-session-store.spec.ts
+++ b/src/engine/adapters/baileys-session-store.spec.ts
@@ -76,6 +76,28 @@ describe('BaileysSessionStore', () => {
expect(store.lastMessage('[email protected]')).toBeNull();
});
+ describe('getEphemeralExpiration (#473)', () => {
+ it('returns the cached disappearing-messages timer for a chat', () => {
+ store.upsertChats([{ id: '[email protected]', ephemeralExpiration: 604800 }]);
+ expect(store.getEphemeralExpiration('[email protected]')).toBe(604800);
+ });
+
+ it('accepts a neutral @c.us id and folds it to the engine dialect for lookup', () => {
+ store.upsertChats([{ id: '[email protected]', ephemeralExpiration: 86400 }]);
+ expect(store.getEphemeralExpiration('[email protected]')).toBe(86400);
+ });
+
+ it('returns undefined when the chat is unknown, disabled (0), or null (no forced disappear)', () => {
+ store.upsertChats([{ id: '[email protected]', ephemeralExpiration: 0 }]);
+ store.upsertChats([{ id: '[email protected]', ephemeralExpiration: null }]);
+ store.upsertChats([{ id: '[email protected]' }]); // field absent
+ expect(store.getEphemeralExpiration('[email protected]')).toBeUndefined();
+ expect(store.getEphemeralExpiration('[email protected]')).toBeUndefined();
+ expect(store.getEphemeralExpiration('[email protected]')).toBeUndefined();
+ expect(store.getEphemeralExpiration('[email protected]')).toBeUndefined();
+ });
+ });
+
it('resolves a phone jid to its user-part, a lid via lidPnMappings, and a contact phoneNumber', () => {
expect(store.resolvePhone('[email protected]')).toBe('628111');
store.addLidMappings([{ lid: '111@lid', pn: '[email protected]' }]);
diff --git a/src/engine/adapters/baileys-session-store.ts b/src/engine/adapters/baileys-session-store.ts
index c232cf387..116e7aae2 100644
--- a/src/engine/adapters/baileys-session-store.ts
+++ b/src/engine/adapters/baileys-session-store.ts
@@ -127,6 +127,19 @@ export class BaileysSessionStore {
return m ? { key: m.key, timestamp: m.timestamp } : null;
}
+ /**
+ * The chat's disappearing-messages timer in seconds (#473), or `undefined` when no timer is known.
+ * Only a positive value is returned: `0` / `null` / absent all mean "no known timer", so the caller
+ * omits the per-message `ephemeralExpiration` and reproduces today's send behavior (Baileys' own send
+ * guard is truthy). This keeps a stale-empty or boot-window cache from ever forcing a message to
+ * disappear. Folds a neutral `@c.us` id to the engine dialect first, like the other chat lookups.
+ */
+ getEphemeralExpiration(chatId: string): number | undefined {
+ const chat = this.chats.get(chatId) ?? this.chats.get(this.toEngineJid(chatId));
+ const exp = chat?.ephemeralExpiration;
+ return typeof exp === 'number' && exp > 0 ? exp : undefined;
+ }
+
resolvePhone(id: string): string | null {
const parsed = parseWaId(id);
// A user id (@c.us / @s.whatsapp.net) already carries the phone as its user-part. The @c.us case
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 7feb8ddec..024f4010f 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -561,6 +561,18 @@ describe('BaileysAdapter messaging', () => {
expect(res).toEqual({ id: 'OUT1', timestamp: 1700000001 });
});
+ it('sendTextMessage honors the chat disappearing timer when one is cached (#473)', async () => {
+ fakeSock.sendMessage.mockResolvedValue({ key: { id: 'OUT1' }, messageTimestamp: 1700000001 });
+ const adapter = await readyAdapter();
+ fakeSock.fire('chats.upsert', [{ id: '[email protected]', ephemeralExpiration: 604800 }]);
+ await adapter.sendTextMessage('[email protected]', 'hello');
+ expect(fakeSock.sendMessage).toHaveBeenCalledWith(
+ { text: 'hello' },
+ { ephemeralExpiration: 604800 },
+ );
+ });
+
it('getNumberId resolves via onWhatsApp and returns a NEUTRAL jid (never @s.whatsapp.net)', async () => {
fakeSock.onWhatsApp.mockResolvedValue([{ jid: '[email protected]', exists: true }]);
const adapter = await readyAdapter();
@@ -1309,6 +1321,41 @@ describe('BaileysAdapter store-backed ops', () => {
expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', { delete: stored.key });
});
+ it('media sends honor the chat disappearing timer via the funnel (#473)', async () => {
+ const adapter = await ready();
+ fakeSock.fire('chats.upsert', [{ id: '[email protected]', ephemeralExpiration: 86400 }]);
+ await adapter.sendImageMessage('[email protected]', { mimetype: 'image/png', data: Buffer.from([1]) });
+ expect(fakeSock.sendMessage).toHaveBeenCalledWith(
+ expect.objectContaining({ image: Buffer.from([1]) }),
+ { ephemeralExpiration: 86400 },
+ );
+ });
+
+ it('replyToMessage merges the disappearing timer with the quoted option (#473)', async () => {
+ fakeStore.getMessage.mockResolvedValue(stored);
+ const adapter = await ready();
+ fakeSock.fire('chats.upsert', [{ id: '[email protected]', ephemeralExpiration: 604800 }]);
+ await adapter.replyToMessage('[email protected]', 'TARGET', 'my reply');
+ expect(fakeSock.sendMessage).toHaveBeenCalledWith(
+ { text: 'my reply' },
+ { quoted: stored, ephemeralExpiration: 604800 },
+ );
+ });
+
+ it('react and delete never carry an ephemeral timer (Baileys does not exclude reactions) (#473)', async () => {
+ fakeStore.getMessage.mockResolvedValue(stored);
+ const adapter = await ready();
+ fakeSock.fire('chats.upsert', [{ id: '[email protected]', ephemeralExpiration: 604800 }]);
+ await adapter.reactToMessage('[email protected]', 'TARGET', 'πŸ‘');
+ await adapter.deleteMessage('[email protected]', 'TARGET', true);
+ expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', {
+ react: { text: 'πŸ‘', key: stored.key },
+ });
+ expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', { delete: stored.key });
+ });
+
it('throws when the referenced message is not in the store', async () => {
fakeStore.getMessage.mockResolvedValue(null);
const adapter = await ready();
diff --git a/src/engine/adapters/baileys.adapter.ts b/src/engine/adapters/baileys.adapter.ts
index 141550c4a..de3d81a1e 100644
--- a/src/engine/adapters/baileys.adapter.ts
+++ b/src/engine/adapters/baileys.adapter.ts
@@ -467,7 +467,10 @@ export class BaileysAdapter implements IWhatsAppEngine {
async sendTextMessage(chatId: string, text: string): Promise<MessageResult> {
this.ensureReady();
- const sent = await this.sock!.sendMessage(chatId, { text });
+ const options = this.withEphemeral(chatId);
+ const sent = options
+ ? await this.sock!.sendMessage(chatId, { text }, options)
+ : await this.sock!.sendMessage(chatId, { text });
if (sent) {
void this.config.messageStore?.put(this.config.sessionId, sent).catch(err =>
this.logger.warn('Failed to persist sent message to store', {
@@ -1311,14 +1314,34 @@ export class BaileysAdapter implements IWhatsAppEngine {
].join('\n');
}
+ /**
+ * Fold the chat's known disappearing-messages timer into Baileys' send options so outbound messages
+ * honor the chat's ephemeral setting (#473). Returns `options` unchanged when no positive timer is
+ * cached: omitting `ephemeralExpiration` reproduces today's behavior (Baileys' send guard is truthy),
+ * so an unknown / boot-window / stale-empty cache never forces a message to disappear. Returning
+ * `undefined` keeps the send a 2-arg call, identical to before. React/delete/status do not route
+ * through here, so they are excluded by construction (reactions are NOT excluded by Baileys' guard).
+ */
+ private withEphemeral(
+ chatId: string,
+ options?: MiscMessageGenerationOptions,
+ ): MiscMessageGenerationOptions | undefined {
+ const ephemeralExpiration = this.sessionStore.getEphemeralExpiration(chatId);
+ if (ephemeralExpiration === undefined) {
+ return options;
+ }
+ return { ...options, ephemeralExpiration };
+ }
+
/** Send a Baileys content object and shape the result like the other sends. */
private async sendContent(
chatId: string,
content: AnyMessageContent,
options?: MiscMessageGenerationOptions,
): Promise<MessageResult> {
- const sent = options
- ? await this.sock!.sendMessage(chatId, content, options)
+ const merged = this.withEphemeral(chatId, options);
+ const sent = merged
+ ? await this.sock!.sendMessage(chatId, content, merged)
: await this.sock!.sendMessage(chatId, content);
if (sent) {
void this.config.messageStore?.put(this.config.sessionId, sent).catch(err =>