Mapping from WhatsApp to Botpress
Content mapping for the official WhatsApp integration.
Overview
In Botpress, you can read data sent from a user’s WhatsApp message using the event
’s properties. This is useful if you want to store user-provided data and access it later.
Here’s a breakdown of how data from WhatsApp is mapped to Botpress:
WhatsApp Message Type | Botpress Type | How to Read |
---|---|---|
Text messages | text | event.preview or event.payload |
Choices from interactive buttons or lists | text | event.preview or event.payload |
Image messages | image | event.payload |
Sticker messages | image | event.payload |
Audio messages | audio | event.payload |
Video messages | video | event.payload |
Document messages | file | event.payload |
Location messages | location | event.payload |
Read data from a payload
To read data from event.payload
:
- Add a Wait for User Input Card to the Node where you want to prompt the user.
- After that Card, check that
event.type
is equal to the data type you’re expecting from the user. - Read
event.payload
.
The payload’s properties depend on the data type:
Text
Text
Image
Image
The URL of the image file
Audio
Audio
The URL of the audio file
Video
Video
The URL of the video file
Get raw file contents
By default, any media sent by a user is automatically uploaded to the Botpress Files API. This means you can read the raw file contents directly from the payload’s URL.
For example, if you prompt the user to send an image, you can read {{event.payload.imageUrl}}
to get the image.
You can also set an expiry time for downloaded files — just update the Downloaded Media Expiry field in the Configuration menu.
From WhatsApp API
If you’d rather get file contents from a WhatsApp media URL, you can disable Download Media in the Configuration menu. When disabled, you’ll have to authenticate with the WhatsApp API to get files.
Get raw file content from the WhatsApp API
Get raw file content from the WhatsApp API
Set a configuration variable
- In your dashboard, navigate to your bot’s Configuration Variables section in the left sidebar.
- Create a Configuration Variable named
WHATSAPP_ACCESS_TOKEN
. - Set its value to your WhatsApp access token.
Make a `GET` request
Now you can make an HTTP GET
request to get the raw file data:
Be sure to:
-
Replace
<URL>
with the actual URL provided in the payload -
Pass
whatsappAccessToken
as aBearer
token in the HTTPAuthorization
headerFor more information, check the WhatsApp documentation on downloading media files.
Tags
You can read event
tags to get information about the current WhatsApp user and conversation:
Phone number
To get the user’s phone number:
event.tags.conversation['whatsapp:userPhone']
This number contains the country code. It has no spaces, dashes (-) or symbols.
Phone number ID
To get the conversation’s WhatsApp phone number ID:
event.tags.conversation['whatsapp:botPhoneNumberId']
This is useful if you have multiple phone number IDs pointing to the same bot.
Check if message is reply
To check if a message was a reply to another:
event.tags.message['whatsapp:replyTo']
If the message is a reply, the tag’s value will be the Botpress messageId
of the quoted message.