I built a WhatsApp travel chatbot for nerds like me

Lindsay M Pettingill
4 min readDec 13, 2021
Photo by Anthony Reungère on Unsplash

I’m (maybe?) taking a trip soon. I’m a little annoying on trips because I wonder about everything (mostly out loud), always want to stop to read the historical markers, and just generally feel an intense need to understand a place.

I used to just devour the travel guides from Lonely Planet/Fodor’s/Rick Steve’s. But I always found it hard to follow along; wonderers who wander don’t follow directions well, if you can imagine. So I thought, why not build a small app that, regardless of whether I am on the right path, can give me a little more context and brighten my day with some new information?

Im this post I’ll walk you through how I built a WhatsApp chatbot to do just that. First off, the component parts. You’ll need accounts with the following (all of which are free):

  • Twilio. Provides the service/infrastructure that powers the chatbot.
  • Heroku. Makes it very easy to deploy and host apps on the web.
  • Iggy. Makes it super simple to understand place. (N.B.: I founded and run this company 😎.)

As for the technical details, you’ll be building a Flask app in Python. That app leverages the services above to power and host our bot. The app has 6 component files:

  • app.py, the engine of the app, so to speak (see below for sample code),
  • twilio_func.py, holds the necessary Twilio credentials (see below for sample code),
  • Procfile, literally a one line file that keeps your app running in Heroku (see below for sample code),
  • requirements.txt, declares Python package dependencies for the app (see below for sample code),
  • .gitignore, tells git which files to ignore (create this on your own),
  • osm_pois.csv, the file for our place data (create this on your own)

I want to spend a moment on Open Street Map (OSM) — an incredibly ambitious community effort to map the world and make that data freely available. While it is far from perfect OSM is, to my mind, an absolute gold mine. And for our purposes here — learning more about what’s around us for a low-stakes and nerdy chatbot — it’s an ideal source of data. Accessing OSM data is not the most straightforward for data novices though, hence why we use iggy — it’s user-friendly in that you can just choose an extracted dataset from our Open Data Catalog, download it in .csv or .json format, and then upload it to your app folder.

The app.py file is where the magic happens. I’ve commented the code below, but what it does is pretty straightforward. When a user sends the bot their location from WhatsApp (instructions here), the bot runs this app.py file on Heroku. In the app.py file we:

  • Import some packages,
  • Have the file read our osm_pois.csv file,
  • Do some typical geospatial stuff to get the data in a place that lets us do some math on it. We define that math in our nearest_point (distance) function. (It’s a quite naive distance function, but it works!).
  • Parse the message the user sent to the bot to extract the lat/lng, and then use that data in the distance function, along with our place data. The function returns the place/poi that is closest to the user’s location, and the bot sends that to us as a WhatsApp message with a bit of additionally appended data. That’s basically it!
Author’s screenshot of our WhatsApp chatbot in action

To get this app running, you’ll have to set up and deploy a Heroku app which is really straightforward — here’s a tutorial with a small section on the basics of Heroku at the end. You’ll also need to register for a WhatsApp Sandbox account in Twilio. (Don’t forget to update the ‘When a message comes in’ settings in the Sandbox configuration with the web address of where your app is hosted on on Heroku, and be sure to include a /bot at the end (i.e. the route we defined in app.py . You can find this address via the ‘Open App’ button on the Heroku Web UI.)

This app was built in a few hours and there are a ton of ways to improve it, from returning more/better data to calculating distance more accurately. Please do so!

✌️,

Lindsay

Relevant code

app.py

twilio_func.py

Procfile

requirements.txt

--

--

Lindsay M Pettingill

Co-founder & CEO @askiggyapp. Ex Data Scientist @Airbnb. I make data useful.