|

|  How to Integrate Rasa with WhatsApp

How to Integrate Rasa with WhatsApp

January 24, 2025

Learn to seamlessly connect Rasa with WhatsApp. Follow our step-by-step guide to enhance user communication through powerful chatbot integration.

How to Connect Rasa to WhatsApp: a Simple Guide

 

Set Up WhatsApp Business API

 

  • Sign up for the Twilio WhatsApp Business API.
  •  

  • After signing up, you must configure your phone number and create a Twilio project. Twilio provides a sandbox environment to test WhatsApp integration.
  •  

  • Follow the instructions in the Twilio Console to link your WhatsApp business number and set up the necessary webhook URLs.

 

Install Rasa

 

  • Ensure that Python and pip are installed on your system. Then, install Rasa using the following command:
  •  

    pip install rasa
    

     

  • Verify the installation by checking Rasa's version:
  •  

    rasa --version
    

 

Create a Rasa Project

 

  • Initialize a new Rasa project by running the following command and following the prompts:
  •  

    rasa init
    

     

  • This command creates a Rasa project structure in your directory with starter files configuration, training data, and more.

 

Configure Rasa for Twilio

 

  • In your Rasa project directory, locate the `credentials.yml` file. This is where you will add your Twilio credentials.
  •  

  • Add the following entries to `credentials.yml` to configure Twilio, replacing placeholder values with your Twilio account details:
  •  

    twilio:
      account_sid: "your_twilio_account_sid"
      auth_token: "your_twilio_auth_token"
      twilio_number: "your_twilio_whatsapp_number"
    

 

Update Your Rasa Domain

 

  • Open the `domain.yml` file in your Rasa project. Make sure you define the intents and responses necessary for your bot to handle.
  •  

  • Update the `responses` section to include the messages you want to send back to users over WhatsApp.

 

Implement Twilio Webhook

 

  • Create a new Python file in your Rasa directory called `twilio_connector.py`, which will manage messages between Rasa and Twilio.
  •  

  • Within the `twilio_connector.py`, implement custom logic for Twilio webhook integration. An example snippet to get started:
  •  

    from twilio.rest import Client
    from flask import Flask, request
    from twilio.twiml.messaging_response import MessagingResponse
    
    app = Flask(__name__)
    
    @app.route('/webhook', methods=['POST'])
    def whatsapp():
        incoming_msg = request.values.get('Body', '').lower()
        # Define your bot's logic here
        resp = MessagingResponse()
        reply = resp.message()
        reply.body('Hello from Rasa with WhatsApp!')
        return str(resp)
    

     

  • Make sure your Flask app is running when testing the bot with Twilio.

 

Run Rasa and Test the Integration

 

  • Start your Rasa server using the following command:
  •  

    rasa run --enable-api
    

     

  • In a separate terminal, start the action server if you have custom actions:
  •  

    rasa run actions
    

     

  • Use a tool like ngrok to expose your localhost webhook to the internet for Twilio to access:
  •  

    ngrok http 5005
    

     

  • Update your Twilio Console webhook URL with the ngrok provided URL.
  •  

  • Send a message to your Twilio WhatsApp number from WhatsApp, and observe the interaction with your Rasa bot.

 

Troubleshooting and Optimization

 

  • If messages aren't flowing through, check your endpoint configurations in Twilio Console and ensure your credentials are correct.
  •  

  • Use logs from Rasa and your Twilio webhook to debug any issues.
  •  

  • Optimize response times by ensuring the server hosting your Rasa instance is sufficiently provisioned for your expected load.

 

Omi Necklace

The #1 Open Source AI necklace: Experiment with how you capture and manage conversations.

Build and test with your own Omi Dev Kit 2.

How to Use Rasa with WhatsApp: Usecases

 

Use Rasa and WhatsApp for a Virtual Travel Assistant

 

  • Leverage Rasa's natural language understanding capabilities to create an intelligent travel assistant capable of understanding and responding to travel-related queries.
  •  

  • Integrate this assistant with WhatsApp to provide users with an accessible and familiar interface for interacting with the assistant from their smartphones.
  •  

  • Utilize WhatsApp's rich media messaging to send users travel itinerary PDFs, images of travel destinations, and links to relevant travel resources.
  •  

  • Enhance the travel assistant by implementing Rasa's machine learning capabilities to dynamically learn user preferences and offer personalized travel recommendations.

 

Handle User Queries with Rasa

 

  • Set up diverse intents and entities within Rasa to handle queries such as flight searches, hotel bookings, itinerary modifications, and local attraction suggestions.
  •  

  • Employ Rasa's dialogue management to maintain a natural and contextually-aware conversation flow while responding to user inquiries.

 

