Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.4 KB
Raw Blame
import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { whatsscaleAuth } from '../auth';
import { whatsscaleClient } from '../common/client';
import { whatsscaleProps } from '../common/props';
import { groupMessageOutputSchema } from '../output-schemas';

export const watchSpecificGroupMessagesTrigger = createTrigger({
  auth: whatsscaleAuth,
  name: 'watch_specific_group_messages',
  classification: 'READ',
  displayName: 'Watch Specific Group Messages',
  description: 'Triggers when a new message is received in a specific WhatsApp group.',
  aiMetadata: {
    description:
      'Fires when a new message is posted in one chosen WhatsApp group (selected by group ID) on the connected session. Each event represents a single inbound group message and includes the message body, sender (participant) ID and name, media details, and reply/forward metadata. Use to react only to a specific group rather than every group the session belongs to.',
  },
  outputSchema: groupMessageOutputSchema,
  type: TriggerStrategy.WEBHOOK,
  props: {
    session: whatsscaleProps.session,
    group: whatsscaleProps.group,
  },
  sampleData: {
    message_id: '3EB0A1B2C3D4E5F6G7H8I9',
    group_id: '[email protected]',
    participant_id: '43001330020491@lid',
    participant_name: 'Jane Smith',
    participant_phone: '962781397972',
    body: 'Hey everyone, meeting at 3pm today!',
    has_media: false,
    media_type: 'text',
    media_url: '',
    media_mimetype: '',
    media_filename: '',
    timestamp: '2026-02-18T10:30:00.000Z',
    is_forwarded: false,
    quoted_message_id: '',
    quoted_body: '',
    is_reply: false,
    from_name: 'Jane Smith',
    chat_id: '[email protected]',
    session_name: 'my_session',
  },
  async onEnable(context) {
    const auth = context.auth.secret_text;
    await whatsscaleClient(auth, HttpMethod.POST, '/make/hooks/subscribe', {
      session: context.propsValue.session,
      webhook_url: context.webhookUrl,
      platform: 'activepieces',
      trigger_type: 'group',
      filter_id: context.propsValue.group, // includes @g.us — send as-is
    });
  },
  async onDisable(context) {
    const auth = context.auth.secret_text;
    await whatsscaleClient(auth, HttpMethod.POST, '/make/hooks/unsubscribe', {
      webhook_url: context.webhookUrl,
    });
  },
  async run(context) {
    return [context.payload.body];
  },
});