コード例 #1
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
    def test_build_primary_text_rich(self):
        text_val = "test"

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

        assert get_text_content(
            primary_text=text_val, primary_text_type=RICH_TEXT_TYPE) == text_content, \
            "get_text_content helper returned wrong text content with " \
            "primary text and rich type"
コード例 #3
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)