paint-brush
Build a WhatsApp Chatbot With Python, Flask, and Messagebirdby@marshallchikari
6,455 reads
6,455 reads

Build a WhatsApp Chatbot With Python, Flask, and Messagebird

by Marshall ChikariMay 26th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coin Mentioned

Mention Thumbnail
featured image - Build a WhatsApp Chatbot With Python, Flask, and Messagebird
Marshall Chikari HackerNoon profile picture


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, and click on WhatsApp then WhatsApp 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 using a version of python on 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.


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()


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 to obtain 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 jsonfrom 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()


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)


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.


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


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()


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)


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.


Previously published here.