首页 文章

如何对Amazon Alexa进行REST API调用

提问于
浏览
2

我正在构建一个自定义的Alexa技能,并希望进行REST API调用 . 目前,我能够发出GET请求从我的Web服务获取数据,然后由Alexa使用 . 但是,我有以下要求,我不知道如何开发这个 .

  • 调用技能(完成)

  • 用户将调用"Get List of Topics" intent,Alexa进行GET REST API调用并提供"list of topics"(完整)

  • 让Alexa提示用户从列表中选择一个主题(待定)

  • 接收用户在lambda函数中做出的响应,并使用响应进行POST / PUT调用 . (待定)

  • 如果所选主题无效(待处理),则重新提示用户 .

我该如何实现3,4和5?我目前正在使用Python 3.6在AWS开发人员控制台上编写lambda函数 . 是否有适用于Python 3.6的Amazon Alexa API指南 .

我如何在Java中这样做,这是我的首选方式?

我按照这里的说明开发了我目前的产品:https://github.com/simonprickett/alexabart

是否有关于如何编写Alexa特定lambda函数及其相关的Python3或Java API指南的详细文档 .

谢谢

2 回答

  • 1

    如果您有很多问题,并且希望每个问题都得到解答,则可以使用 sessionAttributes 变量 . 会话变量可以在会话过程中保留其值 . 您可以将字典保存在 sessionAttributes 中(它必须是字典) .

    您可以在会话变量中保存这样的内容 .

    sessionAttributes: {
        "AllQuestions" : ["string", "string"],
        "LastQuestionIndex" : integer
    }
    

    我'm assuming you'能够获得问题列表(使用GET请求) .

    步骤1:制作一个插槽

    Answer 将是一个存储您的答案的插槽 .

    第2步:准备好你的问题了

    如果你的意图刚刚开始并且 sessionAttributes 中没有任何内容(使用简单的if-else),你必须提出GET请求并收集所有问题(可能在列表或其他内容中) . 您发出GET请求并将所有问题存储在 sessionAttributes['AllQuestions'] 中 . 并设置 LastQuestionIndex = -1 .

    现在棘手的部分进来了 . (我'm also assuming you'能够使用 Dialog.ElicitSlot 指令) .

    第3步:逐个提问 .

    现在您有一个所有问题的列表,并且您还有最后一个问题的索引 . 现在你只需要增加索引,得到下一个问题,并使用 Dialog.ElicitSlot 指令来提出这个新问题 . 并更新sessionAttributes中的 LastQuestionIndex .

    第4步:得到答案

    在继续下一个问题之前,您还必须 check if the slot Answer has any value or not? 如果它确实有一个值(它不是"None"),那么您可以使用 LastQuestionIndex 变量并存储该特定问题的答案 .

    如果您正在寻找代码,请转到:

    # Line 1 - 22 should be in your intent function
    sessionAttributes =  dict()
    if 'sessionAttributes' in event['session']: 
        sessionAttributes = event['session']['sessionAttributes']
    
    if not sessionAttributes.has_key('AllQuestions') : 
        # Make the GET REQUEST
        Questions = ["all", "of", "your", "Questions", "stored", "in", "a", "list"]
        sessionAttributes['AllQuestions'] = Questions
        sessionAttributes['LastQuestionIndex'] = -1
    
    
    Answer = getSlotValue('Answer')
    if Answer != None:
        # AllAnswers is a dictionary which has key as the question number and value is the answer
        # AllAnswers = {
        #   0 : "Answer to your first question",
        #   1 : "Answer to your second question"
        # }
        AllAnswers[sessionAttributes['LastQuestionIndex']] = Answer
    
    return ansNextQuestion(sessionAttributes)
    
    
    def askNextQuestion(sessionAttributes) :
        Last = sessionAttributes['LastQuestionIndex']
        sessionAttributes['LastQuestionIndex'] = Last + 1
    
        if Last < len(sessionAttributes['AllQuestions']): 
            outputSpeech = "Your next question is " + sessionAttributes['AllQuestions'][Last + 1]
            return {
                "version": "1.0",
                "sessionAttributes": sessionAttributes,
                "response": {
                    "outputSpeech": {
                        "type": "PlainText",
                        "text": outputSpeech
                    },
                    "shouldEndSession": False,
                    "directives": [
                        {
                            "type": "Dialog.ElicitSlot",
                            "slotToElicit": "Question",
                            "updatedIntent": {
                                "name": "GetMovieDetails",
                                "confirmationStatus": "NONE",
                                "slots": {
                                    "Answer": {
                                        "name": "Answer",
                                        "value": "NONE" # You can change the value of Answer to get new answer, this is allowed.
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        else : 
            # You are out of questions, now proceed to what you should do after getting all the answers.
    
  • 1

    您可以使用 slotsDialog.ElicitSlot 指令从用户获取信息 . 更具体地说,您将需要一个您将返回 Dialog.ElicitSlot 响应的插槽,并且在响应的 speechOutput 中您将在插槽中收集'll provide the list of options, and when user provides the information, it'll . 看到这个:https://developer.amazon.com/docs/custom-skills/dialog-interface-reference.html#elicitslot

    如果您正在寻找代码,这就是我在Python 2.7中所做的

    def dialog_elicit_slot(output, slotToElicit, city_name, movie_name, attributes, multiplex = None, confirmationStatus = "CONFIRMED"):
        return {
            "version": "1.0",
            "sessionAttributes": attributes,
            "response": {
                "outputSpeech": {
                    "type": "PlainText",
                    "text": output
                },
                "shouldEndSession": False,
                "directives": [
                    {
                        "type": "Dialog.ElicitSlot",
                        "slotToElicit": slotToElicit,
                        "updatedIntent": {
                            "name": "GetMovieDetails",
                            "confirmationStatus": confirmationStatus,
                            "slots": {
                                "CITY": {
                                    "name": "CITY",
                                    "confirmationStatus": "CONFIRMED" if city_name != None else "NONE",
                                    "value": city_name
                                },
                                "NAME": {
                                    "name": "NAME",
                                    "confirmationStatus": "CONFIRMED" if movie_name != None else "NONE",
                                    "value": movie_name
                                },
                                "MULTIPLEX": {
                                    "name": "MULTIPLEX",
                                    "confirmationStatus": "CONFIRMED" if multiplex != None else "NONE",
                                    "value" : multiplex
                                }
                            }
                        }
                    }
                ]
            }
        }
    

    在这里,您可以看到我有3个插槽,其中2个( CITYNAME )在技能构建器中需要 .

    这就是我的技能所做的 . 它在开始时要求电影的城市和名称(技能的调用),然后我的技能将向远程站点发出GET请求以获取多个复选框的列表 . 当我在他的特定城市拥有显示该电影的多路复用列表(用户告诉并在 NAME 插槽中收集)时,我给它们列出了多路复用(在上面的代码中只是一个字符串, output 变量) . 并且 Dialog.ElicitSlot 指令收集 slotToElicit 插槽的插槽信息(在本例中为 MULTIPLEX ) .

    如果这看起来势不可挡,您可以直接与我联系 .

相关问题