【问题标题】:How do I delegate an intent to Alexa using Python and the ask-sdk (intent chaining)?如何使用 Python 和 ask-sdk(意图链接)将意图委托给 Alexa?
【发布时间】:2020-06-26 09:52:35
【问题描述】:

在我的 Alexa-Skill 中,我问用户一个是/否问题(他是否喜欢听新闻) - 在是的部分,我想启动 NewsIntent,就好像用户手动调用它一样。

其背后的想法来自 Justin Jeffress:https://developer.amazon.com/de/blogs/alexa/post/9ffdbddb-948a-4eff-8408-7e210282ed38/intent-chaining-for-alexa-skill

handler_input.response_builder.add_directive(DelegateDirective('NewsIntent')).speak(speech_text)
return handler_input.response_builder.response

当我在开发者控制台中测试它时,我收到了 speach_text,但随后我被告知发生了错误。

这是它的 JSON 输出:

{
    "body": {
        "version": "1.0",
        "response": {
            "outputSpeech": {
                "type": "SSML",
                "ssml": "<speak>My pleasure!</speak>"
            },
            "directives": [
                {
                    "type": "Dialog.Delegate",
                    "updatedIntent": {
                        "name": "NewsIntent",
                        "confirmationStatus": "NONE",
                        "slots": {}
                    }
                }
            ],
            "type": "_DEFAULT_RESPONSE"
        },
        "sessionAttributes": {
            "IntentOrigin": null
        },
        "userAgent": "ask-python/1.13.0 Python/3.6.9 ask-webservice django-ask-sdk ask-webservice django-ask-sdk"
    }
}

有人知道如何解决这个问题吗?在这种情况下,Python 不会抛出任何异常。 (在Django下运行)

谢谢!

【问题讨论】:

    标签: python-3.x delegates alexa alexa-skills-kit


    【解决方案1】:

    终于找到答案了:

    return NewsIntentHandler.handle(self, handler_input)
    

    【讨论】:

      【解决方案2】:

      我不知道这对你是否有用。不幸的是,我无法使用您的解决方案,因为它没有重置/启动意图所需的插槽问题。它只会保留上次触发时存储的值。

      对于您的情况,我会确保您在 NewsIntent 中设置会话属性,如下所示:

      def handle(self, handler_input):
              attribute_manager = handler_input.attributes_manager
              session_attr = attribute_manager.session_attributes
              
              # Your logic for your intent here
              
              session_attr['news'] = 'some string or value'
              speak_output = "I found some news! Would you like to find more?"
              return (handler_input.response_builder.speak(speak_output).response)
      

      您需要在构建器 UI 中添加 AMAZON.YesIntent 意图。

      您需要一个类来定义如何在 AMAZON.YesIntent 触发时对其进行处理。对于这个例子,我将该类命名为MoreNewsIntentHandler。将此添加到代码的底部:

      sb.add_request_handler(MoreNewsIntentHandler())
      

      最后,在 AMAZON.YesIntent 触发时创建具有预期操作的类。

      from ask_sdk_model.intent import Intent
      from ask_sdk_model.dialog import delegate_directive
      
      class MoreNewsIntentHandler(AbstractRequestHandler):
          def can_handle(self, handler_input):
              attribute_manager = handler_input.attributes_manager
              session_attr = attribute_manager.session_attributes
              return (is_intent_name("AMAZON.YesIntent")(handler_input) and "news" in session_attr)
          
          def handle(self, handler_input):
              attribute_manager = handler_input.attributes_manager
              session_attr = attribute_manager.session_attributes
              if "news" in session_attr:
                  speak_output = "Ok. Let's get some more news"
                  intent_name = "NewsIntent"
              return handler_input.response_builder.speak(speak_output).add_directive(delegate_directive.DelegateDirective(updated_intent=Intent(name=intent_name))).response 
      
      
      

      有了这个,我计划使用会话属性来输出不同的语句,并根据会话属性识别需要触发的正确意图。我计划继续修改会话属性,以确保我可以通过它们处理自定义 AMAZON.YesIntent 操作。如果我发现更多信息,我会发布经过编辑的更新。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-26
        • 2019-06-06
        • 1970-01-01
        • 1970-01-01
        • 2019-07-11
        • 1970-01-01
        相关资源
        最近更新 更多