Created
July 10, 2013 03:03
-
-
Save RobSpectre/5963165 to your computer and use it in GitHub Desktop.
Customize our conference wait room to read the most popular stories today from the New York Times API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from twilio import twiml | |
# First, we'll add your favorite HTTP client and mine, Requests | |
import requests | |
app = Flask(__name__) | |
# First, we give our app the key we retrieved from the | |
# New York Times API developer dashboard | |
app.config['NYTIMES_API_KEY'] = "key_we_registered" | |
# Next, we'll define the API request we will use to | |
# retrieve the most popular stories of the day to | |
# read to our users. | |
app.config['API_PATH'] = \ | |
"http://api.nytimes.com/svc/mostpopular/v2/"\ | |
"mostviewed/all-sections/1.json?api-key=" | |
# Keep our conference room same as before. | |
@app.route('/conference', methods=['POST']) | |
def voice(): | |
response = twiml.Response() | |
with response.dial() as dial: | |
dial.conference("Today's Headlines", waitUrl="/wait") | |
return str(response) | |
# Now we'll customize our wait room to read from the New York Times API. | |
@app.route('/wait', methods=['POST']) | |
def wait(): | |
# Create a TwiML response, just like before. | |
response = twiml.Response() | |
# Make sure we set our API key in the app's config dictionary | |
# up at the top of this snippet. | |
if app.config['NYTIMES_API_KEY']: | |
# With our API key set, we'll ask the NYT to give us the Most | |
# Popular stories today. | |
api_request = requests.get("%s%s" % (app.config['API_PATH'], | |
app.config['NYTIMES_API_KEY'])) | |
# If we get a successful response, we'll take a look at the | |
# results. | |
if api_request.status_code == 200: | |
# Parse the data we received with the Requests' module's | |
# handy built-in JSON parser. | |
json_response = api_request.json() | |
# If we have valid JSON, give the caller a quick introduction. | |
if json_response: | |
response.say("While you wait for your conference to connect," \ | |
" here are today's headlines from the New York Times.", | |
voice="alice") | |
# Iterate through all the results to get the dictionary | |
# for each news story. | |
for result in json_response['results']: | |
# Use the Alice voice with the Say verb to read the | |
# summary of the story to the caller. | |
response.say(result['abstract'], voice='alice') | |
# Add a courteous pause so the caller knows Alice | |
# has moved on to the next story. | |
response.pause() | |
else: | |
# If we didn't get JSON, let the caller know. | |
response.say("Unable to parse result from New York Times API.") | |
else: | |
# If we couldn't get a successful request, let the caller know. | |
response.say("Unable to reach New York Times API.") | |
response.say("Check your configuration and logs for the error.") | |
else: | |
# Finally, if we did not set the API key, also let the caller know | |
# and point to some resources for help with fixing this | |
# configuration error. | |
response.say("Configuration error: You need to set your New York " \ | |
"Times API Key environment variable. See the README for " \ | |
"more information.") | |
# Whatever result we have, turn it into TwiML and hand it over | |
# to Twilio to process for the hold room. | |
return str(response) | |
if __name__ == '__main__': | |
port = os.environ.get(5000) | |
if port == 5000: | |
app.debug = True | |
app.run(host='0.0.0.0', port=port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment