Rasa WhatsApp Integration: Step-by-Step Guide
In this article
You can integrate Rasa with WhatsApp by connecting your Rasa server to the WhatsApp Business API through a channel such as Twilio or directly via Meta's Cloud API. This lets your Rasa assistant handle real WhatsApp conversations, sending and receiving messages through the official Business API. Before you start, you need a running Rasa server, a WhatsApp Business Account (or a Twilio account with WhatsApp enabled), and a publicly reachable endpoint for webhooks. The exact menu labels in Meta's or Twilio's dashboards can differ slightly by version and region, so follow the closest match.
What You'll Need
- A working Rasa project with a trained model and a running Rasa server (locally or on a server).
- Python and pip installed, plus the ability to install additional packages.
- A WhatsApp Business Account (WABA) via Meta, or a Twilio account with WhatsApp enabled.
- A phone number approved for WhatsApp Business (or a Twilio sandbox number for testing).
- A public HTTPS endpoint for your Rasa webhook (e.g., using ngrok for local testing or a cloud server).
- Basic familiarity with editing YAML files and running terminal commands.
Step-by-Step
- Choose your connection method. You can use Twilio's WhatsApp channel (easier for testing) or Meta's WhatsApp Cloud API directly. This guide covers both, starting with Twilio because it requires less setup.
- Set up a Twilio account and WhatsApp sandbox (for testing). Sign up at Twilio, go to the WhatsApp sandbox in the console, and follow the instructions to join the sandbox by sending a code from your phone. Note your Account SID and Auth Token.
- Install the Rasa channel package. In your Rasa project, install the Twilio channel for Rasa:
pip install rasa-twilio(or use the built-in Twilio channel if your Rasa version includes it). Check your Rasa version's documentation for the exact package name. - Configure credentials.yml. Add the Twilio credentials to your
credentials.ymlfile. For the built-in Twilio channel, it looks like:
Replace with your actual values. If using a custom channel, follow its documentation.twilio: account_sid: "your_account_sid" auth_token: "your_auth_token" twilio_number: "+1234567890" - Expose your Rasa server publicly. For local testing, run ngrok:
ngrok http 5005. Note the HTTPS URL it gives you. Your Rasa webhook endpoint will behttps://your-ngrok-url/webhooks/twilio/webhook(for Twilio) or the appropriate path for your channel. - Configure the webhook in Twilio. In the Twilio console, go to your WhatsApp sandbox settings and set the "When a message comes in" webhook to your Rasa endpoint (e.g.,
https://your-ngrok-url/webhooks/twilio/webhook) with HTTP POST. Save. - Start your Rasa server. Run
rasa run --credentials credentials.yml --enable-api(andrasa run actionsif you use custom actions). Ensure it's listening on the port you exposed. - Test the connection. Send a WhatsApp message to your Twilio sandbox number. You should receive a response from your Rasa assistant. If not, check the Rasa logs for errors.
- For production, use Meta's WhatsApp Cloud API. Create a Meta developer app, add the WhatsApp product, and get a permanent access token and phone number ID. Set up a webhook URL pointing to your Rasa server (you may need a custom connector or middleware to translate between Meta's webhook format and Rasa). Meta's webhook verification requires a verify token; handle the GET request accordingly.
- Implement a custom connector if needed. If Rasa doesn't have a built-in connector for Meta's Cloud API, you can write a custom channel in Python that receives webhooks and calls Rasa's HTTP API. This involves creating a class that extends
InputChannelandOutputChannel, and registering it incredentials.yml. - Handle message formatting. WhatsApp supports text, media, and interactive messages. Ensure your Rasa responses are compatible; for example, buttons and quick replies may need to be sent as interactive messages via the API. Adjust your custom actions or response templates accordingly.
- Deploy and monitor. Move from ngrok to a production server with a valid SSL certificate. Set up logging and error monitoring for both Rasa and the WhatsApp API to catch issues early.
Common Mistakes
- Using an HTTP webhook instead of HTTPS; WhatsApp requires HTTPS for webhooks.
- Forgetting to enable the API in Rasa (
--enable-api) so the webhook endpoint isn't available. - Incorrect credentials in
credentials.yml(e.g., typos in SID or token). - Not handling the webhook verification challenge for Meta's Cloud API, causing setup to fail.
- Assuming the Twilio sandbox is for production; it's only for testing and has limitations.
- Ignoring message size limits or unsupported message types, leading to delivery failures.
Tips
- Use environment variables for sensitive credentials instead of hardcoding them.
- Test with the Twilio sandbox first to validate your Rasa logic before moving to Meta's API.
- Keep your Rasa version and channel packages up to date for compatibility.
- Log incoming and outgoing messages during development to debug issues.
- Consider using Rasa X or a similar tool for easier conversation review and improvement.
Troubleshooting
If your assistant doesn't respond, check the following in order:
- Verify your Rasa server is running and accessible from the internet (try opening the webhook URL in a browser; it should return a 405 or similar, not a connection error).
- Check the Rasa logs for incoming requests; if none appear, the webhook isn't reaching your server. Recheck the URL in Twilio or Meta.
- Ensure your credentials are correct and the channel is properly configured in
credentials.yml. - If using ngrok, confirm the tunnel is active and the URL hasn't changed (free ngrok URLs change on restart).
- For Meta's Cloud API, ensure your webhook is verified and subscribed to the correct events (messages).
- Check for errors in the Twilio or Meta console; they often provide detailed failure reasons.
- If messages are received but not understood, review your Rasa NLU training data and stories.
Frequently Asked Questions
Can I use Rasa with WhatsApp without a Business Account?
No, WhatsApp requires a Business Account (via Meta or Twilio) to use the API. The Twilio sandbox provides a temporary testing number but still operates under Twilio's WhatsApp Business setup.
Do I need a custom connector for Meta's WhatsApp Cloud API?
Rasa does not have a built-in connector for Meta's Cloud API as of recent versions, so you typically need to write a custom connector or use a middleware service.
Is the Twilio sandbox suitable for production?
No, the sandbox is for testing only. For production, you need a Twilio phone number approved for WhatsApp or use Meta's Cloud API directly.
How do I handle media messages (images, documents) in Rasa?
You need to extend your custom connector to download media from WhatsApp and pass the URL or file to Rasa. Rasa's built-in channels may not support media out of the box.
What is the typical latency for WhatsApp messages through Rasa?
Latency depends on your server and network, but usually responses arrive within a few seconds. Ensure your server has enough resources and is geographically close to your users.
Can I use Rasa with WhatsApp on multiple phone numbers?
Yes, you can configure multiple numbers in your channel settings, but each number must be registered with WhatsApp Business. You may need to handle routing in your custom connector.