How to Build a Football Tracker with Twilio WhatsApp and Node.js
Time to read: 7 minutes
In today's digital age, messaging platforms have become an integral part of our daily lives. One platform that stands out is WhatsApp. WhatsApp as an integral part of our daily communication, helps to connect to friends, family, and customers. With the help of Twilio and Node.js, we can harness its power to create engaging and interactive applications.
In this article, I will guide you through the process of building a football WhatsApp bot using the Twilio API for WhatsApp, Rapid API, and Node.js. By the end, you'll have a robust bot that can provide live scores, match results, league standings, and more!
Prerequisites
- Node.JS 16+. You can download an installer from Nodejs.org
- ngrok. This is used to connect your Express application running locally to a public URL that Twilio can connect to from the Internet.
- A smartphone with WhatsApp installed.
- A Twilio account. If you are new to Twilio, you can create a free account here.
- A Rapid API account.
Setting up the development environment
Creating application directory and installing dependencies
Launch your terminal and navigate to your desired location to create the project directory. Run the following commands to create a directory for your project and initialize an npm project inside it:
Execute the following commands to install the Express framework and other necessary dependencies needed for this project.
The other packages listed above would be needed for the following:
twilio
- Twilio’s Node.js library used to handle sending messages.nodemon
- a package that monitors your project directory and automatically restarts your node application when it detects any changes.dotenv
- a package that is used to load the environment variables.axios
- a promise-based library for making HTTP requests.dayjs
- a package used to parse, manipulate, and display dates and times.
Using your favorite code editor, create an index.js file in the root folder of your application and add the following code:
To allow nodemon
to watch your files and automatically reload the server when you make changes, add this line to the scripts section in the package.json file:
To start your local server on port 5000, run the following command in your terminal:
Setting up Rapid API
The source of data for the chatbot application is Zeus API on the Rapid Api platform. You would need to get an API key from Rapid API. Create a free account and log in to the account. Proceed to the Zeus Api page and subscribe to the API to be able to get access to test the API.
For this tutorial, you will be using the X-RapidAPI-Key located in the Code Snippets section in the image above. This token will need to be sent in the request header of every request to verify your identity. Ensure you only copy the key without the single quotes.
Create a .env file in the root directory of your project, open it, and paste the API key in this format:
Create chatbot
Now that you have set up your development environment, it is time to build the chatbot. For this tutorial the chatbot will be very simple. It will look for particular keywords in the messages sent by the user and send back an appropriate response.
Send and receive message
This section will explain the process of implementing the functionality needed for receiving and responding to messages sent through WhatsApp.
Create a utils folder in the root directory of your project where you would add all the helper functions needed. Create a twilio.js file inside the folder and add the following code:
In the code snippet above, the sendResponse
function makes use of TwiML (Twilio's Markup Language) to specify the desired response to incoming messages received by Twilio. The MessagingResponse helper class from Twilio's package is utilized to generate the TwiML.
To receive event notifications from Twilio, you must set up a route. The Twilio API for WhatsApp uses webhooks in order to interact with users. The webhook delivers data that includes the incoming message.
Update the index.js with the following code to import the sendResponse
function and to create a route in the index.js file. This route handler is triggered each time a user sends a message to the WhatsApp bot:
In the above route handler, the message sent by the user is received in the body of the request. The route handler imports and uses the sendResponse
helper function to send a message back to the user and the message is formatted using TwiML before being returned.
Chatbot logic
The rest of the chatbot logic will be handled by some utility functions that would account for all possible user inputs. The chatbot would be able to:
- Supply league standings for a particular league by looking for the “standings” string in the user’s prompt,
- List match results for a game week by looking for the “week” string in the user’s prompt,
- Give details about a particular match by looking for the “match” string in the user’s prompt.
Fetching league and match details
Inside the utils folder, you'll create three more files. First, you’ll need a file called fetcher.js that would handle the API calls to the Zeus API endpoint:
This file contains three functions that would communicate with the Zeus API.
- The
getLeaguesInfo
function gets all available competitions. - The
getStandings
function gets the current standings for a league. - The
getMatches
function gets the current matches for a particular game week in the specified league.
If an error occurs while getting the necessary information the functions return false so that the user can be informed by the helper function that would be created to parse the user’s message.
Formatting league and match details
Next, create a format.js file that'll convert the data received from the endpoint into a string that can be sent to the user:
This file contains four functions that would format the returned data into a readable string for the user.
- The
formatLeague
function receives an array of the available competitions and using the Array.reduce method converts it into a string. - The
formatStandings
function receives an object with thename
,yearStart
,yearEnd
, andstandings
key and converts it into a string. - The
formatMatches
function receives an array of the matches for a particular game week, the week number, and the league name as arguments, and converts them into a string. - The
formatMatchResult
function receives an object containing the match details for a particular game within a game week, the week number, and the league name as arguments, and converts them into a string.
Parsing user’s request
Finally, create a controller.js file that will parse the message received from the user and choose which of the requests to make to get the necessary information:
This file first of all creates some constants that would be needed by the functions in the file. It then defines two functions.
- The
selectMatch
function receives an array of matches and the name of a team as arguments and then returns the object containing the match details for that team if contained in the array of matches. - The
parseUserInput
function receives the user’s message and determines which of the keywords the user has sent and then determines which of the other helper functions to call based on the user’s message. If an error occurred, the user is informed about it.
After creating the helper functions, it is now time to import and make use of them in the route handler. Go to the index.js file and replace the code in the route handler with the code below:
Now your index.js file should look like this:
With the code for the chatbot done, it is now time to test the application.
Testing the application
The application is currently running on your computer at http://localhost:5000 but it is not accessible from the Internet. For Twilio to have access to the application and send and receive requests you would need to create a temporary public URL that is accessible to Twilio.
This would be done using the ngrok
tool. Open a second terminal window and execute the following command:
The command above tells ngrok
to expose your application running at port 5000 over the internet. You should see an output like the one below which gives you a Forwarding URL through which your app can be accessed over the web. You would be making use of the secure URL, which starts with https://
Set up Twilio Whatsapp Sandbox
Twilio provides a WhatsApp sandbox that allows you to easily develop and test your application. Navigate to the WhatsApp sandbox configuration page in the Twilio Console. On the Sandbox tab which should look like the image below, make a note of the Twilio phone number and the join code:
Switch to the Sandbox Settings tab and set up the endpoint URL that Twilio will make use of to forward messages sent or received from WhatsApp. Copy and paste the public forwarding URL obtained earlier from ngrok
into the When a message comes in field and then click the Save button:
Your application is now ready to be tested. Open WhatsApp on your personal device and send a message to the phone number indicated on the Twilio WhatsApp sandbox. The chatbot would respond based on the messages you send.
Conclusion
In this tutorial you have created a simple chatbot that returns information about football leagues, matches, and match details based on certain keywords. This was implemented using Express, Zeus API, and the Twilio API for WhatsApp.
The chatbot’s functionality can be extended further. For example by allowing users to state the year that they want league standings and match results for. All the code for this tutorial can be found on GitHub here.
I hope you learnt something from this tutorial and are inspired to build some amazing chatbots of your own.
Anderson Osayerie is an experienced Web Developer and technical writer with a proven track record of crafting and creating products for global companies. He is very interested in creating aesthetically pleasing and functional web applications. He can be reached via the following channels:
LinkedIn - https://www.linkedin.com/in/anderson-osayerie/
Github - https://github.com/andemosa
Twitter - https://twitter.com/andemosa
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.