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 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. link 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 “ ” save the file and close: Hello bot from flask import Flask app = Flask(__name__) @app.route(‘/bot’, methods=[‘POST’]) def bot(): #webhook logic if __name__ == ‘__main__’: app.run() Set the environment variable with the following command in the terminal. FLASK_APP 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 request, edit your bot.py file with the following code and I’m going to explain how it works. POST 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 . If you already have it run your flask app with: here 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 if you have any problems. Twitter Previously published here.