Technology is advancing and updating every minute so the trending technology is changing too. In this era, where every human being attract towards the trending technology the fear of lacking behind is also there. So, I am back again with a very interesting topic to keep you up-to-date and connected to the most demanding and trending technology. Even though the lockdown is going on across the world due to the pandemic of coronavirus we are still in contact with our loved ones and colleagues.
Facebook is one of them that keeps us connected across the world to everyone both for the business as well as personal use. In this blog, we are going to talk about the Facebook messenger and also we will cover that how you can automate your Facebook business page with the AI-based chatbot that is capable of talking to anyone with respect to your business model.
In this blog, I’ll cover how to integrate rasa chatbot to your Facebook messenger to automate your Facebook page. Also to explain it to you in more detail I’ll take an example where your chatbot is capable of telling the live corona virus cases in India and also it is capable of taking a corona assessment test for you. This blog is divided into three parts:
1. Building your first chatbot that is capable of taking self assesment test
2. Web scraping based live corona virus cases crawler
3. Integrating rasa chatbot with facebook messenger
Now, let’s start with the first step of building your first chatbot that is capable to take your self-assessment test.
Building your first chatbot
Like in one of our previous blog you have learned how to build your own chatbot using rasa. Now, it’s time to customize your bot for taking a self-assessment test for coronavirus. After building your chatbot it will look like this.
For now in this chatbot we are considering the case when you are not having corona virus are you corona negative. As per that case this chatbot is made but you can further modify it as per the reply given by the user and accordingly you can update the stories. To get the full code of the following chatbot check this link:
To understand the full functionality and building of your rasa chatbot you can check this video:
Web scraping based live corona virus cases crawler
Secondly, we will add the web scraping feature that will crawl the live corona cases in india so that when the bot will be asked for the live update the bot will reply with the active, recovered and deseased corona cases in reply so that you don’t have to check it manually. For crawling the live data we will use the url below and will apply the web scraping on it.
For the web crawling we will use the requests and beautifulsoup package that I’ll be using in this blog. Here is the code for web crawling:
import requests from bs4 import BeautifulSoup url="https://www.worldometers.info/coronavirus/country/india/" def Corona_cases(url): page = requests.get(url) print(page) soup = BeautifulSoup(page.text, "html.parser") pew = soup.findAll(['span']) data= for subs in pew: if subs.get_text()!='[source]': if subs.get_text()!='': if '\n'not in subs.get_text(): if not any([i in subs.get_text() for i in [i for i in 'aeiou']]): data.append(subs.get_text()) return data if __name__=="__main__": out=Corona_cases(url) print(out)
This is very simple program where we are using get request to fetch the data from the above url with the command,
page = requests.get(url)
after that, we will extract the text with respect to the HTML parser and extracted the required information with respect to the findall attribute,
soup = BeautifulSoup(page.text, "html.parser")
pew = soup.findAll(['span'])
You can also understand this clearly for the video given below.
Integrating rasa chatbot with facebook messenger
Lastly, we have to integrate the rasa chatbot with the Facebook messenger. For that, we need to set up the Facebook page where we can integrate our chatbot that can be used for talking to the followers of your Facebook page.
For Integrating rasa chatbot you need to update the credentials.yml file in your rasa project. There you have to update the following,
facebook: verify: "rasa-bot" secret: "3e34709d01ea89032asdebfe5a74518" page-access-token:"EAAbHPa7H9rEBAAuFk4Q3gPKbDedQnx4djJJ1JmQ7CAqO4iJKrQcNT0wtD"
So, to get these secret and access token we must first create the application for Facebook.
Step 1. To start creating, just go to this link and then click on the create app and then follow the instruction to name your application.
Step 2. Now go to the dashboard and search for the messenger and click on setup.
Step 3. Now you are on the setup page for your messenger app which will be integrated with your rasa chatbot. To integrate it with rasa chatbot you need the access token for that scroll down and search for the access tokens section and clink on “Create New Page” if you don’t have any or click on “Add or Remove Pages” if you already have a facebook page setup with you. In my case I already have a facebook page so I’ll choose the “Add or Remove Pages” option and will select the page where I want to integrate the rasa chatbot for automation. After you have choosen the page and it is linked successfully you will get a page like this.
Step 4. Now click on Generate Token, copy this token and paste it in your credentials.yml file against the “page-access-token”,
page-access-token:"<paste the access token here>"
Step 5. Now go to the Settings > Basic, on this page you will find the “App Secret” just click on show and inter the facebook password to view it. Copy this secret code and paste it in your credentials.yml file against the “secret”,
secret: "<paste the App secret code here>"
Step 6. Under products go to Messenger > Settings, Now scroll down and search for Webhooks and click on “Add Callback URL”. Here you will get a pop-up window asking you for the callback URL and Verify Token. In verify token, type the App Name that you have assigned. In place of callback URL place webhook in such a way,
Now you have two options if you run your own website and have a domain name then use that or use ngrok for now. And if you are new to ngrok then check this video,
or else replace the ngrok https:// URL in the above webhook URL. When your webhook URL is ready just copy and paste it in the Callback URL. Before click on verify and save open the terminal for your project and run the command “rasa run”. When this command is executed then click on verify and save. Once you have setup the webhook and verified you will see the something like this.
Now click on “Add Subscriptions” and select at least the messaging and messaging_postback subscriptions.
Step 7. Now go to the messenger in your smart phone/laptop, search for your facebook page name and start talking to your bot. See the demo here.
For more understanding and practical implementation check this video as well.
Now, I hope everthing is understood and now you can apply this into your bussiness model to increase your reach. For any queries related to the topic feel free to leave a comment below. For more such useful contents check other blogs as well.
Stay connected and Happy learning