Rain’s Coming, Tell Your Landlord via Twilio SMS and Zillabyte

January 16, 2015
Written by

Zillabyte Twilio

It rarely rains in San Francisco. We go months without so much as a drop, sprinkle or sun shower. But, when the rain hits, it hits hard. In December there were tremendous floods due to downpour that left homeowners and renters in dire straits. Unprepared landlords had to scramble and work against the clock to fight flooding, and repair water damage.

Nikhil Karnik, a software engineer at distributed cloud computing platform for data analysis, Zillabyte, built an app to give landlords a polite (but direct) heads up when rains are coming. Nikhil experienced flooding first hand, and hopes his new app that sends landlords rain updates via Twilio SMS can save other tenants from the hassle he suffered. Thanks to the power of Zillabyte, Nikhil knows his app can not only help out San Franciscans, but anyone in the US.  Check out his blog post below, and grab the code here.

To learn more about Zillabyte and read Nikhil’s post, originally published on the Zillablog, click here.

How to Build a Global Weather Alert System in Under an Hour

If the 3 day weather forecast calls for a large amount of rain, I can probably just check an app and let my landlord know in advance. But, hold on… Zillabyte is a service for distributed computing… so what’s the fun of being limited to weather data from one city? I want to help tenants notify their landlord all across America (even the whole world!) about upcoming local weather conditions. I am better served using Zillabyte to query and process data from almost every weather station in America and an automated service like Twilio to serve the notifications.

First, I wrapped all of my Twilio rest API calls inside a Zillabyte component. After registering this component, I can call this component at will. The component simply sends a message from a Twilio number about the current weather conditions in a city to a phone number subscribed to that city.

The Crux of the App

import zillabyte
from zillaweather import TwilioEach

app = zillabyte.app(name="zillaweather")

#source from list of world cities
stream = app.source("all_cities")

#call component to find current weather condition at each city
stream = stream.call_component("tomorrow_weather")

#Use current weather and dictionary of cities : numbers to send a SMS with Twilio
stream = stream.each(TwilioEach, name="Prepare_Twilio")
stream = stream.call_component("twilio_text")

stream.sink(name="weather", columns = [{"to":"string"}])

That’s seriously it. In full disclosure, this is what the entire app looks like without modularizing the Twilio Each class. All things considered, maybe 30 lines of code total. How? All because of the power of components.

Sending Twilio SMS with Zillabyte

If the 3 day weather forecast calls for a large amount of rain, I can probably just check an app and let my landlord know in advance. But, hold on… Zillabyte is a service for distributed computing… so what’s the fun of being limited to weather data from one city? I want to help tenants notify their landlord all across America (even the whole world!) about upcoming local weather conditions. I am better served using Zillabyte to query and process data from almost every weather station in America and an automated service like Twilio to serve the notifications.

First, I wrapped all of my Twilio rest API calls inside a Zillabyte component. After registering this component, I can call this component at will. The component simply sends a message from a Twilio number about the current weather conditions in a city to a phone number subscribed to that city.

import zillabyte
from twilio.rest import TwilioRestClient

def send_text(controller, tup):

  # Register twilio client
  twilio_client = TwilioRestClient(tup["twilio_sid"], tup["twilio_auth"])

  try:
    # Send message to recipient and wait 1 second (twilio SMS rate limit)
    text_body = "It is going to be " + tup['condition'] + " in " + tup['city'] + " tomorrow!"
    twilio_client.messages.create(to=tup["to"], from= tup["from"], body= text_body)

    # Emit the recipient back to the stream...
    controller.emit({"to" : text_to})
    time.sleep(1)

  except:
    pass

component = zillabyte.component(name="twilio_weather_text")
stream = component.inputs( 
  name = "input_stream",
  fields = [{"twilio_sid" : "string"}, {"twilio_auth" : "string"},
            {"from" : "string"}, {"to" : "string"}, {"city" : "string"},
            {"condition" : "string"}]
  )

stream = stream.each(send_text)
stream.outputs(name = "output_stream", fields=[{"to" : "string"}])

Fork this component on github.

The Entire App

After registering the component, the remainder of the Zillabyte app is straightforward to write. The app uses a pre-registered component called “tomorrow_weather”, which will determine the expected forecast for tomorrow for a given city. It then sends rain notifications to phone numbers registered by city via the Twilio “send_text” component.

Why Mix Data and Notifications?

The primary utility and accessibility of “big data” stems from the ability of data systems to provide meaningful insights to humans. Certainly, many times large-scale data systems are built to feed some dynamic model on the back-end (such as an Ad model on a social network service), but under certain conditions, notifying humans is necessary. For example, if your Ad model is performing particularly poorly (users aren’t clicking because they aren’t being served relevant ads), your data science team might want to receive a text notification in case they’re out to lunch.