DEV Community

Marshall Nyasha Chikari
Marshall Nyasha Chikari

Posted on

Build a WhatsApp Chatbot With Python, Flask, and Messagebird

In this tutorial, I’m going to build a chatbot using the Messagebird API for WhatsApp and the Flask framework for Python.

The first step is to get access to use the WhatsApp API use this link to get access, once you sign up and you are in the dashboard navigate to WhatsApp and follow the instructions on setting up your own number to work with the API. As of now, we can use the Messagebird sandbox to test our bot so let’s dive into it.

Connect your phone to the sandbox from the Messagebird dashboard, click on WhatsApp then WhatsApp sandbox.

messagebird sandbox

When you send your first message to that number you will be assigned a unique channel id, but when you are verified to use your own number you will also get your own channel id which we will use to communicate with our bot. If you already installed a virtual environment skip step 1.

Step 1: Install Virtual Environment

To install on Linux

sudo apt install python-virtualenv

To install on Mac

sudo python2 -m pip install virtualenv

To install on Windows

py -2 -m pip install virtualenv

Step 2: Create an Environment

Create a directory or folder where you will create your environment use a version of python in your machine, I’m going to use python3 in this tutorial

Create an Environment in Linux and MacOS
In your terminal navigate to the directory you created in step 2 and enter the following command replacing whatsappbot with your own name.

python3 -m venv whatsappbot

Create an Environment in Windows

py -3 -m venv whatsappbot

Step 3: Activate the Environment

Activate the virtual environment in Linux and MacOS with, replace whatsappbot with the name of the environment you created in step 2:

. whatsappbot/bin/activate

For Windows, activate the virtual environment with, replace whatsappbot with the name of the environment you created in step 2:

whatsappbot\Scripts\activate

Step 4: Install Flask

Enter the following command

pip install Flask messagebird requests gunicorn jinja2 werkzeug urllib3

Open your folder with a code editor mine is vscode your project folder should now have the have the venv folder mine is whatsappbot, go create a file bot.py in the root folder outside the venv directory.

Vscode editor image

Add the following code in the bot.py file to make an application that prints “Hello bot” save the file and close:

from flask import Flask
app = Flask(__name__)
@app.route(/bot, methods=[POST])
def bot():
   #webhook logic
if __name__ == __main__:
   app.run()
Enter fullscreen mode Exit fullscreen mode

Set the FLASK_APP environment variable with the following command in the terminal.

On mac and linux

export FLASK_APP=bot.py

On windows

setx FLASK_APP “bot.py”

The first thing we need to do in our chatbot is obtaining the message entered by the user. This message comes in the payload of the POST request, edit your bot.py file with the following code and I’m going to explain how it works.

import json
from flask import Flask, jsonify, request
import requests
app = Flask(__name__)
@app.route(/)
def hello():
  return Bot is alive!”
