在 Python 中为 Alexa 技能添加会话属性

编程入门 行业动态 更新时间:2024-10-10 01:19:27
本文介绍了在 Python 中为 Alexa 技能添加会话属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的意图模式中有 3 个插槽(account、dollar_value、recipient_first)用于 Alexa 技能,我想保存任何插槽由演讲者在会话属性中提供.

I have 3 slots (account, dollar_value, recipient_first) within my intent schema for an Alexa skill and I want to save whatever slots are provided by the speaker in the session Attributes.

我使用以下方法设置会话属性:

I am using the following methods to set session attributes:

def create_dollar_value_attribute(dollar_value): return {"dollar_value": dollar_value} def create_account_attribute(account): return {"account": account} def create_recipient_first_attribute(recipient_first): return {"recipient_first": recipient_first}

然而,正如您可能猜到的,如果我想在 sessionAttributes 中保存多个插槽作为数据,sessionAttributes 将被覆盖,如下所示:>

However, as you may guess, if I want to save more than one slot as data in sessionAttributes, the sessionAttributes is overwritten as in the following case:

session_attributes = {} if session.get('attributes', {}) and "recipient_first" not in session.get('attributes', {}): recipient_first = intent['slots']['recipient_first']['value'] session_attributes = create_recipient_first_attribute(recipient_first) if session.get('attributes', {}) and "dollar_value" not in session.get('attributes', {}): dollar_value = intent['slots']['dollar_value']['value'] session_attributes = create_dollar_value_attribute(dollar_value)

来自我的 lambda 函数对语音输入的 JSON 响应,其中提供了两个插槽(dollar_value 和 recipient_first)如下(我的猜测是 第二个 if 语句中的 create_dollar_value_attribute 方法覆盖了第一个):

The JSON response from my lambda function for a speech input in which two slots (dollar_value and recipient_first) were provided is as follows (my guess is that the create_dollar_value_attribute method in the second if statement is overwriting the first):

{ "version": "1.0", "response": { "outputSpeech": { "type": "PlainText", "text": "Some text output" }, "card": { "content": "SessionSpeechlet - Some text output", "title": "SessionSpeechlet - Send Money", "type": "Simple" }, "reprompt": { "outputSpeech": { "type": "PlainText" } }, "shouldEndSession": false }, "sessionAttributes": { "dollar_value": "30" } }

sessionAttributes 的正确响应应该是:

"sessionAttributes": { "dollar_value": "30", "recipient_first": "Some Name" },

如何创建此回复?有没有更好的方法在 JSON 响应中为 sessionAttributes 添加值?

How do I create this response? Is there a better way to add values to sessionAttributes in the JSON response?

推荐答案

在我看来,使用 Python 添加 sessionAttributes 的最简单方法似乎是使用字典.例如,如果您想在会话属性中存储一些插槽以备将来使用:

The easiest way to add sessionAttributes with Python in my opinion seems to be by using a dictionary. For example, if you want to store some of the slots for future in the session attributes:

session['attributes']['slotKey'] = intent['slots']['slotKey']['value']

接下来,您可以将其传递给构建响应方法:

Next, you can just pass it on to the build response method:

buildResponse(session['attributes'], buildSpeechletResponse(title, output, reprompt, should_end_session))

本例中的实现:

def buildSpeechletResponse(title, output, reprompt_text, should_end_session): return { 'outputSpeech': { 'type': 'PlainText', 'text': output }, 'card': { 'type': 'Simple', 'title': "SessionSpeechlet - " + title, 'content': "SessionSpeechlet - " + output }, 'reprompt': { 'outputSpeech': { 'type': 'PlainText', 'text': reprompt_text } }, 'shouldEndSession': should_end_session } def buildResponse(session_attributes, speechlet_response): return { 'version': '1.0', 'sessionAttributes': session_attributes, 'response': speechlet_response }

这会以推荐的方式在 Lambda 响应 JSON 中创建 sessionAttributes.

This creates the sessionAttributes in the recommended way in the Lambda response JSON.

如果最后一个不存在,也只是添加一个新的 sessionAttribute 不会覆盖它.它只会创建一个新的键值对.

Also just adding a new sessionAttribute doesn't overwrite the last one if it doesn't exist. It will just create a new key-value pair.

请注意,这可能在服务模拟器中运行良好,但在实际 Amazon Echo 上测试时可能会返回关键属性错误.根据这个帖子,

Do note, that this may work well in the service simulator but may return a key attribute error when testing on an actual Amazon Echo. According to this post,

在 Service Simulator 上,会话以 Session:{ ... Attributes:{}, ... } 开始当会话在 Echo 上开始时,会话根本没有 Attributes 键.

On Service Simulator, sessions starts with Session:{ ... Attributes:{}, ... } When sessions start on the Echo, Session does not have an Attributes key at all.

我解决这个问题的方法是在创建新会话时在 lambda 处理程序中手动创建它:

The way I worked around this was to just manually create it in the lambda handler whenever a new session is created:

if event['session']['new']: event['session']['attributes'] = {} onSessionStarted( {'requestId': event['request']['requestId'] }, event['session']) if event['request']['type'] == 'IntentRequest': return onIntent(event['request'], event['session'])

更多推荐

在 Python 中为 Alexa 技能添加会话属性

本文发布于:2023-11-28 02:59:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640704.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中为   属性   技能   Python   Alexa

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!