from flask import jsonify, request from twilio.twiml.voice_response import VoiceResponse, Gather from utils import logger, validate_data_presence, _get_weather, _get_cords, strq # from config import from . import routes as app from . import by_path_counter @app.route("/voice", methods=["GET", "POST"]) @by_path_counter def voice(): """Respond to incoming phone calls with a menu of options""" # Start our TwiML response resp = VoiceResponse() from_number = request.form["From"] if from_number not in env_AUTHORIZED_CALLERS: resp.say("You are calling from an unauthorized number. Goodbye.") return str(resp) resp.say("Welcome to Atmo Assistant.") # Start our verb gather = Gather(num_digits=1, action="/gather") gather.say( "Main menu. Enter one for Asheboro. Two for Lynchburg. Three for Cullowhee. Zero for another zipcode." ) resp.append(gather) # If the user doesn't select an option, redirect them into a loop resp.redirect("/voice") return str(resp) @app.route("/gather", methods=["GET", "POST"]) @by_path_counter def gather(): """Processes results from the prompt in /voice""" # Start our TwiML response resp = VoiceResponse() # If Twilio's request to our app included already gathered digits, # process them if "Digits" in request.values: # Get which digit the caller chose choice = request.values["Digits"] # a different message depending on the caller's choice if choice == "1": weather = _get_weather(35.6396, -79.8509) resp.say(weather) elif choice == "2": weather = _get_weather(37.3490, -79.1787) resp.say(weather) elif choice == "3": weather = _get_weather(35.3087, -83.1861) resp.say(weather) elif choice == "0": custom = Gather(num_digits=5, action="/custom") custom.say("Enter a zipcode for its weather.") resp.append(custom) else: # If the caller didn't choose 1 or 2, apologize and ask them again resp.say("Sorry, I don't understand that choice.") # If the user didn't choose 1 or 2 (or anything), send them back to /voice resp.redirect("/voice") return str(resp) @app.route("/custom", methods=["GET", "POST"]) @by_path_counter def custom(): """Processes results from the prompt in /gather""" # Start our TwiML response resp = VoiceResponse() # If Twilio's request to our app included already gathered digits, # process them if "Digits" in request.values: # Get which digit the caller chose choice = request.values["Digits"] # a different message depending on the caller's choice if len(choice) == 5: weather = _get_weather(_get_cords(choice)) resp.say(weather) return str(resp) else: # If the caller didn't choose 1 or 2, apologize and ask them again resp.say("Sorry, I don't understand that choice.") # If the user didn't choose 1 or 2 (or anything), send them back to /voice resp.redirect("/voice") return str(resp) @app.route("/city", methods=["GET"]) @by_path_counter def city(): if "city" in request.values: city = request.values["city"] # Return a different message depending on the caller's choice # Asheboro if city == "1": weather = _get_weather(35.6396, -79.8509) return strq(weather) # Lynchburg elif city == "2": weather = _get_weather(37.3490, -79.1787) return strq(weather) # Cullowhee elif city == "3": weather = _get_weather(35.3087, -83.1861) return strq(weather) else: return strq("Your selection is invalid.") else: return strq("A city is required.") @app.route("/zipcode", methods=["GET"]) @by_path_counter def zipcode(): if "zipcode" in request.values: zipcode = request.values["zipcode"] if len(zipcode) == 5: lat, long = _get_cords(zipcode) weather = _get_weather(lat, long) return strq(weather) else: return strq("Sorry, I don't understand that zipcode.") else: return strq("A zipcode is required.") @app.route("/test", methods=["GET"]) @by_path_counter def test(): return strq("Successful test. By the way, do you know the muffin man? The muffin man? The muffin man? Do you know the muffin man? He doesn't exist.")