@app.route(/webhook, methods=[POST])
def bot():
   data = request.json
   message = data[message][content][text].lower()
   if (message == hello):
       return conversation_reply(
          data[conversation][id],How are you
       )
   if (message == bye):
       return conversation_reply(data[conversation[id],Good    bye!”)

def conversation_reply(conversation_id, message_text):
   reqUrl = ("https://conversations.messagebird.com/v1/conversations/"+conversation_id+ "/messages")
headersList = {
      Authorization: AccessKey MESSAGEBIRD_ACCESS_KEY,
      Content-Type: application/json,
   }
   payload = json.dumps({type: text, content: {text:  message_text}})
   response = requests.request(POST, reqUrl, data=payload,    headers=headersList)
   return jsonify({status: ok, response: response.text})

if __name__ == __main__:
   app.run()
Enter fullscreen mode Exit fullscreen mode

The request.json contains information from the sender, so we need the message content and use it in our if statement. I created another function that handles post requests to the conversations API with a reply. To test your bot add your Messagebird Access key from the dashboard.

To test on localhost use ngrok

If you do not have ngrok on your machine install it from here. If you already have it run your flask app with:

flask run

Then open another window from your terminal and run

ngrok http 5000

Create your webhook using the following command, you can create a separate file that you will run once replace the accesskey, channelid and url with the one you got from running ngrok don't forget to add /webhook

import requests
reqUrl = "https://conversations.messagebird.com/v1/webhooks"
headersList = {
 Authorization: AccessKey MESSAGEBIRD_ACCESS_KEY,
 Content-Type: application/json 
}
payload = json.dumps({
events:[message.created, message.updated],
channelId: YOUR_CHANNEL_ID_FROM_THE_WHATSAPP_SANDBOX,
url:"https://your-domain.ngrok.io/webhook"
})
response = requests.request(POST, reqUrl, data=payload, headers=headersList)

print(response.text)
Enter fullscreen mode Exit fullscreen mode

And that’s it you can test your bot by sending messages to the sandbox number.

Deploying a WhatsApp bot to Heroku
Add the Procfile, requirements.txt, and runtime.txt files to your root folder

folder structure

Inside the Procfile add

web: gunicorn — bind 0.0.0.0:$PORT app:app

Requirements.txt add the following replacing with the actual versions you are using

flask==2.0.2
Jinja2==3.0.3
gunicorn==20.1.0
requests==2.27.1
urllib3==1.26.8
werkzeug==2.0.3
messagebird==2.0.0
runtime.text

python-3.9.10
Enter fullscreen mode Exit fullscreen mode

I have used the python version I’m using it can be different from yours.

Create environment variables to hide your MESSAGEBIRD API key in the production environment update your code inside bot.py.

import json
from flask import Flask, jsonify, request
import requests
from os import environ

MESSAGEBIRD_ACCESS_KEY = environ[MESSAGEBIRD_ACCESS_KEY]

app = Flask(__name__)
@app.route(/)
def hello():
  return Bot is alive!”
@app.route(/webhook, methods=[POST])
def bot():
data = request.json
message = data[message][content][text].lower()
if (message == hello):
return conversation_reply(data[conversation][id],How are you)
if (message == bye):
return conversation_reply(data[conversation][id],Good bye!”)
def conversation_reply(conversation_id, message_text):
reqUrl = ("https://conversations.messagebird.com/v1/conversations/" + conversation_id + /messages)
headersList = {
Authorization: AccessKey  + MESSAGEBIRD_ACCESS_KEY, Content-Type: application/json,
}
payload = json.dumps({type: text, content: {text: message_text}})
response = requests.request(POST, reqUrl, data=payload, headers=headersList)
return jsonify({status: ok, response: response.text})

if __name__ == __main__:
app.run()
Enter fullscreen mode Exit fullscreen mode

After that run, the following Heroku commands in your terminal

heroku login

Once logged in run

heroku create

After your Heroku app has been created add your Messagebird API key using this command

heroku config:set MESSAGEBIRD_ACCESS_KEY=your-actual-api-key-from-the-messagebird-dashboard

Then commit your changes with git add . and git commit -m “first commit”, now you should be able to run the following command to deploy your WhatsApp bot online.

git push heroku main

After that

heroku ps:scale web=1

Now if you access your Heroku URL you should see the text your bot is alive. Just one thing left is to create a webhook that has the Heroku URL of your bot.

import requests
reqUrl = "https://conversations.messagebird.com/v1/webhooks"
headersList = {
 Authorization: AccessKey MESSAGEBIRD_ACCESS_KEY,
 Content-Type: application/json 
}
payload = json.dumps({
events:[message.created, message.updated],
channelId: YOUR_CHANNEL_ID_FROM_THE_WHATSAPP_SANDBOX,
url:"https://your-domain.herokuapp.com/webhook"
})
response = requests.request(POST, reqUrl, data=payload, headers=headersList)

print(response.text)
Enter fullscreen mode Exit fullscreen mode

That’s it you now have a WhatsApp bot for more information and to enhance your bot read the docs.

Find me on Twitter if you have any problems.

Top comments (1)

Collapse
 
pankti_s profile image
Pankti Sidhpura

Hi, I'm Pankti and I work for a Market Research agency called MarketVistas.
For a research, we're looking to speak to a few profiles that might be similar to yours. You shall certainly be reimbursed for your time and effort. Please see below the different profiles we need. Please hit us up if interested!
We promise you, we're legit!

~5 Software developers that develop tools/add-ons for WhatsApp (unofficially, or in a capacity not directly related to Meta/WhatsApp)
~1-2 Software architects or designers that work on development projects on WhatsApp (same scope as above)
~1-2 Other members of a company or development team that work for a company that does specific development for to WhatsApp. Ideally, this would maybe be the CEO of a small development firm with 10-50 employees. Would likely have a lot of development in their own background.