I was trying to develop a skill to be able to ask Alexa when the next departure of the train nearby is. Fortunately there is an API for the departure times in my city.
So I took a template and tried to rewrite it to my requirement:
from __future__ import print_function
import requests
import boto3
import json
##############################
# Builders
##############################
def build_PlainSpeech(body):
speech = {}
speech['type'] = 'PlainText'
speech['text'] = body
return speech
def build_response(message, session_attributes={}):
response = {}
response['version'] = '1.0'
response['sessionAttributes'] = session_attributes
response['response'] = message
return response
def build_SimpleCard(title, body):
card = {}
card['type'] = 'Simple'
card['title'] = title
card['content'] = body
return card
##############################
# Responses
##############################
def conversation(title, body, session_attributes):
speechlet = {}
speechlet['outputSpeech'] = build_PlainSpeech(body)
speechlet['card'] = build_SimpleCard(title, body)
speechlet['shouldEndSession'] = False
return build_response(speechlet, session_attributes=session_attributes)
def statement(title, body):
speechlet = {}
speechlet['outputSpeech'] = build_PlainSpeech(body)
speechlet['card'] = build_SimpleCard(title, body)
speechlet['shouldEndSession'] = True
return build_response(speechlet)
def continue_dialog():
message = {}
message['shouldEndSession'] = False
message['directives'] = [{'type': 'Dialog.Delegate'}]
return build_response(message)
##############################
# Custom Intents
##############################
def tramTime_intent(event, context):
url = "https://live.kvv.de/webapp/departures/byroute/1/de:8212:608?maxInfos=5&key=377d840e54b59adbe53608ba1aad70e8"
results = requests.get(url)
departures = results.json()["departures"]
destination = "Durlach"
#search for next departure
for departure in departures:
if departure["destination"] == destination:
nextDepTime = departure["time"]
break
if nextDepTime == "0":
text = "Deine Bahn fährt jetzt ab"
return statement("tramTimeIntent", text)
else:
text = "Die nächste Abfahrt in {t} ".format(t = nextDepTime)
return statement("tramTimeIntent", text)
def busTime_intent(event, context):
url = "https://live.kvv.de/webapp/departures/byroute/50/de:8212:608?maxInfos=5&key=377d840e54b59adbe53608ba1aad70e8"
results = requests.get(url)
departures = results.json()["departures"]
destination = "Hauptbahnhof"
for departure in departures:
if departure["destination"] == destination:
nextDepTime = departure["time"]
break
if nextDepTime == "0":
text = "Deine Bahn fährt jetzt ab"
return statement("busTimeIntent", text)
else:
text = "Die nächste Abfahrt in {t} ".format(t = nextDepTime)
return statement("busTimeIntent", text)
##############################
# Required Intents
##############################
def cancel_intent():
return statement("CancelIntent", "You want to cancel")
def help_intent():
return statement("CancelIntent", "You want help")
def stop_intent():
return statement("StopIntent", "You want to stop")
##############################
# On Launch
##############################
def on_launch(event, context):
return statement("title", "Herzlich Willkommen zur KVV live App")
##############################
# Routing
##############################
def intent_router(event, context):
intent = event['request']['intent']['name']
# Custom Intents
if intent == "BusTimeIntent":
return busTime_intent(event, context)
if intent == "TramTimeIntent":
return tramTime_intent(event, context)
# Required Intents
if intent == "AMAZON.CancelIntent":
return cancel_intent()
if intent == "AMAZON.HelpIntent":
return help_intent()
if intent == "AMAZON.StopIntent":
return stop_intent()
##############################
# Program Entry
##############################
def lambda_handler(event, context):
if event['request']['type'] == "LaunchRequest":
return on_launch(event, context)
elif event['request']['type'] == "IntentRequest":
return intent_router(event, context)
As I used the requests package I needed to make a ZIP and upload it. You can see the packages here.
But unfortunately I am getting errors. I could fix some of them but I can't get rid of this one:
Response: { "errorMessage": "Syntax error in module 'lambda_function'" }
Request ID: "7265a32b-f951-11e7-ba4d-fb2546cd448c"
Function Logs: START RequestId: 7265a32b-f951-11e7-ba4d-fb2546cd448c Version: $LATEST Syntax error in module 'lambda_function': unindent does not match any outer indentation level (lambda_function.py, line 68)
END RequestId: 7265a32b-f951-11e7-ba4d-fb2546cd448c REPORT RequestId: 7265a32b-f951-11e7-ba4d-fb2546cd448c Duration: 0.88 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 22 MB
I noticed that this error only occurs when I use a package which is not a default package from Amazon. So I assume I imported the ZIP file wrong.
Can you guys help me with that?