Last active
March 17, 2022 05:48
-
-
Save platisd/9cecfcbc5fd5690ce37acb2593566409 to your computer and use it in GitHub Desktop.
A sample Flask server for my Google Assistant tutorial on Instructables.com
This file contains 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
#!/usr/bin/python3 | |
# -*- coding:utf8 -*- | |
import json | |
from flask import Flask, request, make_response, jsonify | |
app = Flask("My-Local-Traffic-Planner") | |
def respond(fullfilment): | |
return make_response(jsonify({'fulfillmentText': fullfilment})) | |
@app.route('/departures', methods=['POST']) | |
def departures_handler(): | |
try: | |
req = request.get_json(silent=True, force=True) | |
print(json.dumps(req)) | |
location = req.get('queryResult').get('parameters').get('current-location') | |
if location == 'home': | |
return respond("Oh so you are home now. Well, you better hurry!") | |
elif location == 'work': | |
return respond("At work? Well, are you sure it's time to leave already?") | |
else: | |
return respond("I am not sure where " + location + " is.") | |
except Exception as e: | |
print(e) | |
return respond("Sorry, an error occurred. Please check the server logs.") | |
def main(): | |
app.run() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment