Weather by Text Message Using Twilio and DarkSky

Screenshot of iPhone text messages receiving weather forecasts

I recently fixed my Weather by Text mini project to get it working again (since Yahoo shut down YQL) and to update with a few improvements. This service is a simple Twilio-based service which delivers the current weather for the location you send via text message.

The Backstory

The service is simple:text your location to 415-202-6773 and receive back the current weather conditions.

I created this 5 years ago when I was first learning about the Twilio programmable voice and SMS services. It was also made pretty easy by using the old Yahoo YQL service which boiled down to two simple calls:

select woeid from geo.placefinder where text='%s'
select * from weather.forecast where woeid=%s

Unfortunately Yahoo retired all the YQL services in January of this year, leaving my poor little project dead in the water.

Recently I’ve had some time to revisit and rewrite everything to use current webservices directly, so now it works once again!

Services Used

The source code for this project is on GitHub under bcantoni/wxtext if you want to see how any of this was implemented or run it yourself. Here are the services used:

Twilio is the key service here – we’re using the Programmable SMS functionality. This is really the only cost I have for running this service: $1/month for the dedicated number and $0.0075/message (within the US; other countries generally cost more). The best documentation topics that explains the programmable SMS request/response flow are Getting Started with Twilio Webhooks and How to Receive and Reply to SMS and MMS Messages in Python.

HERE Geocoder translates the free form “location” text entered by the user into a physical location (latitude/longitude pair) which is then passed on to the weather API. The Freemium level allows up to 250K requests/month for commercial and non-commercial web sites.

DarkSky at the free level provides up to 1000 weather forecast API calls per day; after that it’s still very cheap (compared to Twilio). Currently I’m only using the current conditions, but they also have hourly and daily forecast data which I might add in the future. The data returned by DarkSky is thorough but also well organized and easy to use (unlike some other more complicated weather services I looked into).

Slack support is provided with the ultra-simple webhook support for posting a message to a channel. I’m using this to post all requests to my personal Slack workspace just to keep an eye on activity and any errors.

What’s Next

No self-respecting side project is without a long to-do list for what’s next. Here’s the list for Weather by Text:

  • Improve the forecast response with more details like general conditions (sunny, cloudy, etc) and possibly some matching icons
  • Add better international support including units, look at using city/country code coming with Twilio request to see if those are useful. Right now the results may work for international locations but are skewed towards the US.
  • Related, do a better job handling cases where more than one location is returned, giving the user a choice to pick which one they meant (San Jose California or San José Costa Rica?)
  • Convert to Python 3.8 and switch to using the Twilio Python client library and other benefits such as requests caching. The code currently runs on Python 2.7 on a shared web host where I don’t have sudo, so I’m not using any libraries beyond what’s normally present in a 2.7 system. This also means today’s solution relies on CGI handling which has been a real pain to troubleshoot.
  • Look at hosting on AWS Lambda w/API Gateway rather than requiring a separate web host
  • Improve error handling everywhere, especially if location is not found (make sure always respond to a request from user)
  • Add ability to get forecast in addition to current conditions

To give it a try, start with texting HELP to 415-202-6773 and have fun!

Phone mockup image credit: MockuPhone