Botify

Menu

Botify Whatsapp Api integration with Payload CMS

Complete guide to sending WhatsApp notifications from Payload CMS. Learn how to enable jobs, create tasks, and automate messaging workflows

Enable Jobs in Payload Config

Payload has a built-in Jobs Queue system

payload.config.ts

import { buildConfig } from 'payload/config'

export default buildConfig({
  collections: [...],
  
  jobs: {
    tasks: [], // we will add tasks later
  },
})

Create a WhatsApp Task

A task is the actual worker function that sends the message.

/src/jobs/sendWhatsApp.ts

import type { TaskHandler } from 'payload/config'

export const sendWhatsAppTask: TaskHandler = async ({ input, req }) => {
  try {
    const { phone, message } = input

    // Example: Call your WhatsApp API (custom or third-party)
    const response = await fetch('https://botify.codenik.in/send-message', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Your API key`,
      },
      body: JSON.stringify({
        to: phone,
        message,
      }),
    })

    const data = await response.json()

    return {
      output: data,
    }
  } catch (error) {
    console.error('WhatsApp Task Error:', error)

    throw error
  }
}

Register Task in Payload Config

πŸ‘‰ update payload.config.ts

import { sendWhatsAppTask } from './jobs/sendWhatsApp'

export default buildConfig({
 collections: [...],

 jobs: {
   tasks: [
     {
       slug: 'send-whatsapp',
       handler: sendWhatsAppTask,
     },
   ],
 },
})

Create a Collection (Trigger Point)

You need a collection where event happens (e.g. Orders, Leads, Users).

πŸ‘‰ Example: leads collection

import { CollectionConfig } from 'payload/types'

export const Leads: CollectionConfig = {
  slug: 'leads',

  fields: [
    {
      name: 'name',
      type: 'text',
    },
    {
      name: 'phone',
      type: 'text',
      required: true,
    },
  ],

  hooks: {
    afterChange: [
      async ({ doc, req, operation }) => {
        if (operation === 'create') {
          // create job here
          await req.payload.jobs.queue({
            task: 'send-whatsapp',
            input: {
              phone: doc.phone,
              message: `Hello ${doc.name}, thanks for your interest!`,
            },
          })
        }
      },
    ],
  },
}

Run Worker (VERY IMPORTANT)

Jobs won’t run unless the worker is running.

πŸ‘‰ Start worker

payload jobs:run