Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(plugins): send WhatsApp voice notes via ctx.conversations.send (#610)
Yudhi Armyndharis59e9e304c8e1
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5320c2141..31ce64b07 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++- **Plugins can send WhatsApp voice notes through `ctx.conversations.send`.** A new `voice` envelope type sends the media at `mediaUrl` as a PTT voice note (audio bubble with waveform) rather than a plain audio file — the host maps it to an audio send with `ptt` set, which defaults the codec to `audio/ogg; codecs=opus` and classifies the message as `voice`, matching inbound classification. It rides the same `conversation:send` permission and activated-session scope as the other media types. (#607)+## [0.8.2] - 2026-07-03### Addeddiff --git a/src/core/plugins/conversation-send-facade.spec.ts b/src/core/plugins/conversation-send-facade.spec.tsindex d7ca62dac..0f6845d67 100644--- a/src/core/plugins/conversation-send-facade.spec.ts+++ b/src/core/plugins/conversation-send-facade.spec.ts@@ -90,6 +90,27 @@ describe('conversation.send facade', () => {expect(res).toEqual({ id: 'm2' });});+ it('routes a voice envelope to sendMedia with type voice (loader maps it to a PTT audio send)', async () => {+ const sendMedia = jest.fn().mockResolvedValue({ id: 'v1' });+ const facade = buildConversationSendFacade({+ manifest: manifest(['conversation:send']) as never,+ assertPermission: () => undefined,+ assertSessionActive: jest.fn(),+ resolveChatId: () => Promise.resolve('[email protected]'),+ runGuarded: (_events: string[], run: () => Promise<unknown>) => run(),+ sendText: jest.fn(),+ reply: jest.fn(),+ sendMedia,+ } as never);+ await facade.send({ type: 'voice', mediaUrl: 'https://cdn.example/n.ogg', sessionId: 's', chatId: '[email protected]' });+ expect(sendMedia).toHaveBeenCalledWith('s', {+ chatId: '[email protected]',+ url: 'https://cdn.example/n.ogg',+ type: 'voice',+ caption: undefined,+ });+ });+it('rejects replyTo on a media envelope — media-reply is unsupported by the engine', async () => {const sendMedia = jest.fn();const facade = buildConversationSendFacade({diff --git a/src/core/plugins/conversation-send-facade.ts b/src/core/plugins/conversation-send-facade.tsindex b2f8b97dd..0a3695182 100644--- a/src/core/plugins/conversation-send-facade.ts+++ b/src/core/plugins/conversation-send-facade.ts@@ -28,10 +28,13 @@ export interface ConversationSendDeps {) => Promise<unknown>;}-/** Envelope types carried as media by URL. `location` is a non-text type but has no mediaUrl, so it is excluded. */-export type ConversationMediaType = 'image' | 'file' | 'audio' | 'video';+/**+ * Envelope types carried as media by URL. `voice` is a PTT audio note (the loader maps it to an audio+ * send with `ptt`). `location` is a non-text type but has no mediaUrl, so it is excluded.+ */+export type ConversationMediaType = 'image' | 'file' | 'audio' | 'video' | 'voice';-const MEDIA_TYPES: readonly ConversationMediaType[] = ['image', 'file', 'audio', 'video'];+const MEDIA_TYPES: readonly ConversationMediaType[] = ['image', 'file', 'audio', 'video', 'voice'];const isMediaType = (type: ConversationSendEnvelope['type']): type is ConversationMediaType =>(MEDIA_TYPES as readonly string[]).includes(type);diff --git a/src/core/plugins/plugin-loader.service.spec.ts b/src/core/plugins/plugin-loader.service.spec.tsindex 38f8bedbc..a7e04ab84 100644--- a/src/core/plugins/plugin-loader.service.spec.ts+++ b/src/core/plugins/plugin-loader.service.spec.ts@@ -88,7 +88,7 @@ describe('dispatchConversationMedia', () => {['video', 'sendVideo'],['audio', 'sendAudio'],['file', 'sendDocument'],- ] as const)('routes %s to %s with a url+caption DTO', async (type, method) => {+ ] as const)('routes %s to %s with a url+caption DTO (no ptt)', async (type, method) => {const s = svc();await dispatchConversationMedia(s, 's', opts(type));expect(s[method]).toHaveBeenCalledWith('s', { chatId: '[email protected]', url: 'https://cdn.example/m', caption: 'cap' });@@ -97,6 +97,20 @@ describe('dispatchConversationMedia', () => {if (other !== method) expect(s[other]).not.toHaveBeenCalled();}});++ it("routes 'voice' to sendAudio with ptt:true so it renders as a WhatsApp voice note", async () => {+ const s = svc();+ await dispatchConversationMedia(s, 's', { chatId: '[email protected]', url: 'https://cdn.example/n.ogg', type: 'voice' });+ expect(s.sendAudio).toHaveBeenCalledWith('s', {+ chatId: '[email protected]',+ url: 'https://cdn.example/n.ogg',+ caption: undefined,+ ptt: true,+ });+ for (const other of ['sendImage', 'sendVideo', 'sendDocument'] as const) {+ expect(s[other]).not.toHaveBeenCalled();+ }+ });});import * as fs from 'fs';diff --git a/src/core/plugins/plugin-loader.service.ts b/src/core/plugins/plugin-loader.service.tsindex 57822c983..315aa0d8b 100644--- a/src/core/plugins/plugin-loader.service.ts+++ b/src/core/plugins/plugin-loader.service.ts@@ -116,6 +116,10 @@ export function dispatchConversationMedia(return svc.sendVideo(sessionId, dto);case 'audio':return svc.sendAudio(sessionId, dto);+ case 'voice':+ // A voice envelope is a PTT note: sendAudio with ptt classifies it as 'voice' and defaults the+ // codec to audio/ogg;opus, so it renders as a WhatsApp voice bubble rather than an audio file.+ return svc.sendAudio(sessionId, { ...dto, ptt: true });case 'file':return svc.sendDocument(sessionId, dto);}diff --git a/src/core/plugins/plugin.interfaces.ts b/src/core/plugins/plugin.interfaces.tsindex ddf588ce2..8ef0586a8 100644--- a/src/core/plugins/plugin.interfaces.ts+++ b/src/core/plugins/plugin.interfaces.ts@@ -215,7 +215,7 @@ export interface ConversationSendEnvelope {sessionId?: string;instanceId?: string;chatId?: string;- type: 'text' | 'image' | 'file' | 'audio' | 'video' | 'location';+ type: 'text' | 'image' | 'file' | 'audio' | 'video' | 'voice' | 'location';text?: string;mediaUrl?: string;replyTo?: string;