Deploy Rasa on WhatsApp

 

  • Set up a connection between Rasa and WhatsApp using a reliable messaging service API, such as Twilio or Vonage, to enable seamless message exchange.
  •  

  • Implement security checks and user authentication to ensure that interactions over WhatsApp are secure and user privacy is maintained.

 

Example of WhatsApp Deployment Code

 


from twilio.rest import Client

account_sid = 'your_account_sid'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

message = client.messages.create(
    from_='whatsapp:+14155238886',
    body='Hello from your travel assistant!',
    to='whatsapp:+1234567890'
)

 

Enhance User Experience

 

  • Incorporate multilingual support to cater to a global audience, using Rasa's language models for language understanding.
  •  

  • Gather user feedback through WhatsApp interactions to continually improve and refine the assistant's capabilities.

 

Monitor and Scale

 

  • Utilize analytics to monitor user interaction patterns and identify areas for improvement in the assistant's response capabilities.
  •  

  • Scale the assistant's deployment to handle multiple concurrent users efficiently by optimizing the Rasa server and message API configurations.

 

 

Use Rasa and WhatsApp for E-commerce Customer Support

 

  • Integrate Rasa's NLU capabilities to develop a responsive virtual assistant dedicated to handling customer support queries in the e-commerce sector.
  •  

  • Leverage WhatsApp's platform to provide a seamless interface, allowing customers to easily reach out for inquiries regarding product availability, order status, and return policies.
  •  

  • Utilize WhatsApp's interactive message templates to guide users through various support options, providing quick access to frequently asked questions and support resources.
  •  

  • Improve the assistant with Rasa's machine learning tools to identify customer sentiment and tailor responses accordingly, ensuring enhanced customer satisfaction.

 

Manage Customer Inquiries Effectively

 

  • Configure intents and entities in Rasa to manage a spectrum of common customer queries, such as payment issues, product details, and shipping information.
  •  

  • Apply Rasa's dialogue management features to sustain a coherent and intuitive conversation flow while handling user questions and concerns.

 

Deploy Rasa on WhatsApp

 

  • Establish connectivity between Rasa and WhatsApp via a trustworthy messaging service API, such as Twilio or Vonage, to facilitate efficient communication.
  •  

  • Implement user authentication mechanisms and data privacy protocols to ensure secure interactions over the WhatsApp platform.

 

Sample WhatsApp Deployment Code

 

from twilio.rest import Client

account_sid = 'your_account_sid'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

message = client.messages.create(
    from_='whatsapp:+14155238886',
    body='Welcome to our e-commerce support!',
    to='whatsapp:+1234567890'
)

 

Optimize User Experience

 

  • Add support for multiple languages to serve a diverse customer base, leveraging Rasa's language models for enhanced language processing.
  •  

  • Encourage customer feedback through WhatsApp interactions to continually evolve and enhance the assistant’s effectiveness.

 

Monitor Performance and Scale Efficiently

 

  • Use analytics tools to track customer interaction trends and identify possible advancements in the assistant’s problem-solving capabilities.
  •  

  • Adjust the assistant's deployment to accommodate numerous users simultaneously by optimizing the Rasa server and messaging API setups.

 

Omi App

Fully Open-Source AI wearable app: build and use reminders, meeting summaries, task suggestions and more. All in one simple app.

Github →

Order Friend Dev Kit

Open-source AI wearable
Build using the power of recall

Order Now

Troubleshooting Rasa and WhatsApp Integration

How to connect Rasa chatbot to WhatsApp?

 

Connect Rasa Chatbot to WhatsApp

 

  • Set Up Rasa: Ensure your Rasa setup is complete and running. Test your Rasa server locally to confirm it's working correctly.
  •  

  • Use Twilio WhatsApp API: Sign up for a Twilio account, and activate the WhatsApp sandbox in the console. Note down the provided credentials: Account SID and Auth Token.
  •  

  • Install Dependencies: Create a Python script to handle the integration. Install dependencies using the shell command:
  •  

    pip install twilio
    

     

  • Create Integration Script: Write a Python script to forward messages between WhatsApp and Rasa:
  •  

    from twilio.rest import Client
    client = Client('ACXXXXXXXXXXXXXXXX', 'your_auth_token')
    
    @app.route("/sms", methods=['POST'])
    def sms_reply():
        msg = request.form.get('Body')
        user_number = request.form.get('From')
        # Connect to Rasa
        rasa_response = requests.post('http://localhost:5005/webhooks/rest/webhook', json={"message": msg})
        for response in rasa_response.json():
            client.messages.create(
                from_='whatsapp:+14155238886', 
                body=response.get('text'), 
                to=user_number
            )
    

Why is my Rasa bot not responding on WhatsApp?

 

