Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(plugins): add disabled-by-default auto-reply reference extension plugin
rmyndharis12d76972767c
diff --git a/src/app.module.ts b/src/app.module.tsindex 7266e5ec4..4a86fb931 100644--- a/src/app.module.ts+++ b/src/app.module.ts@@ -29,6 +29,7 @@ import { CatalogModule } from './modules/catalog/catalog.module';import { HooksModule } from './core/hooks';import { PluginsModule } from './core/plugins';import { PluginsApiModule } from './modules/plugins/plugins.module';+import { ExtensionsModule } from './plugins/extensions/extensions.module';// Only import QueueModule if explicitly enabled to avoid Redis connection errorsconst queueModules: Array<Type | DynamicModule> = [];@@ -186,6 +187,7 @@ if (process.env.QUEUE_ENABLED === 'true') {StatusModule, // Phase 3: Status/Stories APICatalogModule, // Phase 3: Catalog API (WhatsApp Business)PluginsApiModule, // Phase 5: Plugins API+ ExtensionsModule, // First-party extension plugins (registered disabled)],})export class AppModule {}diff --git a/src/plugins/extensions/auto-reply/auto-reply.plugin.spec.ts b/src/plugins/extensions/auto-reply/auto-reply.plugin.spec.tsnew file mode 100644index 000000000..3efdf5c27--- /dev/null+++ b/src/plugins/extensions/auto-reply/auto-reply.plugin.spec.ts@@ -0,0 +1,70 @@+import { AutoReplyPlugin } from './index';+import { PluginContext } from '../../../core/plugins';+import { HookContext, HookEvent, HookHandler } from '../../../core/hooks';+import { IncomingMessage } from '../../../engine/interfaces/whatsapp-engine.interface';++function makeContext(reply: jest.Mock): { context: PluginContext; getHandler: () => HookHandler } {+ let captured: HookHandler | undefined;+ const context = {+ pluginId: 'auto-reply',+ registerHook: (_event: HookEvent, handler: HookHandler) => {+ captured = handler;+ },+ messages: { reply, sendText: jest.fn() },+ logger: { log: jest.fn(), debug: jest.fn(), warn: jest.fn(), error: jest.fn() },+ } as unknown as PluginContext;+ return { context, getHandler: () => captured as HookHandler };+}++function inbound(overrides: Partial<IncomingMessage> = {}): IncomingMessage {+ return {+ id: 'msg-1',+ from: '[email protected]',+ to: 'me',+ chatId: '[email protected]',+ body: 'ping',+ type: 'text',+ timestamp: 1,+ fromMe: false,+ isGroup: false,+ ...overrides,+ };+}++function ctxFor(data: IncomingMessage): HookContext<IncomingMessage> {+ return { event: 'message:received', data, sessionId: 'sess-1', timestamp: new Date(), source: 'Engine' };+}++describe('AutoReplyPlugin', () => {+ it('replies to an inbound direct message and keeps it in history', async () => {+ const reply = jest.fn().mockResolvedValue({ messageId: 'x', timestamp: 1 });+ const { context, getHandler } = makeContext(reply);+ await new AutoReplyPlugin().onEnable(context);++ const result = await getHandler()(ctxFor(inbound()));++ expect(reply).toHaveBeenCalledWith('sess-1', '[email protected]', 'msg-1', 'Auto-reply: ping');+ expect(result).toEqual({ continue: true });+ });++ it('does NOT reply to its own outgoing messages (fromMe)', async () => {+ const reply = jest.fn();+ const { context, getHandler } = makeContext(reply);+ await new AutoReplyPlugin().onEnable(context);++ const result = await getHandler()(ctxFor(inbound({ fromMe: true })));++ expect(reply).not.toHaveBeenCalled();+ expect(result).toEqual({ continue: true });+ });++ it('does NOT reply to group messages', async () => {+ const reply = jest.fn();+ const { context, getHandler } = makeContext(reply);+ await new AutoReplyPlugin().onEnable(context);++ await getHandler()(ctxFor(inbound({ isGroup: true })));++ expect(reply).not.toHaveBeenCalled();+ });+});diff --git a/src/plugins/extensions/auto-reply/index.ts b/src/plugins/extensions/auto-reply/index.tsnew file mode 100644index 000000000..f31d27c9b--- /dev/null+++ b/src/plugins/extensions/auto-reply/index.ts@@ -0,0 +1,43 @@+/**+ * Auto-reply reference extension plugin.+ *+ * Demonstrates the Tier-2 capability layer end-to-end: it hooks inbound messages and replies+ * via ctx.messages.reply. Registered DISABLED by default — enable it from the dashboard to try+ * the capability layer live. Replies only to inbound, non-group, engine-originated messages.+ */+import { PluginContext, IPlugin } from '../../../core/plugins';+import { HookContext, HookResult } from '../../../core/hooks';+import { IncomingMessage } from '../../../engine/interfaces/whatsapp-engine.interface';++export class AutoReplyPlugin implements IPlugin {+ onEnable(context: PluginContext): Promise<void> {+ context.registerHook('message:received', ctx =>+ this.onMessage(context, ctx as HookContext<IncomingMessage>),+ );+ context.logger.log('Auto-reply reference plugin enabled');+ return Promise.resolve();+ }++ private async onMessage(+ context: PluginContext,+ ctx: HookContext<IncomingMessage>,+ ): Promise<HookResult> {+ const message = ctx.data;++ // Reply only to inbound, non-group, engine-originated messages; never to our own sends.+ if (ctx.source !== 'Engine' || !ctx.sessionId || message.fromMe || message.isGroup) {+ return { continue: true };+ }++ try {+ await context.messages.reply(ctx.sessionId, message.chatId, message.id, `Auto-reply: ${message.body}`);+ } catch (error) {+ context.logger.error('Auto-reply failed', error);+ }++ // Keep the inbound message in history + webhooks + ws (do not swallow).+ return { continue: true };+ }+}++export default AutoReplyPlugin;diff --git a/src/plugins/extensions/auto-reply/manifest.json b/src/plugins/extensions/auto-reply/manifest.jsonnew file mode 100644index 000000000..11cefd317--- /dev/null+++ b/src/plugins/extensions/auto-reply/manifest.json@@ -0,0 +1,10 @@+{+ "id": "auto-reply",+ "name": "Auto Reply (reference)",+ "version": "1.0.0",+ "type": "extension",+ "description": "Reference extension plugin: replies to inbound direct messages. Disabled by default.",+ "main": "index.ts",+ "permissions": ["messages:send"],+ "sessions": ["*"]+}diff --git a/src/plugins/extensions/extensions.module.ts b/src/plugins/extensions/extensions.module.tsnew file mode 100644index 000000000..29651cd51--- /dev/null+++ b/src/plugins/extensions/extensions.module.ts@@ -0,0 +1,37 @@+import { Injectable, Module, OnModuleInit } from '@nestjs/common';+import { PluginLoaderService, PluginManifest, PluginType } from '../../core/plugins';+import { AutoReplyPlugin } from './auto-reply';+import { createLogger } from '../../common/services/logger.service';++/**+ * Registers first-party built-in EXTENSION plugins with the (global) PluginLoaderService.+ * Mirrors EngineFactory's registration pattern so src/core never imports a concrete plugin.+ * Built-in extensions are registered DISABLED; operators enable them via POST /plugins/:id/enable.+ */+@Injectable()+export class ExtensionsRegistrar implements OnModuleInit {+ private readonly logger = createLogger('ExtensionsRegistrar');++ constructor(private readonly pluginLoader: PluginLoaderService) {}++ onModuleInit(): void {+ const autoReplyManifest: PluginManifest = {+ id: 'auto-reply',+ name: 'Auto Reply (reference)',+ version: '1.0.0',+ type: PluginType.EXTENSION,+ description: 'Reference extension plugin: replies to inbound direct messages. Disabled by default.',+ main: 'index.ts',+ permissions: ['messages:send'],+ sessions: ['*'],+ };++ this.pluginLoader.registerBuiltInPlugin(autoReplyManifest, new AutoReplyPlugin());+ this.logger.log('Auto-reply reference plugin registered (disabled)');+ }+}++@Module({+ providers: [ExtensionsRegistrar],+})+export class ExtensionsModule {}