feat: waha-webhook

This commit is contained in:
devlikepro committed 2026-08-31 12:32:48 +07:00
1 parent e968b49349
commit 69306942ee
8 files changed
+63 -42

No files matched your search

+2 -17
View File
@@ -1,22 +1,18 @@
import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { GlobalWebhookConfigConfig } from '@waha/core/plugins/WebhookPlugin.env';
import { MediaDownloadOptions } from '@waha/core/media/IMediaManager';
import { IgnoreJidConfig } from '@waha/core/utils/jids';
import * as lodash from 'lodash';
import { parseBool } from './helpers';
import { WebhookConfig } from './structures/webhooks.config.dto';
import { Auth } from '@waha/core/auth/config';
@Injectable()
export class WhatsappConfigService implements OnApplicationBootstrap {
export class WhatsappConfigService {
private logger: Logger;
private webhookConfig: GlobalWebhookConfigConfig;
constructor(private configService: ConfigService) {
this.logger = new Logger('WhatsappConfigService');
this.webhookConfig = new GlobalWebhookConfigConfig(configService);
}
get schema() {
@@ -155,10 +151,6 @@ export class WhatsappConfigService implements OnApplicationBootstrap {
return this.configService.get('WHATSAPP_PROXY_SERVER_PASSWORD', undefined);
}
getWebhookConfig(): WebhookConfig | undefined {
return this.webhookConfig.config;
}
getSessionMongoUrl(): string | undefined {
return this.configService.get('WHATSAPP_SESSIONS_MONGO_URL', undefined);
}
@@ -236,11 +228,4 @@ export class WhatsappConfigService implements OnApplicationBootstrap {
);
return { status, groups, channels, broadcast };
}
onApplicationBootstrap() {
const error = this.webhookConfig.validateConfig();
if (error) {
throw new Error(`Invalid global webhook config:\n${error}\n`);
}
}
}
+2
View File
@@ -34,6 +34,7 @@ import { isPresenceAutoOnlineEnabled } from '@waha/modules/waha-maintain-online-
import { MaintainOnlineStatusModule } from '@waha/modules/waha-maintain-online-status/maintain-online-status.module';
import { isJidEngine } from '@waha/modules/waha-wid-jid/wid-jid.plugins';
import { MessageSourceModule } from '@waha/modules/waha-message-source/message-source.module';
import { WebhookModule } from '@waha/modules/waha-webhook/webhook.module';
import { SessionRuntimeInfoModule } from '@waha/modules/waha-session-runtime-info/session-runtime-info.module';
import { WidJIDModule } from '@waha/modules/waha-wid-jid/wid-jid.module';
import { WidSuffixModule } from '@waha/modules/waha-wid-suffix/wid-suffix.module';
@@ -149,6 +150,7 @@ export const IMPORTS_CORE = [
TerminusModule,
SessionPluginsModule,
SessionRuntimeInfoModule,
WebhookModule,
MessageSourceModule,
WidSuffixModule,
ConditionalModule.registerWhen(WidJIDModule, isJidEngine, {
-20
View File
@@ -67,12 +67,10 @@ import {
SessionDTO,
SessionInfo,
} from '../structures/sessions.dto';
import { WebhookConfig } from '../structures/webhooks.config.dto';
import { populateSessionInfo, SessionManager } from './abc/manager.abc';
import { SessionParams, WhatsappSession } from './abc/session.abc';
import { EngineConfigService } from './config/EngineConfigService';
import { WebhookPlugin } from '@waha/core/plugins/WebhookPlugin';
import { SessionPluginsService } from '@waha/plugins/SessionPluginsService';
const ALL = '*';
@@ -379,9 +377,6 @@ export class SessionManagerCore
this.updateSessions();
// Plugins
const webhooks = this.getWebhooks(config);
session.plugins.add(WebhookPlugin.with({ webhooks: webhooks }, null));
// Plugins contributed by modules (SessionPluginsModule)
for (const options of this.sessionPlugins.plugins(session)) {
session.plugins.add(options);
}
@@ -479,21 +474,6 @@ export class SessionManagerCore
this.log.info({ session: name }, `Session has been logged out.`);
}
/**
* Combine per session and global webhooks
*/
private getWebhooks(config: SessionConfig) {
let webhooks: WebhookConfig[] = [];
if (config?.webhooks) {
webhooks = webhooks.concat(config.webhooks);
}
const globalWebhookConfig = this.config.getWebhookConfig();
if (globalWebhookConfig) {
webhooks.push(globalWebhookConfig);
}
return webhooks;
}
/**
* Get either session's or global proxy if defined
*/
@@ -1,26 +1,38 @@
import { populateSessionInfo } from '@waha/core/abc/manager.abc';
import { SessionPlugin } from '@waha/core/abc/session.plugin';
import { WebhookSender } from '@waha/core/plugins/WebhookPlugin.sender';
import { WebhookSender } from '@waha/modules/waha-webhook/WebhookPlugin.sender';
import { WAHAEvents, WAHAEventsWild } from '@waha/structures/enums.dto';
import { WebhookConfig } from '@waha/structures/webhooks.config.dto';
import { EventWildUnmask } from '@waha/utils/events';
export class WebhookPluginConfig {
webhooks: WebhookConfig[];
webhooks: WebhookConfig | null;
}
/**
* Sends session events to the configured webhooks (per session and global ones)
* Sends session events to the configured webhooks (per session and predefined ones)
*/
export class WebhookPlugin extends SessionPlugin<WebhookPluginConfig> {
private eventUnmask = new EventWildUnmask(WAHAEvents, WAHAEventsWild);
attach(): void {
for (const webhookConfig of this.config.webhooks) {
for (const webhookConfig of this.webhooks()) {
this.configure(webhookConfig);
}
}
/**
* Session webhooks + the predefined one (WHATSAPP_HOOK_URL)
*/
private webhooks(): WebhookConfig[] {
const webhooks: WebhookConfig[] = [];
webhooks.push(...(this.session.sessionConfig?.webhooks ?? []));
if (this.config.webhooks) {
webhooks.push(this.config.webhooks);
}
return webhooks;
}
private getSuitableEvents(events: WAHAEvents[] | string[]): WAHAEvents[] {
return this.eventUnmask.unmask(events);
}
@@ -1,3 +1,4 @@
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { WAHAEvents } from '@waha/structures/enums.dto';
import {
@@ -19,11 +20,19 @@ enum Env {
WHATSAPP_HOOK_CUSTOM_HEADERS = 'WHATSAPP_HOOK_CUSTOM_HEADERS',
}
export class GlobalWebhookConfigConfig {
@Injectable()
export class GlobalWebhookConfig implements OnApplicationBootstrap {
protected _config: WebhookConfig | null = null;
constructor(private configService: ConfigService) {}
onApplicationBootstrap() {
const error = this.validateConfig();
if (error) {
throw new Error(`Invalid global webhook config:\n${error}\n`);
}
}
private getUrl(): string | undefined {
return this.configService.get(Env.WHATSAPP_HOOK_URL);
}
@@ -0,0 +1,18 @@
import { Module } from '@nestjs/common';
import { GlobalWebhookConfig } from '@waha/modules/waha-webhook/webhook.config';
import { WebhookPluginsProvider } from '@waha/modules/waha-webhook/webhook.plugins';
import { SessionPluginsModule } from '@waha/plugins/session.plugins.module';
import { SessionPluginsService } from '@waha/plugins/SessionPluginsService';
@Module({
imports: [SessionPluginsModule],
providers: [GlobalWebhookConfig, WebhookPluginsProvider],
})
export class WebhookModule {
constructor(
sessionPlugins: SessionPluginsService,
provider: WebhookPluginsProvider,
) {
sessionPlugins.register(provider);
}
}
@@ -0,0 +1,15 @@
import { Injectable } from '@nestjs/common';
import type { WhatsappSession } from '@waha/core/abc/session.abc';
import { PluginOptions } from '@waha/core/abc/session.plugin';
import { GlobalWebhookConfig } from '@waha/modules/waha-webhook/webhook.config';
import { WebhookPlugin } from '@waha/modules/waha-webhook/WebhookPlugin';
import { SessionPluginsProvider } from '@waha/plugins/SessionPluginsService';
@Injectable()
export class WebhookPluginsProvider implements SessionPluginsProvider {
constructor(private globalWebhook: GlobalWebhookConfig) {}
plugins(session: WhatsappSession): PluginOptions[] {
return [WebhookPlugin.with({ webhooks: this.globalWebhook.config }, null)];
}
}