View the Project on GitHub IBM/customized-voice-text-bot-for-whatsapp-telegram
To execute the WhatsApp Bot application on your local machine, kindly follow the subsections below in the specified order.
To locally run the app successfully, please ensure that you have these components ready:
ngrok is a third-party software that can be used to expose "local servers to the public internet over secure tunnels" (ngrok documentation). In this specific context of our chatbot, we use Flask to create the web app that handles all of the messaging between Watson Assistant and WhatsApp users, but by default, Flask starts a local host to deploy the application. ngrok provides a random public url that can forward requests to a local host.
After acquiring the necessary components and cloning our GitHub repository, you would need to create a Python 3 virtual environment (using virtualenv for example), and install the dependencies. Below you can see how to do these steps:
$ python3 -m venv <name_of_virtualenv>
$ source <venv>/bin/activate
C:\> <venv>\Scripts\activate.bat
PS C:\> <venv>\Scripts\Activate.ps1
$ pip install -r requirements_windows_local.txt
$ python3 -m pip install -r requirements.txt
In the src
directory, you should also create a .env file. This will store the API keys and the code will search for them.
$ touch .env
$ type nul > your_file.txt
In the .env file, you can store API keys with these exact names. We do this to protect sensitive information, namely, API keys and other IDs. STT, TTS, Watson Assistant, COS and Cloudant API keys can be found on the IBM Cloud website (by going to Resource List and then opening each service). For the Watson Assistant ID, you first have to launch Watson Assistant (this can be done from the resource list) and create an assistant. After that, under Assistant Settings the assistant ID will be there.
These are the API keys that you will need to run the app:
We have curated a page which contains detailed instructions on the process of creating a Twilio Sandbox account and obtaining the essential API keys. To access the page, kindly follow this link.
The following image is an example of how the .env
file should be formatted:
First, run the command for your respective OS on the command line (in the folder ngrok were installed):
$ ./ngrok http 8080
$ ngrok http 8080
After this, an ngrok interface, similar to this, should appear:
There would be two "Forwarding" sections of the interface - you can copy the https://____.ngrok.io
url.
After completing the previous steps, we are ready to configure Twilio. We have prepared a special page with more information on how to create a Twilio Sandbox account. You can access it by clicking here.
Now, you would open up the Twilio console:
Then navigate to your WhatsApp Sandbox settings:
With the to see a screen just like this one:
Then you should copy and paste your ngrok url in the box "WHEN A MESSAGE COMES IN". Append the path "/chatbot-message" to your ngrok url too - so the final url that goes in that box should be similar to https://____.ngrok.io/chatbot-message
. All other settings can be left as the default.
Now, in the src/
folder:
$ gunicorn -b :8080 whatsapp:app
$ waitress-serve --listen=*:8080 whatsapp:app
After this is done, you now should be able to interact with the bot on WhatsApp. Yeeey!
You can monitor bot activity through Cloudant database and access the media files sent and received by the bot in the Cloud Object Storage bucket you create. You can also view a reduced version of the conversation history in the Twilio Console, going to the Overview page (Messaging -> Overview), under "Recent Messages".
Of course, there should also be a working Watson Assistant dialog skill attached to your Watson Assistant assistant (that correlates with WATSON_ASSISTANT_ID
) in order for you to receive sensible responses from the app.
Important Note: If you terminate the ngrok process and run it again, you will have to update the ngrok url in the box "WHEN A MESSAGE COMES IN" on the Twilio Sandbox page - this is because ngrok creates a random url every time you run $ ngrok http 8080
.
The code has try-except blocks in many places, so as to catch Twilio and Flask specific errors. Most errors should produce a debug output within the terminal. Some errors might be visible from the Twilio console, and Twilio might email you when errors occur.
When errors occur, a good first step is to identify if it has do with the path that Twilio is forwarding messages too. If it is, the ngrok interface should show error codes of 4XX. If it is not, it is likely that the error occurred within the Flask app.