[英]How to make REST API calls to Amazon Alexa
I am building a custom Alexa skill and want to make REST API calls. 我正在构建一个自定义的Alexa技能,并希望进行REST API调用。 Currently, I am able to make GET request to fetch data from my web service which is then used by Alexa.
目前,我能够发出GET请求以从我的Web服务获取数据,然后由Alexa使用。 However, I have the following requirements and I am not sure how to go about developing this.
但是,我有以下要求,我不知道如何开发这个。
How do I implement 3, 4, and 5? 我该如何实现3,4和5? I am currently using Python 3.6 to write the lambda function at AWS developer console.
我目前正在使用Python 3.6在AWS开发人员控制台上编写lambda函数。 Are there any Amazon Alexa APIs guide for Python 3.6.
是否有适用于Python 3.6的Amazon Alexa API指南。
How do I do this in Java which is my preferred way? 我如何在Java中这样做,这是我的首选方式?
I followed the instructions here to develop what I currently have: https://github.com/simonprickett/alexabart 我按照这里的说明开发了我目前的产品: https : //github.com/simonprickett/alexabart
Is there any detailed documentation available on how to write Alexa specific lambda function and its associated API guide for Python3 or Java. 是否有关于如何编写Alexa特定lambda函数及其相关的Python3或Java API指南的详细文档。
Thanks 谢谢
You can use slots
and Dialog.ElicitSlot
directives, to get the information from the user. 您可以使用
slots
和Dialog.ElicitSlot
指令来从用户获取信息。 More specifically, you'll need a slot for which you will return Dialog.ElicitSlot
response, and in the speechOutput
of the response you'll provide the list of options, and when user provides the information, it'll be collected in the slot. 更具体地说,您将需要一个插槽,您将返回
Dialog.ElicitSlot
响应,并在响应的speechOutput
中提供选项列表,当用户提供信息时,它将被收集在插槽中。 see this : https://developer.amazon.com/docs/custom-skills/dialog-interface-reference.html#elicitslot 请看: https : //developer.amazon.com/docs/custom-skills/dialog-interface-reference.html#elicitslot
If you're looking for code, this is what I did in Python 2.7 如果您正在寻找代码,这就是我在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
}
}
}
}
]
}
}
Here, you can see I had 3 slots, out of which 2 ( CITY
and NAME
) were made required in the skill builder. 在这里,您可以看到我有3个插槽,其中2个(
CITY
和NAME
)在技能构建器中是必需的。
This is what my skill does. 这就是我的技能所做的。 It asks for the city and name of the movie in the beginning (invokation of the skill), then my skill would make a GET request to remote site to get the list of mulitplexes.
它在开始时要求电影的城市和名称(技能的调用),然后我的技能将向远程站点发出GET请求以获取多个复选框列表。 And when I had the list of multiplexes which show that movie(which the user told and is collected in
NAME
slot) in his particular city, I give them the list of Multiplexes (which is just a string, output
variable in the above code). 当我在他的特定城市中有多路复用列表显示该电影(用户告诉并在
NAME
插槽中收集)时,我给他们多路复用器列表(这只是一个字符串,上面代码中的output
变量) 。 And Dialog.ElicitSlot
directive collects the slot information for the slotToElicit
slot(which in this case is MULTIPLEX
). Dialog.ElicitSlot
指令收集slotToElicit
插槽(在本例中为MULTIPLEX
)的插槽信息。
If this looks overwhelming, you can just contact me directly. 如果这看起来势不可挡,您可以直接与我联系。
If you have a lot of questions and you want each of them answered, you can use sessionAttributes
variable. 如果您有很多问题,并且希望每个问题都得到解答,则可以使用
sessionAttributes
变量。 The session variable can retain it's value through out the course of the session. 会话变量可以在会话过程中保留其值。 You can make a dictionary to be saved in your
sessionAttributes
(It has to be a dictionary). 您可以将字典保存在
sessionAttributes
(它必须是字典)。
You can save something like this in your session variable. 您可以在会话变量中保存这样的内容。
sessionAttributes: {
"AllQuestions" : ["string", "string"],
"LastQuestionIndex" : integer
}
I'm assuming you're able to get the list of questions (using a GET request). 我假设您能够获得问题列表(使用GET请求)。
Answer
would be a slot which is going to store your answer. Answer
将是一个存储您的答案的插槽。
When your intent has just started and you don't have anything in your sessionAttributes
(use a simple if-else) you'll have to make the GET request and gather all of your questions (maybe in a list or something). 当你的意图刚刚开始并且你的
sessionAttributes
没有任何东西时(使用简单的if-else)你必须发出GET请求并收集你所有的问题(可能在列表或其他东西中)。 You make your GET request and store all the questions in your sessionAttributes['AllQuestions']
. 您发出GET请求并将所有问题存储在
sessionAttributes['AllQuestions']
。 And set LastQuestionIndex = -1
. 并设置
LastQuestionIndex = -1
。
Now the tricky part comes in. (I'm also assuming you're able to use Dialog.ElicitSlot
Directive). 现在棘手的部分进来了。 (我还假设你能够使用
Dialog.ElicitSlot
指令)。
Now you have a list of all the questions and you also have the index of last question that was asked. 现在您有一个所有问题的列表,并且您还有最后一个问题的索引。 And now you just have to increment the index, get the Next Question, and use
Dialog.ElicitSlot
Directive to ask this new question. 现在你只需要增加索引,得到下一个问题,并使用
Dialog.ElicitSlot
指令来提出这个新问题。 And update the LastQuestionIndex
in your sessionAttributes. 并更新
LastQuestionIndex
中的LastQuestionIndex。
Before proceeding to next question, you'll also have to check if the slot Answer
has any value or not? 在继续下一个问题之前,您还必须检查插槽
Answer
是否有任何价值? If it does have a value (it is not "None"), then you can use the LastQuestionIndex
variable and store the answer for that particular question. 如果它确实有一个值(它不是“None”),那么您可以使用
LastQuestionIndex
变量并存储该特定问题的答案。
If you're looking for code, here you go: 如果您正在寻找代码,请转到:
# 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.