Thumbnail
WhatsApp Business API | Integration for Rasa
New

Rasa Open Source is a conversational AI framework for building contextual assistants and helps to keep your customers engaged with your business. 

logo_rasa_ct2x

Introduction

Rasa Open Source is a conversational AI framework for building contextual assistants and helps to keep your customers engaged with your business. 


Rasa Open Source includes

  • NLU: determines what the user wants and captures key contextual information
  • Core: selects the next best response or action based on conversation history
  • Channels and integrations: connect assistant to users and backend systems

Rasa X is a toolset used to improve a contextual assistant built using Rasa Open Source. 

We will be using RASA X for this tutorial. 

Prerequisites

Before setup of rasa in your virtual server machine make sure the things are there

  1. Setup a virtual machine (VM).
  2. Python 3 installed with  version 3.6 on this VM.
  3. Pip installed with version ~= 19.1 on this VM.

1. Build an assistant with Rasa

Run the command provided below in your VM’s terminal to setup your rasa core modules in your machine

pip install rasa-x --extra-index-url https://pypi.rasa.com/simple

If its asking for system permission issue try below command. 

pip install rasa-x --extra-index-url https://pypi.rasa.com/simple --user

Once everything is installed successfully, Create a new directory. From terminal, navigate to this new directory and run the below command which will initialize your project setup.

rasa init 

2. Train with Rasa X

Load Rasa X interface on browser

Run the command rasa x which will prompt your browser to open a rasa x interface. Here, you can do interactive training for your bot. Let’s see how.

Note: In the interface if it asks for password, please check your terminal to get the information. Credentials are valid till your server is live.

Create Intents

Follow the steps below to create intents and add training phrases. 

  • In Rasa x interface select the NLU Training in the side menu
  • In the Annotate new data tab click the plus icon to add sentence and click enter to save it.
  • Once you save, it will appear in the list with predicted intent and confidence. 
  • You can create a new intent or change if a wrong intent is predicted.
  • Repeat above steps to create more annotated data for training your model
  • In the Training data tab, it is possible to change the intent for existing training phrases.
  • In Regex & Lookup tab, you can create your own regular expressions.

Create Entities

  • Select a word from the training phrase which you have added in the training data tab.
  • A popup will trigger asking for the entity name you want to attach it. 
  • You can also see the option to create a new entity.
  • Make sure to save after assigning the entities to training phrases.

Bot Responses

Here you need to define responses which would be sent back to the user whenever a corresponding intent is triggered. 

  • Select the Responses tab from the left side of rasa x interface
  • Click the add icon to create a new response.
  • From Create or select a template dropdown, either create a new template or select an existing template. Add the Bot response against this template. 

Activate a Model

  • Click the Train button in the left of rasa x interface after creating intents, entities and responses for your bot.
  • On successful training, a model is generated in the model menu section.
  • On Hover of model list, three dots and delete icon will appear.
  • Click the three dots to make the model as active.

Interactive Training

In interactive learning mode, you train your bot while you are talking to it. This is a powerful way to explore what your bot can do, and the easiest way to fix any mistakes it makes. 

  • Click Talk to your bot menu in the interface. 
  • Click the highlighter icon in the typing section to start interactive training. See the screenshot below. 
  • Type the phrases and select the correct intent in dropdown.
  • Select the next action for that intent. 
  • Repeat the same process for all possible paths. 
  • Once the interactive training is completed, save the stories and train the model with the interactive learning you have done. Make it active.

Test your Bot

  • Click the conversations on the left panel of interface. 
  • You can share your model bot with test user by clicking the share icon.
  • Once you have entered the bot details you can generate the link which is shareable.
  • You can also access all test users’ chat history. 
  • You can provide training for the unhandled training phrases in the annotated tab in NLU training page.

3. Connect Server with Rasa and tyntec

  • Create HTTPS post webhook endpoint
  • Ask tyntec's support team to add the endpoint to your active WhatsApp number. This is for the purpose of receiving the message from WhatsApp to your webhook
  • On receiving the message from whatsapp, make this Api request to send your message to rasa for NLP processing.
URL : { your rasa domain }/webhooks/rest/webhook
Method: POST
Body:    {
"sender":"user id",
    	"message":"your text"
 }

 Response format would be:

[{recipient_id: “user id”, text: “ your response ”}]
  • Lastly, Use the below tyntec API to send response back to Whatsapp user.
URL : https://api.tyntec.com/chat-api/v2/messages
Method: POST
Headers: { “apiKey”:” your api secret Key}
Body:   {
                "to": "user Number",
                "channels": [
                    "whatsapp"
                ],
                "whatsapp": {
                    "from": "your Business Number",
                    "contentType": "text",
                    "text": "Rasa response"
                }
              }