Skip to content

GitauHarrison/live-traffic-whatsapp-chatbot

Repository files navigation

Live Traffic Chatbot Using WhatsApp

Live Traffic Update

This is a simple chatbot that gives a live update about a user's location. Not only that, it provides a map's link to see where exactly the live update information applies.

Features

  • Appropriate feedback if the chatbot does not understand what the user says
  • Live update about traffic
  • Link to an interactive map

Tools

Deployed Application

Contributors

Testing This Application Locally

This is a mobile-based chatbot. You cannot find the chatbot on the deployed link. To test it out, you will need these:

  • A smartphone
  • An active phone number
  • WhatsApp mobile application installed in your smartphone
  • Twilio Account

Twilio provides a WhatsApp Sandbox that allows you to easily develop and test your application. To get started:

Try WhatsApp

You will see the Sandbox phonenumber assigned to you. There is also a code beginning with join- followed by two random words. You will need to send this code as a WhatsApp message to the number assigned to you to enable WhatsApp Sandbox.

  • Clone this project:
$ git clone git@github.com:GitauHarrison/live-traffic-whatsapp-chatbot.git
  • Move into the clone folder:
$ cd live-traffic-whatsapp-chatbot
  • Activate your virtual environment:
$ mkvirtualenv live_whatsapp_chatbot
  • Install all project requirements within your activate virtual environment:
(live_whatsapp_chatbot)$ pip3 install -r requirements.txt
  • Before you can run this application, you will need to create a .env file in the top-level directory following the example seen in the .env-template:
(live_whatsapp_chatbot)$ touch .env
  • Create a developer account with from TomTom API here. This will grant you 2500 free API calls per day.

  • You can create your API Key which you will later see in your dashboard

TomTom API Key

  • Add this key to your .env file:
TOMTOM_API_KEY='<your-TomTom_API-Key>'
  • Run the application:
(live_whatsapp_chatbot)$ flask run

You can access your localhost on http://127.0.0.1:5000/ as seen in the terminal. Additionally, if you look carefully in your terminal, you will see * Tunnel URL: NgrokTunnel: "https://4209c9af6d43.ngrok.io" -> "http://localhost:5000". This is the free public URL that ngrok has assigned you; giving access to your locally hosted application.

Alternatively, you can open another window in your terminal and run:

(live_whatsapp_chatbot)$ ngrok http 5000

Check out on the lines beginning with "Forwarding". You will use the https:// URL to allow for access to your locally hosted application.

Twilio Sandbox

  • In the box that says "WHEN A MESSAGE COMES IN", paste that ngrok URL. Remember to append /bot to your URL because this is the webhook that Twilio needs to know.

  • Click the Save button

Go back to your WhatsApp application on your phone. Ensure you have added your Sandbox number to your contact list. Then send a message to it as seen in the gif image at the top of this page.

Alternatively, if you do not want to use the free ngrok URL, which keep changing and is short-lived, you can deploy your application to Heroku. Paste your Heroku URL in the "WHEN A MESSAGE COMES IN" box. Test out the application in your WhatsApp.

Reference

  • If you are new to flask, I recommend that you begin from here
  • If you do not know what virtualenvwrapper is or you do not know how to use it, check this guide
  • It you would like to create your own live traffic chatbot, you can learn how to do it here