Building a WhatsAPP Bot using FastAPI — Python — Fruits Bot
In this tutorial you will learn how to build and deploy (for testing purposes) a WhatsApp bot using the framework FastAPI. This bot will provide you nutricional information about fruits.
What is a Chatbot?
Chatbot it’s a software design to keep a conversation with a user. This can be a massive upgrade to many e-commerces, because permits to give attention to a costumer without really loosing time doing that. They can be:
- Rule based: you have to define all use cases previously
- ML/AI based: use techniques to reply to the costumers in a “intelligent” ways
Goals
The purpose of this tutorial is to show you how you can build your chatbot and thus develop these new skills.
This tutorial will teach you how to create a chatbot that allows you to know ‘nutritions’ about fruits, more than the use case, the important of this study is to explain.
Purpose of this tutorial
I’ve seen many tutorials and guides in the internet about the topic “WhatsApp Chatbots” and I found all of them use Flask framework. The web framework FastAPI it’s incredible fast, have data validation and supports currency in an easy way, so why we don’t try to use this “new” tool?
Get started
To make our communication we will use the service Twilio, they offer you free tier to try and use their services:
- Create an account to WhatsApp Sandbox https://www.twilio.com/messaging/whatsapp
- Send a message to the bot number with your phone (the number of the bot appears right after you create an account for WhatsApp Sandobox). To activate the bot you should send the “phrase”that appears in the dashboard of Twilio
Chatbot Code
So now, I will explain what is happening in this code, keep in mind you have to read the section below “Deploy Chatbot” to be able to test.
import requests, uvicorn
from fastapi import FastAPI, Form, Response
from twilio.twiml.messaging_response import MessagingResponse
import json bot_app = FastAPI()
This is the way you create an object of FastAPI. Make sure you have all the packages installed:
- FastAPI
- python-multipart
- Uvicorn
- Twilio
Have a look here in this snipped of code:
In the __main__ function you define the name of “app” you want to launch. In this exemple is “bot_app”. You choose the host/port, the port will be important to deploy, in this case port = 500.
You have to define the route, like this:
@bot_app.post("/bot")
We need to read the message that client send to us in the bot chat, so we need to parse the Body using Form, like this:
async def chat(Body: str = Form(...)):
After this we call two functions:
- getInfoFruit(fruit_name): request the api https://www.fruityvice.com/ to get information about the fruits and here it’s possible to handle errors from the request
- formatOutput(Body, data): only to format the output
In the end we need to return the message, and here we need to define the media_type, because Twilio use XML formatting:
return Response(content=str(response), media_type="application/xml")
Deploy Chatbot
- Install ngrok from here https://ngrok.com/download
- Remember I said to you the port is important? 5000 was the port. After installing you run on your terminal:
ngrok http 5000
3. This will create a foward between your local and one public IP in this case is https://809a-5-249-12-221.eu.ngrok.io:
4. Go to your WhatsApp sandbox settings in the Twilio page and write in this square your public IP + route, so in this example is:
https://809a-5-249-12-221.eu.ngrok.io/bot
5. Run the app and see this in the terminal
6. Now your are able to interact to your bot:
Non fruit case (error request):
Hope you liked!
Github: https://github.com/albertonvilas/
Linkedin: https://www.linkedin.com/in/albertovilas/
Alberto Vilas