'Build a WhatsApp Chatbot With Python, Flask, and Messagebird' python whatsapp
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.In your terminal navigate to the directory you created in step 2 and enter the following command replacing whatsappbot with your own name.
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:For Windows, activate the virtual environment with, replace whatsappbot with the name of the environment you created in step 2: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.” save the file and close: from flask import Flask app=Flask @app.route def bot: #webhook logic if __name__==‘__main__’: app.runOn WindowsThe 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 theimport jsonfrom flask import Flask, jsonify, request import requests app=Flask @app.route def hello: return “Bot is alive!” @app.route def bot: data=request.json message=data[“message”][“content”][“text”].lower if : return conversation_reply if : return conversation_reply def conversation_reply: reqUrl= headersList={ “Authorization”: “AccessKey MESSAGEBIRD_ACCESS_KEY“, “Content-Type”: “application/json”, } payload=json.dumps response=requests.request return jsonify 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.. If you already have it run your flask app with: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 response=requests.request printDeploying a WhatsApp bot to HerokuInside the Procfile add.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.0I 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 @app.route def hello: return “Bot is alive!” @app.route def bot: data=request.json message=data[“message”][“content”][“text”].lower if : return conversation_reply if : return conversation_reply def conversation_reply: reqUrl=headersList={“Authorization”: “AccessKey “ + MESSAGEBIRD_ACCESS_KEY, “Content-Type”: “application/json”,} payload=json.dumps response=requests.request return jsonify if __name__==‘__main__’: app.run
United States Latest News, United States Headlines
Similar News:You can also read news stories similar to this one that we have collected from other news sources.
Great Expectations: Chapter VII | HackerNoonGreat Expectations by Charles Dickens is part of HackerNoon’s Book Blog Post series. You can jump to any chapter in this book here.
Read more »
Developing, Packaging and Distributing a Python Library | HackerNoonHow to use new packaging standards with virtual environment tools — adapted from the official documentations of python.org and Pipenv
Read more »
Andrus, Lowrie homer, A’s end 13-game skid against MarinersA’s beat their AL West rivals for the first time since last July.
Read more »
Adventures of Huckleberry Finn: Chapter IX | HackerNoonAdventures of Huckleberry Finn, by Mark Twain (Samuel Clemens) is part of HackerNoon’s Book Blog Post series.
Read more »
Moby-Dick; or The Whale: Chapter 15 - Chowder | HackerNoonMoby-Dick; or The Whale, Chapter 15: Chowder by Herman Melville is part of HackerNoon’s Book Blog Post series.
Read more »
