California is in the middle of a historically extreme drought. While this isn’t news to anyone, San Francisco has seen an unexpectedly large amount of rain in the last couple weeks, causing problems for many residents.
Personally, my building has had major leaking and flooding issues each of the last 3 weeks. After getting fed up over the overall lack of preparation and response, I decided to have some fun with weather data and Twilio to “gently inform” my landlord when the rains are coming.
Sneak Preview: 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 TwilioEach class. All things considered, maybe 30 lines of code total. How? All because of the power of components.
Sending Twilio messages 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.