Skip to content

Commit 00feeed

Browse files
committed
Add PlayBehavior optional parameter on response builder speak, ask methods
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.
1 parent ae9bee7 commit 00feeed

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

ask-sdk-core/ask_sdk_core/response_helper.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from ask_sdk_model import Directive
2828
from ask_sdk_model.ui import Card
2929
from ask_sdk_model.canfulfill import CanFulfillIntent
30+
from ask_sdk_model.ui.play_behavior import PlayBehavior
3031

3132

3233
PLAIN_TEXT_TYPE = "PlainText"
@@ -51,23 +52,27 @@ def __init__(self):
5152
directives=None, should_end_session=None,
5253
can_fulfill_intent=None)
5354

54-
def speak(self, speech):
55-
# type: (str) -> 'ResponseFactory'
55+
def speak(self, speech, play_behavior=None):
56+
# type: (str, PlayBehavior) -> 'ResponseFactory'
5657
"""Say the provided speech to the user.
5758
5859
:param speech: the output speech sent back to the user.
5960
:type speech: str
61+
:param play_behavior: attribute to control alexa's speech
62+
interruption
63+
:type play_behavior: ask_sdk_model.ui.play_behavior.PlayBehavior
6064
:return: response factory with partial response being built and
6165
access from self.response.
6266
:rtype: ResponseFactory
6367
"""
6468
ssml = "<speak>{}</speak>".format(self.__trim_outputspeech(
6569
speech_output=speech))
66-
self.response.output_speech = SsmlOutputSpeech(ssml=ssml)
70+
self.response.output_speech = SsmlOutputSpeech(
71+
ssml=ssml, play_behavior=play_behavior)
6772
return self
6873

69-
def ask(self, reprompt):
70-
# type: (str) -> 'ResponseFactory'
74+
def ask(self, reprompt, play_behavior=None):
75+
# type: (str, PlayBehavior) -> 'ResponseFactory'
7176
"""Provide reprompt speech to the user, if no response for
7277
8 seconds.
7378
@@ -76,13 +81,17 @@ def ask(self, reprompt):
7681
7782
:param reprompt: the output speech to reprompt.
7883
:type reprompt: str
84+
:param play_behavior: attribute to control alexa's speech
85+
interruption
86+
:type play_behavior: ask_sdk_model.ui.play_behavior.PlayBehavior
7987
:return: response factory with partial response being built and
8088
access from self.response.
8189
:rtype: ResponseFactory
8290
"""
8391
ssml = "<speak>{}</speak>".format(self.__trim_outputspeech(
8492
speech_output=reprompt))
85-
output_speech = SsmlOutputSpeech(ssml=ssml)
93+
output_speech = SsmlOutputSpeech(
94+
ssml=ssml, play_behavior=play_behavior)
8695
self.response.reprompt = Reprompt(output_speech=output_speech)
8796
if not self.__is_video_app_launch_directive_present():
8897
self.response.should_end_session = False

ask-sdk-core/tests/unit/test_response_helper.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from ask_sdk_model.canfulfill import (
2727
CanFulfillIntent, CanFulfillIntentValues, CanFulfillSlot,
2828
CanFulfillSlotValues, CanUnderstandSlotValues)
29+
from ask_sdk_model.ui.play_behavior import PlayBehavior
2930

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

47+
def test_speak_with_play_behavior(self):
48+
test_play_behavior = PlayBehavior.ENQUEUE
49+
response_factory = self.response_factory.speak(
50+
speech=None, play_behavior=test_play_behavior)
51+
52+
assert response_factory.response.output_speech == SsmlOutputSpeech(
53+
ssml="<speak></speak>", play_behavior=test_play_behavior), (
54+
"The speak method of ResponseFactory fails to set play behavior "
55+
"on output speech")
56+
4657
def test_ask(self):
4758
response_factory = self.response_factory.ask(reprompt=None)
4859

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

67+
def test_ask_with_play_behavior(self):
68+
test_play_behavior = PlayBehavior.REPLACE_ALL
69+
response_factory = self.response_factory.ask(
70+
reprompt=None, play_behavior=test_play_behavior)
71+
72+
assert response_factory.response.reprompt == Reprompt(
73+
output_speech=SsmlOutputSpeech(
74+
ssml="<speak></speak>",
75+
play_behavior=test_play_behavior)), (
76+
"The ask method of ResponseFactory fails to set play behavior "
77+
"on reprompt output speech")
78+
5679
def test_ask_with_video_app_launch_directive(self):
5780
directive = LaunchDirective(video_item=VideoItem(
5881
source=None, metadata=Metadata(title=None, subtitle=None)))

0 commit comments

Comments
 (0)