使用 Python SDK 在执行操作之前让 Alexa 说话
Make Alexa speak before executing action with Python SDK
该用例与已经在使用的东西非常相似。我想让 Alexa 说些什么,然后执行一个动作。例如,通过 Spotify 集成,我可以要求 Alexa 播放播放列表:
- Alexa 播放摇滚播放列表
- Alexa 说“我将播放 spotify 中的摇滚经典播放列表”
- Alexa 继续播放播放列表。
请注意,Alexa 在实际发送命令到 Spotify API 要求播放播放列表之前先说话。所以我的问题是,如何使用 python SDK 实现相同的行为?
class GetNewFactHandler(AbstractRequestHandler):
"""Handler for Skill Launch and GetNewFact Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return (is_request_type("LaunchRequest")(handler_input) or
is_intent_name("GetNewFactIntent")(handler_input))
def handle(self, handler_input):
# type: (HandlerInput) -> Response
logger.info("In GetNewFactHandler")
print("Running action here")
speak = "I will run the action"
return (handler_input.response_builder.speak(speak).response)
我有一些类似于上面的代码。但它总是在 Alexa 说它将执行之前执行操作。有没有办法将回调传递给响应生成器?我是SDK的新手,如果太明显请见谅。
您可以使用Progressive Response
Your skill can send progressive responses to keep the user engaged while your skill prepares a full response to the user's request. A progressive response is interstitial SSML content (including text-to-speech and short audio) that Alexa plays while waiting for your full skill response.
请注意,Alexa 不会在说出语音后调用 API,它会在 API 响应用户时调用 API看起来很光滑。
def get_progressive_response(handler_input):
# type: (HandlerInput) -> None
request_id_holder = handler_input.request_envelope.request.request_id
directive_header = Header(request_id=request_id_holder)
speech = SpeakDirective(speech="Ok, give me a minute")
directive_request = SendDirectiveRequest(
header=directive_header, directive=speech)
directive_service_client = handler_input.service_client_factory.get_directive_service()
directive_service_client.enqueue(directive_request)
time.sleep(5)
return
class HelloWorldIntentHandler(AbstractRequestHandler):
# Handler for Hello World Intent
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("HelloWorldIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
speech_text = "Hello World!"
get_progressive_response(handler_input)
handler_input.response_builder.speak(speech_text).set_card(
SimpleCard("Hello World", speech_text)).set_should_end_session(
False)
return handler_input.response_builder.response
该用例与已经在使用的东西非常相似。我想让 Alexa 说些什么,然后执行一个动作。例如,通过 Spotify 集成,我可以要求 Alexa 播放播放列表:
- Alexa 播放摇滚播放列表
- Alexa 说“我将播放 spotify 中的摇滚经典播放列表”
- Alexa 继续播放播放列表。
请注意,Alexa 在实际发送命令到 Spotify API 要求播放播放列表之前先说话。所以我的问题是,如何使用 python SDK 实现相同的行为?
class GetNewFactHandler(AbstractRequestHandler):
"""Handler for Skill Launch and GetNewFact Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return (is_request_type("LaunchRequest")(handler_input) or
is_intent_name("GetNewFactIntent")(handler_input))
def handle(self, handler_input):
# type: (HandlerInput) -> Response
logger.info("In GetNewFactHandler")
print("Running action here")
speak = "I will run the action"
return (handler_input.response_builder.speak(speak).response)
我有一些类似于上面的代码。但它总是在 Alexa 说它将执行之前执行操作。有没有办法将回调传递给响应生成器?我是SDK的新手,如果太明显请见谅。
您可以使用Progressive Response
Your skill can send progressive responses to keep the user engaged while your skill prepares a full response to the user's request. A progressive response is interstitial SSML content (including text-to-speech and short audio) that Alexa plays while waiting for your full skill response.
请注意,Alexa 不会在说出语音后调用 API,它会在 API 响应用户时调用 API看起来很光滑。
def get_progressive_response(handler_input):
# type: (HandlerInput) -> None
request_id_holder = handler_input.request_envelope.request.request_id
directive_header = Header(request_id=request_id_holder)
speech = SpeakDirective(speech="Ok, give me a minute")
directive_request = SendDirectiveRequest(
header=directive_header, directive=speech)
directive_service_client = handler_input.service_client_factory.get_directive_service()
directive_service_client.enqueue(directive_request)
time.sleep(5)
return
class HelloWorldIntentHandler(AbstractRequestHandler):
# Handler for Hello World Intent
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("HelloWorldIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
speech_text = "Hello World!"
get_progressive_response(handler_input)
handler_input.response_builder.speak(speech_text).set_card(
SimpleCard("Hello World", speech_text)).set_should_end_session(
False)
return handler_input.response_builder.response