Possible Reasons Your Rasa Bot Isn't Responding

 

  • Incorrect Configuration: Verify that your Rasa bot and WhatsApp configurations are correct. Ensure the integration between Rasa and a service like Twilio or Vonage is set up properly.
  •  

  • Token Issues: Confirm that the access tokens and API keys used in your setup are valid and correctly implemented.
  •  

  • Network Issues: Double-check your network settings, as well as firewall permissions that could be blocking connections between Rasa and WhatsApp.
  •  

  • Webhook Problems: Make sure the webhook URL is accurately configured in your WhatsApp API dashboard and reachable externally.
  •  

  • Server Logs: Inspect Rasa server logs for errors that might indicate issues with message receiving or processing.
  •  

  • Message Format: Ensure the incoming and outgoing message formats adhere to expected standards, possibly converting them as needed.

 


# Example issue: Incorrect Twilio setup in credentials.yml
twilio:
  account_sid: "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  auth_token: "your_auth_token"
  twilio_number: "+1234567890"

 

Troubleshooting Steps

 

  • Test your bot locally first to ensure general functionalities work correctly.
  •  

  • Use external tools like Postman to test WhatsApp API endpoints independently.

 

How to deploy Rasa with WhatsApp Business API?

 

Setup Rasa

 

  • Install Rasa Open Source by running pip install rasa in your virtual environment.
  •  

  • Create a new project with rasa init.

 

Setup WhatsApp Business API

 

  • Register your phone number with WhatsApp Business and set up your account with a provider like Twilio.
  •  

  • Ensure you have access to your WhatsApp API Client URL and authentication token.

 

Connect Rasa to WhatsApp

 

  • Use a service like Twilio to relay messages from WhatsApp to Rasa. You can use a Flask or FastAPI server to handle incoming messages and send responses from Rasa.
  •  

  • Configure your credentials.yml in Rasa with Twilio details:
    twilio:
      account_sid: "your_account\_sid"
      auth_token: "your_auth\_token"
      phone_number: "your_twilio\_number"
      twilio_whatsapp_number: "whatsapp:+your_whatsapp_number"
        
  •  

  • Deploy Rasa to a server, allowing it to receive webhook requests from Twilio:

 

rasa run -m models --enable-api --cors "*" --debug

 

Testing & Maintenance

 

  • Verify the integration by sending a test message from your WhatsApp account to the Rasa bot via the Twilio Sandbox.
  •  

  • Regularly check logs and monitor message flow to ensure smooth operation.

 

Don’t let questions slow you down—experience true productivity with the AI Necklace. With Omi, you can have the power of AI wherever you go—summarize ideas, get reminders, and prep for your next project effortlessly.

Order Now

Join the #1 open-source AI wearable community

Build faster and better with 3900+ community members on Omi Discord

Participate in hackathons to expand the Omi platform and win prizes

Participate in hackathons to expand the Omi platform and win prizes

Get cash bounties, free Omi devices and priority access by taking part in community activities

Join our Discord → 

OMI NECKLACE + OMI APP
First & only open-source AI wearable platform

a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded

OMI NECKLACE: DEV KIT
Order your Omi Dev Kit 2 now and create your use cases

Omi Dev Kit 2

Endless customization

OMI DEV KIT 2

$69.99

Make your life more fun with your AI wearable clone. It gives you thoughts, personalized feedback and becomes your second brain to discuss your thoughts and feelings. Available on iOS and Android.

Your Omi will seamlessly sync with your existing omi persona, giving you a full clone of yourself – with limitless potential for use cases:

  • Real-time conversation transcription and processing;
  • Develop your own use cases for fun and productivity;
  • Hundreds of community apps to make use of your Omi Persona and conversations.

Learn more

Omi Dev Kit 2: build at a new level

Key Specs

OMI DEV KIT

OMI DEV KIT 2

Microphone

Yes

Yes

Battery

4 days (250mAH)

2 days (250mAH)

On-board memory (works without phone)

No

Yes

Speaker

No

Yes

Programmable button

No

Yes

Estimated Delivery 

-

1 week

What people say

“Helping with MEMORY,

COMMUNICATION

with business/life partner,

capturing IDEAS, and solving for

a hearing CHALLENGE."

Nathan Sudds

“I wish I had this device

last summer

to RECORD

A CONVERSATION."

Chris Y.

“Fixed my ADHD and

helped me stay

organized."

David Nigh

OMI NECKLACE: DEV KIT
Take your brain to the next level

LATEST NEWS
Follow and be first in the know

Latest news
FOLLOW AND BE FIRST IN THE KNOW

thought to action

team@basedhardware.com

company

careers

events

invest

privacy

products

omi

omi dev kit

personas

resources

apps

bounties

affiliate

docs

github

help