3

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?

petezurich
  • 9,280
  • 9
  • 43
  • 57
Fi Fur
  • 31
  • 2
  • 1
    there is a possibility that your indentation is mixed with TABS and SPACES, in that case try all the indentation to either one of them solves this issue. – sid8491 Jan 15 '18 at 10:44
  • Have you managed to figure it out? – peachykeen Jan 19 '18 at 10:20
  • @agaidis no tried every possible position for the break comment still the same error. – Fi Fur Jan 19 '18 at 10:23
  • Are you working in an IDE or nice text editor like Atom or Sublime? Is there an option to auto-format or fix indentation? As far as uploading ZIPs go, I have found it easier for small pieces of code like this to just copy and paste it into my AWS console. – peachykeen Jan 19 '18 at 10:35
  • @agaidis I first uploaded the files and then created the function in the AWS Editor. But the main problem/question is did I correctly uploaded the requestes package? When I type "import" there is an autocomplete option for request, so it shouldn't be a problem but I am not sure. – Fi Fur Jan 19 '18 at 10:41
  • Have you looked at [this post](https://stackoverflow.com/questions/38877058/how-do-i-add-python-libraries-to-an-aws-lambda-function-for-alexa)? – peachykeen Jan 19 '18 at 11:10
  • @agaidis Thank you, I will look into it this weekend. – Fi Fur Jan 19 '18 at 11:51
  • @agaidis Now it works :) – Fi Fur Jan 27 '18 at 18:27
  • @FiFur Please add information on how you fixed your problem so that others can learn from that too. – petezurich Mar 04 '18 at 21:23

0 Answers0