Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

feat(plugins): let a plugin ask for a link preview on a text send

Yudhi Armyndharis535d584a6560
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5027dcef8..38b230549 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 ask for a link preview.** `ConversationSendEnvelope` gained `linkPreview`, forwarded to the engine on a plain text send. Since 0.14.0 made Baileys previews opt-in, a plugin relaying a URL had no way to restore the preview card the REST API can already request — most visibly for a bot whose reply *is* a bare link. Ignored on media, location and quoted sends, whose engine paths take no preview option.
+
### Fixed
- **Swagger "Try it out" called `http://localhost:2785` instead of the host that served the docs**, failing with `Failed to fetch` anywhere else — a relative server is now listed first, so it resolves to the serving origin.
diff --git a/src/core/plugins/conversation-send-facade.spec.ts b/src/core/plugins/conversation-send-facade.spec.ts
index 36e5e42cf..1dd510b61 100644
--- a/src/core/plugins/conversation-send-facade.spec.ts
+++ b/src/core/plugins/conversation-send-facade.spec.ts
@@ -60,6 +60,63 @@ describe('conversation.send facade', () => {
expect(res).toEqual({ id: 'm1' });
});
+ it.each([true, false])('forwards an explicit linkPreview: %s to the text send', async choice => {
+ const sendText = jest.fn().mockResolvedValue({ id: 'm1' });
+ 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,
+ reply: jest.fn(),
+ } as never);
+ await facade.send({
+ type: 'text',
+ text: 'see https://example.com',
+ sessionId: 's',
+ chatId: '[email protected]',
+ linkPreview: choice,
+ });
+ expect(sendText).toHaveBeenCalledWith('s', {
+ chatId: '[email protected]',
+ text: 'see https://example.com',
+ linkPreview: choice,
+ });
+ });
+
+ it('ignores linkPreview on a media envelope rather than rejecting the send', async () => {
+ // The engine media path takes no preview option, but a caption carrying a URL is ordinary — refusing
+ // the send over a hint the engine simply cannot act on would cost the message for nothing.
+ const sendMedia = jest.fn().mockResolvedValue({ id: 'm2' });
+ const sendText = jest.fn();
+ 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,
+ reply: jest.fn(),
+ sendMedia,
+ } as never);
+ await facade.send({
+ type: 'image',
+ mediaUrl: 'https://cdn.example.com/x.jpg',
+ text: 'see https://example.com',
+ sessionId: 's',
+ chatId: '[email protected]',
+ linkPreview: true,
+ });
+ expect(sendMedia).toHaveBeenCalledWith('s', {
+ chatId: '[email protected]',
+ url: 'https://cdn.example.com/x.jpg',
+ type: 'image',
+ caption: 'see https://example.com',
+ });
+ expect(sendText).not.toHaveBeenCalled();
+ });
+
it('routes a media envelope to sendMedia with the caption from text, not sendText', async () => {
const sendMedia = jest.fn().mockResolvedValue({ id: 'm2' });
const sendText = jest.fn();
diff --git a/src/core/plugins/conversation-send-facade.ts b/src/core/plugins/conversation-send-facade.ts
index 7401408ee..b8223488e 100644
--- a/src/core/plugins/conversation-send-facade.ts
+++ b/src/core/plugins/conversation-send-facade.ts
@@ -18,7 +18,7 @@ export interface ConversationSendDeps {
// synchronously inside sendText/reply. 'message:sent' is emitted later by SessionEngineLifecycle's
// engine callback (onMessageCreate), outside this call's async scope, so seeding it here would be a no-op.
runGuarded: <T>(events: string[], run: () => Promise<T>) => Promise<T>;
- sendText: (sessionId: string, opts: { chatId: string; text: string }) => Promise<unknown>;
+ sendText: (sessionId: string, opts: { chatId: string; text: string; linkPreview?: boolean }) => Promise<unknown>;
reply: (sessionId: string, opts: { chatId: string; quotedMessageId: string; text: string }) => Promise<unknown>;
// Media send by URL. The loader binds this to the per-type MessageService media methods
// (sendImage/sendVideo/sendAudio/sendDocument) — the facade stays DTO-agnostic.
@@ -93,7 +93,14 @@ export function buildConversationSendFacade(deps: ConversationSendDeps) {
if (env.replyTo) {
return deps.reply(sessionId, { chatId, quotedMessageId: env.replyTo, text: env.text ?? '' });
}
- return deps.sendText(sessionId, { chatId, text: env.text ?? '' });
+ // Forwarded only when the plugin actually expressed a choice: MessageService widens the engine
+ // call whenever the key is present, so a blanket `linkPreview: undefined` would rewrite the call
+ // shape of every plugin send that never asked for a preview.
+ return deps.sendText(sessionId, {
+ chatId,
+ text: env.text ?? '',
+ ...(env.linkPreview === undefined ? {} : { linkPreview: env.linkPreview }),
+ });
});
},
};
diff --git a/src/core/plugins/plugin.interfaces.ts b/src/core/plugins/plugin.interfaces.ts
index 23160e8b6..6823b92b5 100644
--- a/src/core/plugins/plugin.interfaces.ts
+++ b/src/core/plugins/plugin.interfaces.ts
@@ -274,6 +274,13 @@ export interface ConversationSendEnvelope {
text?: string;
mediaUrl?: string;
replyTo?: string;
+ /**
+ * Ask the engine for a link preview on a plain text send. Baileys generates one only when this is
+ * `true`, so a plugin relaying a URL gets a bare link without it; whatsapp-web.js previews by
+ * default and takes `false` to suppress. Ignored on media, location and quoted sends, which route
+ * through engine paths that take no preview option.
+ */
+ linkPreview?: boolean;
/** WGS84 coordinates; required for type 'location', ignored otherwise. `text` doubles as the
* location description. */
latitude?: number;