Messenger Quick Replies with Wit.ai Integration
This is Written Tutorial on how you can create the same chatbot with perfect explaination
Facebook app allows us to retrieve and post request as well as post data on facebook via Facebook API.
- Create A Facebook Page thats a Simple Process
- Go To https://developers.facebook.com/
- You will see a Awesome interface where you can login to your facebook account. Login button at the top right besides the search icon . Login Through Your Credentials .
- Click on My Apps which will navigate you to the Create App page .
- So Click on Create App . You will be prompted with 4 options select the one which interests you here we will be using the first one
Manage Business Integration
option, then continue . Enter your app name EG:- test-app. Now select the app purpose. Then Create App . Now complete the security check recaptcha and submit. - Then you will be navigated to a page were you will be prompted to add products to your App in our case it is messenger . Click on Messenger Setup Button. &. Navigated to the next page were you can add and make changes to the settings . Now scroll down towardds the access token . Create a New Page if you don't have else add a page
- Once you login with the credentials you give the test-app the authority yo manage & message to the page . Once completed you will be given your page access token keep it safe . Select the fb page and continue . Click on Generate Token to get your access token .
Requirements
- Python3 Installed
- pip install virtualenv
- create a virtual env and activate it
- pip intall flask, requests, pymessenger, bs4, wit
So we have to set up a webhook for our application were we will be adding a callback url through which we can handle all kinds of requests . When we setup a webhook we need to verify our callback url .
So in the docs provided at the https://developers.facebook.com/ is in the java format we will be doing it using python
- Create a file app.py
- Now import all the dependencies and packages and initilaize your flask app
- Now check the Veriify function in the app.py file through which we will be verifying our callback url. So the word
'hello'
is our callback url token. so when you run the file its runs on the local host for live web interaction we need to take it online for that purpose we need to install ngrok through which you can tunnel up your localhost network . - Visit https://ngrok.com/ and install it once done open the terminal in the file were ngrok is extracted and run the command
ngrok.exe http 80
- Now you can run your app.py and then then url in ngrok provided enter it at developers.facebook.com for webhook callback url and enter the verifcation token i.e
hello
and select the webhook services.
Now we will handle messaging events. So as you see in the app.py we have setup a app route
The Function def webhook():
will be handling all kinds of requests If you want you can event print out the whole request to see how the actually the request is been received
We are actually handling and extracting the data from the json format request and extracting the relevant data
So Now we have to create some other files from were will firstly do some webscrapping and extract the relevant data .
Now you can open the other python files to check what exactly is happening and in which format is exactly the data been stored
For the sharemarket database we will be scrapping data from https://www.moneycontrol.com/ & for crypto-currency https://coinmarketcap.com/ we will be extracting the required data and storing it is a certain format as required and specified in messenger quick replies docs
Open utils.py to get the basic idea how exactly the data is scrapped and filtered out and is stored in the json format
This What the database looks like
Similarly You can check the other python files to see what and how data is filtered out and stored
Now we will be creating and training the bot to analysize the messages firstly open https://wit.ai/ login and create a app and start training it with some local commands and then create entities, entity EG:- price of tcs
Here the entity will be share_market
So go on training to increase the accuracy of your bot
Now open user_response.py
now take the access token and see how the response is been handled