Skip to content

Commit

Permalink
Add PlayBehavior optional parameter on response builder speak, ask me…
Browse files Browse the repository at this point in the history
…thods

This commit includes the optional parameter play_behavior on ResponseFactory's
speak and ask methods, to include the Speech Interruption property
PlayBehavior as mentioned in the Alexa Response structure definition.
  • Loading branch information
nikhilym committed Feb 15, 2019
1 parent ae9bee7 commit 00feeed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
21 changes: 15 additions & 6 deletions ask-sdk-core/ask_sdk_core/response_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ask_sdk_model import Directive
from ask_sdk_model.ui import Card
from ask_sdk_model.canfulfill import CanFulfillIntent
from ask_sdk_model.ui.play_behavior import PlayBehavior


PLAIN_TEXT_TYPE = "PlainText"
Expand All @@ -51,23 +52,27 @@ def __init__(self):
directives=None, should_end_session=None,
can_fulfill_intent=None)

def speak(self, speech):
# type: (str) -> 'ResponseFactory'
def speak(self, speech, play_behavior=None):
# type: (str, PlayBehavior) -> 'ResponseFactory'
"""Say the provided speech to the user.
:param speech: the output speech sent back to the user.
:type speech: str
:param play_behavior: attribute to control alexa's speech
interruption
:type play_behavior: ask_sdk_model.ui.play_behavior.PlayBehavior
:return: response factory with partial response being built and
access from self.response.
:rtype: ResponseFactory
"""
ssml = "<speak>{}</speak>".format(self.__trim_outputspeech(
speech_output=speech))
self.response.output_speech = SsmlOutputSpeech(ssml=ssml)
self.response.output_speech = SsmlOutputSpeech(
ssml=ssml, play_behavior=play_behavior)
return self

def ask(self, reprompt):
# type: (str) -> 'ResponseFactory'
def ask(self, reprompt, play_behavior=None):
# type: (str, PlayBehavior) -> 'ResponseFactory'
"""Provide reprompt speech to the user, if no response for
8 seconds.
Expand All @@ -76,13 +81,17 @@ def ask(self, reprompt):
:param reprompt: the output speech to reprompt.
:type reprompt: str
:param play_behavior: attribute to control alexa's speech
interruption
:type play_behavior: ask_sdk_model.ui.play_behavior.PlayBehavior
:return: response factory with partial response being built and
access from self.response.
:rtype: ResponseFactory
"""
ssml = "<speak>{}</speak>".format(self.__trim_outputspeech(
speech_output=reprompt))
output_speech = SsmlOutputSpeech(ssml=ssml)
output_speech = SsmlOutputSpeech(
ssml=ssml, play_behavior=play_behavior)
self.response.reprompt = Reprompt(output_speech=output_speech)
if not self.__is_video_app_launch_directive_present():
self.response.should_end_session = False
Expand Down
23 changes: 23 additions & 0 deletions ask-sdk-core/tests/unit/test_response_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ask_sdk_model.canfulfill import (
CanFulfillIntent, CanFulfillIntentValues, CanFulfillSlot,
CanFulfillSlotValues, CanUnderstandSlotValues)
from ask_sdk_model.ui.play_behavior import PlayBehavior

from ask_sdk_core.response_helper import (
ResponseFactory, get_text_content, get_plain_text_content,
Expand All @@ -43,6 +44,16 @@ def test_speak(self):
ssml="<speak></speak>"), (
"The speak method of ResponseFactory fails to set output speech")

def test_speak_with_play_behavior(self):
test_play_behavior = PlayBehavior.ENQUEUE
response_factory = self.response_factory.speak(
speech=None, play_behavior=test_play_behavior)

assert response_factory.response.output_speech == SsmlOutputSpeech(
ssml="<speak></speak>", play_behavior=test_play_behavior), (
"The speak method of ResponseFactory fails to set play behavior "
"on output speech")

def test_ask(self):
response_factory = self.response_factory.ask(reprompt=None)

Expand All @@ -53,6 +64,18 @@ def test_ask(self):
"The ask method of ResponseFactory fails to set the "
"should_end_session to False")

def test_ask_with_play_behavior(self):
test_play_behavior = PlayBehavior.REPLACE_ALL
response_factory = self.response_factory.ask(
reprompt=None, play_behavior=test_play_behavior)

assert response_factory.response.reprompt == Reprompt(
output_speech=SsmlOutputSpeech(
ssml="<speak></speak>",
play_behavior=test_play_behavior)), (
"The ask method of ResponseFactory fails to set play behavior "
"on reprompt output speech")

def test_ask_with_video_app_launch_directive(self):
directive = LaunchDirective(video_item=VideoItem(
source=None, metadata=Metadata(title=None, subtitle=None)))
Expand Down

0 comments on commit 00feeed

Please sign in to comment.