18/12/2018
Deploying Chatbots On Facebook
by Sumit Raj
In this post you will learn to deploy your chatbot on Facebook. Let’s begin by deploying our chatbot using Heroku in the cloud. Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud. A key benefit of Heroku is that you can easily get your app running on https without much pain. You don’t need buy SSL certificates while you are learning and testing our chatbots. The reason why https is required is because some platforms like Facebook do not allow developers to use non-https URLs as callback URLs.
We’ll be following steps to successfully deploy our chatbot as a web service in Cloud. Once we have done that, it will be much easier to integrate it with different platforms (like Facebook.) So, let’s begin.
Creating an app on Heroku
Sign up on Heroku and create an app. Name it [something]-actions, as this is going to be our actions server app. Have a look at the screenshot in fig. 1 below where you can give a unique name for your actions server, which should be available on Heroku. Once you secure your name, you can click on the "Create app" button to create the actions server app.
Feel free to name it anything you want if your name is not available but always try to give meaningful names.
fig. 1. Creating the action server app on Heroku with name horoscopebot1212-actions
Setting up Heroku on your local system
Install Heroku CLI on your local operating system. Refer to this link. If you are on MacOS, use the below command.
brew install heroku/brew/heroku
Creating and setting up an app on Facebook
To be able to deploy our chatbot on Facebook, we first need to have credentials in the Facebook app. In order to get the Facebook credentials, we need to set up a Facebook app and a Page.
1. Go to https://developers.facebook.com/ and create an app if you don’t have one already. Enter the desired details and then click on "Create App ID".
fig. 2. Creating app on Facebook for developers
2. Once your app is created, select "Basic" under Settings, and click on the "Show" button under App Secret. This is your fb_secret.
fig. 3. Getting App Secret from Facebook's App
3. Go to the dashboard for the app and scroll down to “Add a Product”. Find the Messenger section and click "Set Up".
fig. 4. Adding Messenger as product to Facebook app
4. Scroll down to the Token Generation section, where you will get a link to create a new page for your app. (If you have a Page already, simply select it from the dropdown.) The “Page Access Token” is your fb_access_token here.
fig. 5. Generating token for Facebook Messenger App
5. After the Token Generation section, scroll to the Webhooks section and click on “Setup Webhooks”.
fig. 6. Setting up Facebook Webhooks
6. Next, select a verify token. A verify token can be any random string. This will be your fb_verify. For now, leave the Callback URL section blank as is. Don’t close your browser - we’ll come back here again.
fig. 7. Adding verify token to Facebook Webhook setup
7. Keep your fb_verify, fb_secret and fb_access_token handy to connect your bot to Facebook.
Creating and deploying Rasa actions server app on Heroku
In this step we are going to use our actions from our Heroku app for Rasa's actions server. We need to have two different applications, as we cannot run two web applications in a single Heroku app. Go to your command line and execute the below set of commands from your project directory.
Create a new folder called actions_app and get into the directory:
mkdir actions_app
cd actions_app
Copy your actions.py from main project directory to actions_app directory
Create a requirements.txt file with the below contents. Use requirements.txt - it will tell the Heroku app to install the packages with their versions.
rasa-core-sdk==0.11.0
requests==2.18.4
Create a file named Procfile with the below contents. Procfile how Heroku understands what to do in order to crank up the applications.
web: python -m rasa_core_sdk.endpoint --actions actions --port $PORT
Run the below set of commands
$ heroku login
$ git init
$ heroku git:remote -a <your-heroku-app-name>
$ heroku buildpacks:set heroku/python
$ heroku config:set PORT=5055
$ git add .
$ git commit -am "deploy my bot"
$ git push heroku master
After the last command, Heroku will install all of our packages needed as per the requirements.txt file. If your app is successfully deployed, you should see logs similar to the below:
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 48.3M
remote: -----> Launching...
remote: Released v4
remote: https://horoscopebot1212-actions.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/horoscopebot1212-actions.git
* [new branch] master -> master
At this point, we will verify if our app is responding to public requests. In order to do that, let’s hit the app URL appended by “Webhook.”
The app URL in my case is https://horoscopebot1212-actions.herokuapp.com/, so I’ll check if my action’s server is responding. I go to this URL: https://horoscopebot1212-actions.herokuapp.com/webhook, and as expected, it comes back to me saying that the method not allowed. This means that the app is responding correctly as per the user request.
fig. 8. Verifying Action Server Endpoint
Creating Rasa chatbot API app
In this step we will follow some steps and commands similar to what we just did. However, this is a new app that we’ll create, and it will e our main app for dialog management. First, come back to the main project directory. For example, enter horoscope_bot, create a file name Procfile, and add the below contents to it.)
web: python -m spacy download en && python facebook.py
Creating a standalone script for Facebook Messenger Chatbot
Create a file name facebook.py in the same project directory. The contents of the Python file should reflect the below.
from rasa_core.channels.facebook import FacebookInput
from rasa_core.agent import Agent
from rasa_core.interpreter import RasaNLUInterpreter
import os
from rasa_core.utils import EndpointConfig
# load your trained agent
interpreter = RasaNLUInterpreter("models/nlu/default/horoscopebot/")
MODEL_PATH = "models/dialogue"
action_endpoint = EndpointConfig(url="https://horoscopebot1212-actions.herokuapp.com/webhook")
agent = Agent.load(MODEL_PATH, interpreter=interpreter)
input_channel = FacebookInput(
fb_verify="YOUR_FB_VERIFY_TOKEN",
# you need tell facebook this token, to confirm your URL
fb_secret="YOUR_FB_SECRET", # your app secret
fb_access_token="YOUR_FB_ACCESS_TOKEN"
# token for the page you subscribed to
)
# set serve_forever=False if you want to keep the server running
s = agent.handle_channels([input_channel], int(os.environ.get('PORT', 5004)), serve_forever=True)
Make sure to replace the fb_verify, fb_secret and fb_access_token variable values in the above code with what we kept in Step 3.
Create a new requirements.txt file and add all of the packages with their versions needed for this project. My requirements.txt file looks like the below, but for your project the requirements may differ.
rasa-core==0.11.1
rasa-core-sdk==0.11.0
rasa-nlu==0.13.2
gunicorn==19.9.0
requests==2.18.4
spacy==2.0.11
sklearn-crfsuite==0.3.6
Save the requirements.txt in the project directory again. This is the file Heroku will use to install our packages in the server.
Now, let’s create a new app in Heroku like we did earlier. Go to your Heroku dashboard and create a new app.
fig. 9. Creating Dialogue Management App in Heroku
Once you have created the app, you can now go to your project root directory and run below set of commands from the command line in your project folder.
$ git init
$ heroku git:remote -a <your-heroku-app-name>
$ heroku buildpacks:set heroku/python
$ heroku config:set PORT=5004
$ git add .
$ git commit -am "deploy my bot"
$ git push heroku master
If you get a runtime error after deployment, it may look like the below:
ValueError: You may be trying to read with python 3 a joblib pickle generated with python 2. This feature is not
supported by joblib.
This will happen if you are using the Python2.x version. Heroku uses the Python3.x version by default. So, if you want to use Python2.x, you need to follow the steps below to resolve the error.
- Change Python3.6 to Python-2.7.15.
- To do this, create a file runtime.txt under the root app directory of your project. Open the runtime.txt file and add the line Python-2.7.1 and save it. Heroku will use the above-mentioned Python version only to build your project.
- Once the successful deployment is done, you will see a URL that Heroku gives you that says that the app is deployed to <url>.
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing requirements with pip
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 254M
remote: -----> Launching...
remote: Released v17
remote: https://horoscopebot1212.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/horoscopebot1212.git
cd3eb1b..c0e081d master -> master
This deployment is going to take a little time, so be patient. If you don’t get any error messages, you have successfully deployed your chatbot to Heroku on the cloud to make it work with Facebook Messenger. Now, let’s verify if it works.
Verifying the deployment of dialogue management app on Heroku
To verify if our dialogue management app is successfully deployed on Heroku, we’ll be doing below steps.
1. Take the URL given by Heroku and append the below endpoint to it:
/webhooks/facebook/webhook?hub.verify_token=YOUR_FB_VERIFY_TOKEN&hub.challenge=successfully_verified
Make sure to use the correct Verify Token that you used for Webhooks settings in Facebook.
2. Go to the browser and paste the entire URL. It should return your hub.challenge value back if your hub.verify_token is correct. Your complete URL will appear as below:
https://horoscopebot1212.herokuapp.com/webhooks/facebook/webhook?hub.verify_token=YOUR_FB_VERIFY_TOKEN&hub.challenge=successsfully_verified
If you get the message successfully_verified in the browser, your app is successfully deployed and working.
Integrating Webhook with Facebook
Let’s go back to our Facebook app configuration. Go back and add your callback URL from Step 3. Make sure to check the messages in Subscription Fields. Check the figure below for reference.
fig. 10. Facebook Messenger Webhooks Configuration
Click on “Verify and Save”. Facebook will match the Verify Token using the above URL, meaning our app will only respond to requests who have the correct Verify Token. Once the Verify Token matches the Webhook, the subscription will be activated for our app.
Next, select a Page to subscribe your Webhook to. Then click on "Subscribe".
fig. 11. Subscribe webhook to facebook page events
Now, it is time to test our horoscope bot on Facebook.
Post Deployment Verification - Facebook Chatbot
In normal software development scenarios, people build a product, test it, deploy it, and then do Post Deployment Verification, or PDV. We’ll be doing something similar. This is important because as you learned the chatbot needs to connect to the actions server to respond to some intent requests of the user. PDV is like a sanity test to see that the health of the app is good overall. If you are building a bot that uses 10-15 different vendor’s APIs, then it is a must to check all scenarios where your bot hits the action server and uses the API to return data back to the user.
Go to your Messenger app or Facebook in your computer browser and search for your bot.
Below is what my horoscope bot does and tells me.
fig. 12. Horoscope Bot on Facebook
fig. 13. Horoscope Bot on Facebook
fig. 14. Horoscope Bot on Facebook
Voila! Our first in-house built chatbot app is deployed on the web and can be accessed via the Facebook Messenger platform. Go ahead and share it with your family, friends, colleagues, and the entire world!
About the Author
Sumit Raj is a techie at heart who loves coding and building applications. He is a Python expert with a keen interest in Machine Learning and Natural Language Processing. He believes in the idea of writing code which directly impacts revenue of the company. Sumit has worked in multiple domains like Personal Finance Management, Real-Estate, E-commerce, Revenue Analytics to build multiple scalable applications. He has helped various early age startups with their initial design & architecture of the product which got funded later by investors and governments. He comes with a good experience of cutting-edge technologies used in high-volume internet/enterprise applications for scalability, performance tuning & optimization and cost-reduction. In his free time, he likes to write on his blog and answer questions on Computer Programming, Chatbots, Python/Django, Career Advice & Web Development on Quora having over 1 million views together. Feel free for A2A on his Quora profile. Currently, he is working as Senior Solutions Architect at GeoSpark R&D, Bangalore, India building a developer platform for location tracking. You can get to know more about him from his website. http://sumitraj.in/
This article was contributed by Sumit Raj, author of Building Chatbots with Python.