|

|  How to Integrate Rasa with Twilio

How to Integrate Rasa with Twilio

January 24, 2025

Learn how to seamlessly integrate Rasa with Twilio to enhance your chatbot capabilities. Follow our step-by-step guide for a smooth integration process.

How to Connect Rasa to Twilio: a Simple Guide

 

Set Up Rasa Environment

 

  • Ensure you have Rasa installed. You can install it using pip if not already done:

 

pip install rasa

 

  • Create a new Rasa project (if you haven't). Use following command in your terminal:

 

rasa init

 

  • Navigate to your Rasa project directory:

 

cd your_project_name

 

Set Up Twilio Account

 

  • Go to the Twilio website and create an account if you don't have one.
  •  

  • Get your Account SID and Auth Token from the Twilio Console. You will need these for authentication.
  •  

  • Buy a phone number within Twilio which will be used for sending and receiving messages.

 

Install Twilio Python SDK

 

  • Install the Twilio Python client which will be used to interact with Twilio services:

 

pip install twilio

 

Create Custom Twilio Channel for Rasa

 

  • Create a new file under `your_project_name` named `twilio_channel.py`.
  •  

  • Add the following code to initialize your custom Twilio input channel:

 

from twilio.rest import Client
from rasa.core.channels.channel import InputChannel, UserMessage
from sanic import Blueprint, response
import json

class TwilioInput(InputChannel):
    def name(self) -> str:
        return "twilio"
        
    def blueprint(self, on_new_message):
        twilio_webhook = Blueprint("twilio_webhook", __name__)
        
        @twilio_webhook.route("/", methods=["POST"])
        async def receive_message(request):
            sender = request.json.get("From")
            message = request.json.get("Body")
            
            if message:
                await on_new_message(UserMessage(message, self.get_output_channel(), sender))
            
            return response.text("Message received")
        
        return twilio_webhook

 

Configure Rasa to Use Twilio Channel

 

  • Edit your Rasa project’s `credentials.yml` file to include Twilio channel custom configuration:

 

twilio_channel.TwilioInput:

 

Run Your Action Server

 

  • Start your Rasa actions server if you have custom actions defined:

 

rasa run actions

 

Run Rasa Server with Twilio Channel

 

  • Run the Rasa server with the custom Twilio input channel by specifying the `endpoint.yml` and `credentials.yml` files:

 

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

 

Configure Twilio Webhook

 

  • Login to your Twilio console and navigate to the phone numbers section.
  •  

  • Select the number you're using for your bot, then scroll to the Messaging section.
  •  

  • Set the "A Message Comes In" webhook URL to point to your Rasa server's public URL with `/webhooks/twilio/webhook` appended. e.g., `https://your-custom-url.com/webhooks/twilio/webhook`.

 

Test Your Bot

 

  • Send a message to your Twilio phone number and observe your Rasa bot interact through your terminal logs and respond accordingly.

 

This should effectively integrate Rasa with Twilio, allowing your Rasa bot to send and receive messages through Twilio. Adjust your configurations as necessary depending on specific requirements or additional settings within Twilio.

Omi Necklace

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

Build and test with your own Omi.

How to Use Rasa with Twilio: Usecases

 

Use Case: Intelligent Customer Support Chatbot

 

  • Integrate Rasa, an open-source machine learning framework for building conversational AI, with Twilio, a cloud communications platform, to deploy an intelligent customer support chatbot.
  •  

  • Utilize Rasa to design an advanced conversational model that understands and processes user intent efficiently.
  •  

  • Employ Twilio's API to send and receive messages, enabling communication with users through SMS, WhatsApp, or other messaging services.
  •  

  • Set up Rasa on your server and configure a webhook to connect it with Twilio's messaging interface.
  •  

  • Develop a conversational flow using Rasa's dialogue management tools, formulating responses that could include FAQs, troubleshooting guides, or informational queries.
  •  

  • Leverage Rasa's NLU capabilities to adapt to varied customer inputs and improve over time through machine learning feedback loops.
  •  

  • Ensure compliance with privacy and data protection regulations by securing the communication channels and handling sensitive user information appropriately.
  •  

 

Code Example: Integrating Rasa with Twilio

 

from twilio.rest import Client  
from rasa_core.channels.twilio import TwilioInput  
from rasa_core.agent import Agent

# Twilio credentials  
account_sid = 'your_account_sid'  
auth_token = 'your_auth_token'  

# Initialize the Twilio client  
twilio_client = Client(account_sid, auth_token)  

# Load your trained Rasa model  
agent = Agent.load("path_to_your_model")  

# Create the Twilio input channel  
input_channel = TwilioInput(  
    account_sid=account_sid,  
    auth_token=auth_token,  
    phone_number="your_twilio_phone_number"  
)

# Start the Rasa server  
agent.handle_channels([input_channel], 5004, serve_forever=True)  

 

Advantages

 

  • Seamless communication with customers through preferred messaging platforms enhances customer satisfaction.
  •  

  • Customizable interactions ensure the chatbot can adapt to a variety of business use cases.
  •  

  • Cost-effective solution for businesses to automate routine customer service tasks, freeing human agents for more complex issues.
  •  

 

Use Case: Personalized Healthcare Assistant

 

  • Combine Rasa, an open-source conversational AI framework, with Twilio, to create a personalized healthcare assistant that offers advice, schedules appointments, and provides medication reminders.
  •  

  • Utilize Rasa's capabilities to design a sophisticated dialogue system that processes user queries related to health issues, symptoms, and recommended actions.
  •  

  • Leverage Twilio's robust API to send alerts, reminders, and confirmations through SMS or WhatsApp, ensuring patients receive timely healthcare updates.
  •  

  • Deploy Rasa on a secure server and establish a webhook for seamless integration with Twilio's messaging services.
  •  

  • Craft personalized interactions by using Rasa's dialogue management features, allowing the assistant to provide tailored recommendations and manage appointment bookings.
  •  

  • Enhance flexibility by training Rasa's NLU component to recognize diverse patterns in patient input, supporting ongoing improvement via feedback and learning loops.
  •  

  • Ensure strict adherence to healthcare privacy regulations, maintaining confidentiality and security of all patient communications through encrypted channels.
  •  

 

Code Example: Integrating Rasa with Twilio for Healthcare

 

from twilio.rest import Client
from rasa_core.channels.twilio import TwilioInput
from rasa_core.agent import Agent

# Twilio credentials  
account_sid = 'your_account_sid'  
auth_token = 'your_auth_token'  

# Initialize the Twilio client  
twilio_client = Client(account_sid, auth_token)  

# Load your trained Rasa model  
agent = Agent.load("path_to_your_model")  

# Create the Twilio input channel  
input_channel = TwilioInput(  
    account_sid=account_sid,  
    auth_token=auth_token,  
    phone_number="your_twilio_phone_number"  
)

# Start the Rasa server  
agent.handle_channels([input_channel], 5004, serve_forever=True)  

 

Advantages

 

  • Improves patient engagement by facilitating easy and continuous communication through preferred messaging services.
  •  

  • Offers customizable pathways in patient interactions, thus adapting to unique healthcare needs efficiently.
  •  

  • Streamlines administrative tasks such as appointment handling and medication reminders, granting healthcare professionals more time to focus on critical patient care.
  •  

Omi App

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

Github →

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