Reply = ("Hello and welcome to the English Premier League 2018/19 WhatsApp bot!\n\n" # make string lowercase and remove whitespace ![]() We can know if a question doesn’t do so by checking the length of the team list:įrom _response import MessagingResponseĭf = df.str.lower() Firstly, every user question must mention at least one team name. The result and other statistics from a particular match - we will look for mentions of two team names.Number of shots a team has had or conceded - we will look for the “shots” keyword (and “concede” if the question is about shots conceded).Number of goals a team has scored (which could be broken down into home/away) - we will look for the “goals” string in the question (also “home” and “away” or neither).Number of matches a team has played - we will look for the “matches” string in the user’s question.We will aim to answer 4 types of questions: The rest of the chatbot logic will be implemented as a group of conditional statements that will account for all possible user inputs. ![]() This is passed to msg.body() and is returned as a response. You can see that the reply variable contains our welcome message and is a multi-line string. I will go through and explain each section of the code first, then the final script will be available at the end for you to copy if required. Let’s create a file called app.py in the current directory and start writing some code in this file. The Flask framework makes it easy to define a webhook. Our application will configure a URL, also referred to as an endpoint, so that Twilio can communicate with this webhook. A webhook delivers data (in our application this includes the incoming message). The Twilio API for WhatsApp uses webhooks in order to interact with users. We will program our chatbot to pick out words such as “goals” and “matches” so that our data source can be queried accordingly. We want the user to ask our chatbot questions such as “how many goals did Arsenal score?”, “tell me about the Chelsea vs Everton match” etc. We will be using a data source which provides information about historical English Premier League soccer matches including team names, scores, shots, yellow/red cards and many other statistics. It will look for particular keywords in the messages sent by the user and send back an appropriate response. Now that we have set up our development environment, we can start building our chatbot.įor this tutorial the chatbot will be very simple. If you are using a Unix or Mac OS system, open a terminal and enter the following commands to do the tasks described above: We will then install the packages we require inside of it. We will create a separate directory for this project and create a virtual environment using Python’s inbuilt venv module. We are now going to start developing our chatbot application. ![]() You can also connect additional phone numbers to this sandbox by repeating the same process. After a moment, you should receive a reply from Twilio confirming your mobile number is connected and can start sending and receiving messages. To enable the WhatsApp sandbox for your smartphone, send a WhatsApp message with this code to the number assigned to your account. You will also see a code that starts with join followed by two random words. You should now see the sandbox phone number assigned to your account as below. From there, select Programmable SMS and then click on WhatsApp on the left-hand menu. From your Twilio Console, open the Dock by clicking on the three dots on the left-hand side of the page. Let’s start by connecting your smartphone to the sandbox. Once you are happy with your application and want to put it into production, you can request access for your Twilio phone number, which requires approval by WhatsApp. Twilio provides a WhatsApp sandbox allowing you to easily develop and test your application. When you sign up, ensure you use the same phone number as the one you will use to test this application. You can review the features and limitations of a free Twilio account. If you are a new user, you can create a free account. A smartphone with an active phone number and WhatsApp installed.Installation instructions for your operating system can be found here. We will use this free utility to connect our Flask application running on our local system to a public URL that Twilio can connect to from the Internet. We will use this web framework to build an application that responds to incoming WhatsApp messages. To follow this tutorial you will need the following: The chatbot will allow users to get information about soccer teams and match statistics. ![]() There isn’t much live sport to watch at the moment which gives us the perfect opportunity to analyze historical data about the sport instead! In this tutorial I’m going to show you how to build a basic chatbot for WhatsApp using the Twilio API for WhatsApp and the Flask framework for Python.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |