This project aims to illustrate how to build a chatbot for WhatsApp using the Twilio API for WhatsApp and the Flask framework for Python.
The following installation has been tested on MacOSX 10.13.6 and Ubuntu 16.04.
This project requires Python 3, a smartphone with an active phone number and WhatsApp installed, a Twilio account and the following Python libraries installed(plus a few others depending on task):
- Clone the repo
git clone https://github.com/gauravchopracg/WhatsApp-Chatbot.git
cd WhatsApp-Chatbot/
- Install Dependencies
pip install -r requirements.txt
- Configure the Twilio WhatsApp Sandbox From your Twilio Console, select Programmable SMS and then click on WhatsApp. Now, from your smartphone send a WhatsApp message with the given code to the number assigned to your account.
- Configuring the Chatbot
$ cd Code/
$ python bot.py
* Serving Flask app "bot" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Open a second terminal window and run ngrok http 500 from the folder where you have downloaded ngrok. The output of ngrok should be something like this:
Now you need to copy the https:// URL from the ngrok output to Sandbox in the Twilio Console on WhatsApp and append /bot at the end of the root ngrok URL.
Now you can start sending messages to the chatbot from the smartphone that you connected to the sandbox.Type any sentence with words "quote" and "cat" in them.
To deploy a WhatsApp chatbot for production use, skip the step of using ngrok and choose a cloud server for deployment of application. The two most common production ready web servers for Python web applicaiton are gunicorn and uWSGI after that you can simply run chatbot by following:
$ gunicorn -b :5000 bot:app
The credits for this project goes to Miguel Grindberg and twilio for creating amazing tutorials on how to create web applications using Python and Flask framework integrated with Twilio API.