コード例 #1
0
def build_response(handler_input,
                   card_title,
                   card_text,
                   speech_text,
                   img_tuple=None,
                   end_session=False):
    if handler_input.attributes_manager.session_attributes["drunk_mode_state"]:
        speech_text = '<prosody rate="slow"><emphasis level="strong">' + speech_text + '</emphasis></prosody>'

    if img_tuple and supports_display(handler_input):
        card = StandardCard(card_title, card_text,
                            ui.Image(img_tuple[1], img_tuple[1]))
        img = Image('Meme', [ImageInstance(url=img_tuple[1])])
        reddit_text = TextContent(primary_text=PlainText(img_tuple[0]),
                                  secondary_text=PlainText(img_tuple[2]))
        directive = RenderTemplateDirective(
            BodyTemplate2(back_button=BackButtonBehavior.VISIBLE,
                          image=img,
                          title=card_title,
                          text_content=reddit_text))
        handler_input.response_builder.speak(speech_text).set_card(
            card).add_directive(directive).set_should_end_session(end_session)
    else:
        card = SimpleCard(card_title, card_text)
        handler_input.response_builder.speak(speech_text).set_card(
            card).set_should_end_session(end_session)
    return handler_input.response_builder.response
    def test_build_primary_text(self):
        text_val = "test"

        rich_text = RichText(text=text_val)
        text_content = TextContent(primary_text=rich_text)

        assert get_rich_text_content(primary_text=text_val) == text_content, \
            "get_rich_text_content helper returned wrong text content " \
            "with primary text"
    def test_build_primary_text(self):
        text_val = "test"

        plain_text = PlainText(text=text_val)
        text_content = TextContent(primary_text=plain_text)

        assert get_plain_text_content(primary_text=text_val) == text_content, \
            "get_plain_text_content helper returned wrong text content " \
            "with primary text"
    def test_build_tertiary_text_default(self):
        text_val = "test"

        plain_text = PlainText(text=text_val)
        text_content = TextContent(tertiary_text=plain_text)

        assert get_text_content(tertiary_text=text_val) == text_content, \
            "get_text_content helper returned wrong text content with " \
            "tertiary text and default type"
    def test_build_tertiary_text_rich(self):
        text_val = "test"

        plain_text = RichText(text=text_val)
        text_content = TextContent(tertiary_text=plain_text)

        assert get_text_content(
            tertiary_text=text_val, tertiary_text_type=RICH_TEXT_TYPE) == text_content, \
            "get_text_content helper returned wrong text content with " \
            "tertiary text and rich type"
コード例 #6
0
def get_text_content(primary_text=None,
                     primary_text_type=PLAIN_TEXT_TYPE,
                     secondary_text=None,
                     secondary_text_type=PLAIN_TEXT_TYPE,
                     tertiary_text=None,
                     tertiary_text_type=PLAIN_TEXT_TYPE):
    # type: (str, str, str, str, str, str) -> TextContent
    """Responsible for building text content object using ask-sdk-model
    in Alexa skills kit display interface.
    https://developer.amazon.com/docs/custom-skills/display-interface-reference.html#textcontent-object-specifications.

    :param primary_text: Text for primary_text field
    :type primary_text: (optional) str
    :param primary_text_type: Type of the primary text field. Allowed
        values are `PlainText` and `RichText`.
        Defaulted to `PlainText`.
    :param secondary_text: Text for secondary_text field
    :type primary_text_type: (optional) str
    :type secondary_text: (optional) str
    :param secondary_text_type: Type of the secondary text field.
        Allowed values are `PlainText` and `RichText`.
        Defaulted to `PlainText`.
    :param tertiary_text: Text for tertiary_text field
    :type tertiary_text: (optional) str
    :param tertiary_text_type: Type of the tertiary text field.
        Allowed values are `PlainText` and `RichText`.
        Defaulted to `PlainText`.
    :return: Text Content instance with primary, secondary and tertiary
        text set.
    :rtype: TextContent
    :raises: ValueError
    """
    text_content = TextContent()
    if primary_text:
        text_content.primary_text = __set_text_field(primary_text,
                                                     primary_text_type)
    if secondary_text:
        text_content.secondary_text = __set_text_field(secondary_text,
                                                       secondary_text_type)
    if tertiary_text:
        text_content.tertiary_text = __set_text_field(tertiary_text,
                                                      tertiary_text_type)
    return text_content
コード例 #7
0
    def render(handler_input, message):
        # Check for display
        if not handler_input.request_envelope.context.system.device.supported_interfaces.display:
            print('No display to render.')
            return

        if not message.get('display_text'):
            print('Render template without primary text!')

        text = message.get('display_text', '')
        if isinstance(text, list):
            text = settings.pick_random(text)

        subtext = message.get('display_subtext', '')
        if isinstance(subtext, list):
            subtext = settings.pick_random(subtext)

        background = (
            message.get('bg_image') or
            settings.pick_random(settings.IMAGES['background'])
        )

        data = {
            'back_button': BackButtonBehavior('HIDDEN'),
            'background_image': Image(sources=[ImageInstance(url=background)]),
            'title': message['display_title'],
            'text_content': TextContent(PlainText(text), PlainText(subtext)),
        }

        if message.get('image'):
            image = Image(sources=[ImageInstance(url=message['image'])])
            template = BodyTemplate3(**data, image=image)
        else:
            template = BodyTemplate1(**data)

        directive = RenderTemplateDirective(template=template)

        handler_input.attributes_manager.request_attributes['directives'].append(directive)