Webhooks
Message Event
Webhooks
Message Event
Trigger a workflow whenever a WhatsApp message is received in your WhatsApp Business Account
Usage
import { Trigger } from "@trigger.dev/sdk";
import { events } from "@trigger.dev/whatsapp";
new Trigger({
id: "demo",
on: events.messageEvent({
accountId: "<account_id>",
}),
run: async (event, ctx) => {
//your workflow code here
},
}).listen();
Params
accountIdrequired
string
Your WhatsApp Business Account id
Event
messagerequired
object
The message object has the type of message (e.g. text, video, audio etc) and the message content.
contactsrequired
object
The contact information of the sender.
metadatarequired
object
Information about the account and phone number that received the message, i.e. your WhatsApp Business Account details.
Example Workflows
import { Trigger } from "@trigger.dev/sdk";
import { events } from "@trigger.dev/whatsapp";
new Trigger({
id: "demo",
on: events.messageEvent({
accountId: "<account_id>",
}),
run: async (event, ctx) => {
//get the user from your database, using the phone number
const user = await db.getUser({ phoneNumber: event.message.from });
if (!user) {
await ctx.logger.error(`No user to phone number ${event.message.from}`);
return;
}
//post a message to your support Slack channel
await slack.postMessage({
channel: "support",
text: `Support message from ${user.name} (${user.email})\n${event.message.body}`,
});
},
}).listen();
Example Event payload
{
"type": "message",
"message": {
"id": "wamid.ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"from": "12345678901",
"text": {
"body": "The quick brown fox jumps over the lazy dog"
},
"type": "text",
"timestamp": 1675257050000
},
"contacts": [
{
"wa_id": "12345678901",
"profile": {
"name": "Matt Aitken"
}
}
],
"metadata": {
"phone_number_id": "987654321012",
"display_phone_number": "15550676999"
}
}