如何在ASK-SDK lambda函数中获取Alexa插槽值?

时间:2018-10-14 11:50:22

标签: python-2.7 alexa alexa-skills-kit alexa-slot

我想在我的Lambda函数中访问插槽值'{cityName}'。我正在使用ASK-SDK。这样做的python代码或语法是什么?

My Alexa Console Picture

2 个答案:

答案 0 :(得分:0)

如果WeatherApiCallIntent是由CityName插槽值触发的,则请求JSON将如下所示:

"request": {
        "type": "IntentRequest",
        "requestId": "amzn1.echo-api.request.xxxxx-xxx-xxx-xx-xxxxxxx",
        "timestamp": "2018-09-12T13:35:25Z",
        "locale": "en-US",
        "intent": {
            "name": "WeatherApiCallIntent",
            "confirmationStatus": "NONE",
            "slots": {
                "CityName ": {
                    "name": "CityName",
                    "value": "Kochi",
                    ...

在您的WeatherApiCallIntent处理程序中,您可以使用

print("Slot: " + intent_request['intent']['slots']['CityName']['value'])

答案 1 :(得分:0)

以上方法是正确的,但对我不起作用。
上述问题的另一个答案如下:

item, is_resolved = util.get_intent_(slots=handler_input.request_envelope.request.intent.slots)

然后定义函数get_intent_,如下所示:

import random
import six
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_core.utils import is_request_type

def get_intent_(slots):
    item = []
    resolved_item = None
    for _,slot in six.iteritems(slots):
        if slot.value is not None:
            resolved_item = slot.value

    if resolved_item is not None:
        return resolved_item, True
    else:
        return resolved_item, False

此方法看起来很复杂,但是定义一个函数并调用它是一个好习惯。