CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-line-bot-sdk

LINE Messaging API SDK for Python with comprehensive bot development capabilities including messaging, webhooks, and platform integrations.

Pending
Overview
Eval results
Files

rich-messages.mddocs/

Rich Messages and Templates

Advanced message formatting including Flex Messages, templates, quick replies, and interactive components. Supports complex layouts, carousels, image maps, and rich interactive experiences for enhanced user engagement.

Capabilities

Template Messages

Pre-defined message templates for common interactive patterns including buttons, carousels, and confirmations.

class TemplateMessage:
    def __init__(self, alt_text: str, template: Template):
        """
        Create a template-based message with interactive elements.
        
        Args:
            alt_text: Alternative text for notifications and accessibility
            template: Template object (ButtonsTemplate, CarouselTemplate, etc.)
        """

class Template:
    """Base template class"""
    type: str

class ButtonsTemplate(Template):
    def __init__(
        self, 
        text: str, 
        actions: List[Action],
        thumbnail_image_url: Optional[str] = None,
        image_aspect_ratio: Optional[TemplateImageAspectRatio] = None,
        image_size: Optional[TemplateImageSize] = None,
        image_background_color: Optional[str] = None,
        title: Optional[str] = None
    ):
        """
        Template with text, optional image, and action buttons.
        
        Args:
            text: Message text (max 400 characters)
            actions: List of action objects (max 4)
            thumbnail_image_url: Optional header image URL
            title: Optional title text
        """

class ConfirmTemplate(Template):
    def __init__(self, text: str, actions: List[Action]):
        """
        Confirmation template with two action buttons.
        
        Args:
            text: Confirmation message text
            actions: Exactly 2 action objects (typically "Yes" and "No")
        """

class CarouselTemplate(Template):
    def __init__(
        self, 
        columns: List[CarouselColumn],
        image_aspect_ratio: Optional[TemplateImageAspectRatio] = None,
        image_size: Optional[TemplateImageSize] = None
    ):
        """
        Horizontally scrollable carousel of items.
        
        Args:
            columns: List of carousel column objects (max 10)
            image_aspect_ratio: Aspect ratio for all images
            image_size: Size setting for all images
        """

class ImageCarouselTemplate(Template):
    def __init__(self, columns: List[ImageCarouselColumn]):
        """
        Carousel of images with associated actions.
        
        Args:
            columns: List of image carousel columns (max 10)
        """

Flex Messages

Highly customizable messages with complex layouts using flexible box model design.

class FlexMessage:
    def __init__(self, alt_text: str, contents: FlexContainer):
        """
        Create a flexible layout message with complex visual design.
        
        Args:
            alt_text: Alternative text for notifications
            contents: Flex container (FlexBubble or FlexCarousel)
        """

class FlexContainer:
    """Base flex container class"""
    type: str

class FlexBubble(FlexContainer):
    def __init__(
        self,
        header: Optional[FlexBox] = None,
        hero: Optional[FlexComponent] = None,
        body: Optional[FlexBox] = None,
        footer: Optional[FlexBox] = None,
        styles: Optional[FlexBubbleStyles] = None,
        direction: Optional[str] = None,
        size: Optional[str] = None
    ):
        """
        Single bubble container with header, hero, body, and footer sections.
        
        Args:
            header: Optional header section
            hero: Optional hero image/video section
            body: Main content section
            footer: Optional footer section with actions
            styles: Custom styling options
            direction: Text direction (ltr, rtl)
        """

class FlexCarousel(FlexContainer):
    def __init__(self, contents: List[FlexBubble]):
        """
        Horizontally scrollable carousel of flex bubbles.
        
        Args:
            contents: List of FlexBubble objects (max 12)
        """

Flex Components

Individual components for building flex message layouts.

class FlexComponent:
    """Base flex component class"""
    type: str

class FlexBox(FlexComponent):
    def __init__(
        self,
        layout: str,
        contents: List[FlexComponent],
        flex: Optional[int] = None,
        spacing: Optional[FlexBoxSpacing] = None,
        margin: Optional[FlexMargin] = None,
        padding_all: Optional[str] = None,
        padding_top: Optional[str] = None,
        padding_bottom: Optional[str] = None,
        padding_start: Optional[str] = None,
        padding_end: Optional[str] = None,
        background: Optional[FlexBoxBackground] = None,
        border_width: Optional[FlexBoxBorderWidth] = None,
        corner_radius: Optional[FlexBoxCornerRadius] = None
    ):
        """
        Container component for grouping other components.
        
        Args:
            layout: Layout direction ("vertical" or "horizontal") 
            contents: List of child components
            flex: Flex weight for space allocation
            spacing: Space between child components
            margin: External spacing
            padding_*: Internal padding options
            background: Background styling
        """

class FlexText(FlexComponent):
    def __init__(
        self,
        text: str,
        flex: Optional[int] = None,
        margin: Optional[FlexMargin] = None,
        size: Optional[FlexTextFontSize] = None,
        align: Optional[str] = None,
        gravity: Optional[str] = None,
        wrap: Optional[bool] = None,
        max_lines: Optional[int] = None,
        weight: Optional[str] = None,
        color: Optional[str] = None,
        style: Optional[str] = None,
        decoration: Optional[str] = None
    ):
        """
        Text component with rich formatting options.
        
        Args:
            text: Text content with emoji and variable support
            flex: Flex weight for space allocation
            size: Font size (xxs, xs, sm, md, lg, xl, xxl, 3xl, 4xl, 5xl)
            align: Text alignment (start, end, center)
            gravity: Vertical alignment (top, bottom, center)
            wrap: Enable text wrapping
            weight: Font weight (regular, bold)
            color: Text color (hex format)
            style: Text style (normal, italic)
        """

class FlexImage(FlexComponent):
    def __init__(
        self,
        url: str,
        flex: Optional[int] = None,
        margin: Optional[FlexMargin] = None,
        align: Optional[str] = None,
        gravity: Optional[str] = None,
        size: Optional[FlexImageSize] = None,
        aspect_ratio: Optional[str] = None,
        aspect_mode: Optional[str] = None,
        background_color: Optional[str] = None,
        action: Optional[Action] = None
    ):
        """
        Image component with flexible sizing and positioning.
        
        Args:
            url: Image URL (HTTPS required)
            flex: Flex weight for space allocation
            size: Image size (xxs to 5xl or full)
            aspect_ratio: Width:height ratio (e.g., "1:1", "3:2")
            aspect_mode: How image fits container (fit, fill)
            action: Action when image is tapped
        """

class FlexButton(FlexComponent):
    def __init__(
        self,
        action: Action,
        flex: Optional[int] = None,
        margin: Optional[FlexMargin] = None,
        height: Optional[str] = None,
        style: Optional[str] = None,
        color: Optional[str] = None,
        gravity: Optional[str] = None
    ):
        """
        Interactive button component.
        
        Args:
            action: Action to perform when button is pressed
            flex: Flex weight for space allocation
            height: Button height (sm, md)
            style: Button style (link, primary, secondary)
            color: Button color (hex format)
            gravity: Vertical alignment
        """

class FlexSeparator(FlexComponent):
    def __init__(
        self,
        margin: Optional[FlexMargin] = None,
        color: Optional[str] = None
    ):
        """
        Visual separator line component.
        
        Args:
            margin: External spacing
            color: Separator color (hex format)
        """

class FlexIcon(FlexComponent):
    def __init__(
        self,
        url: str,
        margin: Optional[FlexMargin] = None,
        size: Optional[FlexIconSize] = None,
        aspect_ratio: Optional[str] = None
    ):
        """
        Small icon component for decorative elements.
        
        Args:
            url: Icon image URL (HTTPS required)
            size: Icon size (xxs to 5xl)
            aspect_ratio: Width:height ratio
        """

Actions

Interactive elements that trigger behaviors when tapped.

class Action:
    """Base action class"""
    type: str

class PostbackAction(Action):
    def __init__(
        self,
        data: str,
        label: Optional[str] = None,
        display_text: Optional[str] = None,
        text: Optional[str] = None
    ):
        """
        Action that sends postback data to webhook.
        
        Args:
            data: Postback data string (max 300 characters)
            label: Button label text
            display_text: Text shown in chat when button is pressed
        """

class MessageAction(Action):
    def __init__(self, text: str, label: Optional[str] = None):
        """
        Action that sends a message as if user typed it.
        
        Args:
            text: Message text to send
            label: Button label text
        """

class URIAction(Action):
    def __init__(self, uri: str, label: Optional[str] = None, alt_uri: Optional[AltUri] = None):
        """
        Action that opens a URL or deep link.
        
        Args:
            uri: URL, telephone number, or app deep link
            label: Button label text
            alt_uri: Alternative URI for different platforms
        """

class DatetimePickerAction(Action):
    def __init__(
        self,
        data: str,
        mode: str,
        label: Optional[str] = None,
        initial: Optional[str] = None,
        max: Optional[str] = None,
        min: Optional[str] = None
    ):
        """
        Action that opens date/time picker.
        
        Args:
            data: Postback data when date/time is selected
            mode: Picker mode ("date", "time", or "datetime")
            label: Button label text
            initial: Initial value in ISO format
            max: Maximum selectable value
            min: Minimum selectable value
        """

class CameraAction(Action):
    def __init__(self, label: str):
        """Action that opens device camera."""

class CameraRollAction(Action):
    def __init__(self, label: str):
        """Action that opens device photo gallery."""

class LocationAction(Action):
    def __init__(self, label: str):
        """Action that opens location picker."""

class RichMenuSwitchAction(Action):
    def __init__(self, rich_menu_alias_id: str, data: str):
        """
        Action that switches to a different rich menu.
        
        Args:
            rich_menu_alias_id: Target rich menu alias
            data: Postback data for the switch
        """

class ClipboardAction(Action):
    def __init__(self, clipboard_text: str, label: Optional[str] = None):
        """
        Action that copies text to device clipboard.
        
        Args:
            clipboard_text: Text to copy to clipboard
            label: Button label text
        """

Quick Replies

Fast-access reply options displayed above the input field.

class QuickReply:
    def __init__(self, items: List[QuickReplyItem]):
        """
        Quick reply interface with predefined response options.
        
        Args:
            items: List of quick reply items (max 13)
        """

class QuickReplyItem:
    def __init__(
        self,
        action: Action,
        image_url: Optional[str] = None
    ):
        """
        Individual quick reply button.
        
        Args:
            action: Action to perform when item is tapped
            image_url: Optional icon image URL
        """

Image Maps

Interactive images with clickable areas that trigger actions.

class ImagemapMessage:
    def __init__(
        self,
        base_url: str,
        alt_text: str,
        base_size: ImagemapBaseSize,
        actions: List[ImagemapAction],
        video: Optional[ImagemapVideo] = None,
        external_link: Optional[ImagemapExternalLink] = None
    ):
        """
        Interactive image with clickable hotspots.
        
        Args:
            base_url: Base URL for image files (without file extension)
            alt_text: Alternative text for accessibility
            base_size: Original image dimensions
            actions: List of clickable areas and their actions
            video: Optional video overlay
            external_link: Optional external link area
        """

class ImagemapBaseSize:
    def __init__(self, width: int, height: int):
        """
        Original image dimensions in pixels.
        
        Args:
            width: Image width (max 1040px)
            height: Image height (max 1040px)
        """

class ImagemapAction:
    """Base imagemap action class"""
    type: str
    area: ImagemapArea

class URIImagemapAction(ImagemapAction):
    def __init__(self, uri: str, area: ImagemapArea):
        """Imagemap action that opens a URL."""

class MessageImagemapAction(ImagemapAction):
    def __init__(self, text: str, area: ImagemapArea):
        """Imagemap action that sends a message."""

class ImagemapArea:
    def __init__(self, x: int, y: int, width: int, height: int):
        """
        Rectangular clickable area on imagemap.
        
        Args:
            x: Left edge X coordinate
            y: Top edge Y coordinate  
            width: Area width
            height: Area height
        """

Usage Examples

Basic Template Messages

from linebot.v3.messaging.models import (
    TemplateMessage, ButtonsTemplate, ConfirmTemplate,
    PostbackAction, URIAction, MessageAction
)

# Buttons template
buttons_template = TemplateMessage(
    alt_text="Choose an option",
    template=ButtonsTemplate(
        text="What would you like to do?",
        title="Main Menu",
        actions=[
            PostbackAction(label="View Profile", data="action=profile"),
            URIAction(label="Visit Website", uri="https://example.com"),
            MessageAction(label="Say Hello", text="Hello!"),
        ]
    )
)

# Confirmation template
confirm_template = TemplateMessage(
    alt_text="Confirm action",
    template=ConfirmTemplate(
        text="Are you sure you want to delete this item?",
        actions=[
            PostbackAction(label="Yes", data="delete=confirm"),
            PostbackAction(label="No", data="delete=cancel")
        ]
    )
)

Carousel Template

from linebot.v3.messaging.models import (
    CarouselTemplate, CarouselColumn, TemplateImageAspectRatio
)

carousel_template = TemplateMessage(
    alt_text="Product catalog",
    template=CarouselTemplate(
        columns=[
            CarouselColumn(
                thumbnail_image_url="https://example.com/product1.jpg",
                title="Product 1",
                text="Amazing product description",
                actions=[
                    PostbackAction(label="Buy", data="buy=product1"),
                    URIAction(label="Details", uri="https://example.com/product1")
                ]
            ),
            CarouselColumn(
                thumbnail_image_url="https://example.com/product2.jpg", 
                title="Product 2",
                text="Another great product",
                actions=[
                    PostbackAction(label="Buy", data="buy=product2"),
                    URIAction(label="Details", uri="https://example.com/product2")
                ]
            )
        ],
        image_aspect_ratio=TemplateImageAspectRatio.RECTANGLE,
        image_size="cover"
    )
)

Flex Message Examples

from linebot.v3.messaging.models import (
    FlexMessage, FlexBubble, FlexBox, FlexText, FlexImage, 
    FlexButton, FlexSeparator, PostbackAction
)

# Product card flex message
product_card = FlexMessage(
    alt_text="Product Card",
    contents=FlexBubble(
        hero=FlexImage(
            url="https://example.com/product.jpg",
            size="full",
            aspect_ratio="20:13",
            aspect_mode="cover"
        ),
        body=FlexBox(
            layout="vertical",
            contents=[
                FlexText(
                    text="Premium Headphones",
                    weight="bold", 
                    size="xl",
                    color="#333333"
                ),
                FlexText(
                    text="$299.99",
                    size="lg",
                    color="#E74C3C",
                    weight="bold",
                    margin="md"
                ),
                FlexSeparator(margin="lg"),
                FlexText(
                    text="High-quality wireless headphones with noise cancellation and premium sound quality.",
                    wrap=True,
                    margin="lg",
                    size="sm",
                    color="#666666"
                )
            ]
        ),
        footer=FlexBox(
            layout="horizontal",
            contents=[
                FlexButton(
                    action=PostbackAction(
                        label="Add to Cart",
                        data="action=add_cart&product=headphones"
                    ),
                    style="primary",
                    color="#1DB954"
                ),
                FlexButton(
                    action=URIAction(
                        label="View Details", 
                        uri="https://example.com/headphones"
                    ),
                    style="secondary"
                )
            ],
            spacing="sm"
        )
    )
)

Complex Flex Layout

# Multi-section flex bubble with rich content
complex_flex = FlexMessage(
    alt_text="Event Details",
    contents=FlexBubble(
        header=FlexBox(
            layout="vertical",
            contents=[
                FlexText(
                    text="UPCOMING EVENT",
                    size="sm",
                    color="#FFFFFF",
                    weight="bold"
                )
            ],
            background=FlexBoxBackground(type="linearGradient"),
            padding_all="lg"
        ),
        hero=FlexImage(
            url="https://example.com/event.jpg",
            size="full",
            aspect_ratio="20:13",
            aspect_mode="cover"
        ),
        body=FlexBox(
            layout="vertical",
            contents=[
                FlexText(text="Tech Conference 2024", weight="bold", size="xl"),
                FlexBox(
                    layout="horizontal",
                    contents=[
                        FlexBox(
                            layout="vertical",
                            contents=[
                                FlexText(text="Date", size="sm", color="#666666"),
                                FlexText(text="March 15, 2024", size="sm", weight="bold")
                            ],
                            flex=1
                        ),
                        FlexBox(
                            layout="vertical", 
                            contents=[
                                FlexText(text="Time", size="sm", color="#666666"),
                                FlexText(text="9:00 AM", size="sm", weight="bold")
                            ],
                            flex=1
                        )
                    ],
                    margin="lg"
                ),
                FlexSeparator(margin="lg"),
                FlexText(
                    text="Join us for a day of innovation, networking, and learning about the latest in technology.",
                    wrap=True,
                    margin="lg",
                    size="sm"
                )
            ]
        ),
        footer=FlexBox(
            layout="vertical",
            contents=[
                FlexButton(
                    action=PostbackAction(
                        label="Register Now",
                        data="event=register&id=tech2024"
                    ),
                    style="primary"
                ),
                FlexButton(
                    action=URIAction(
                        label="More Info",
                        uri="https://example.com/event"
                    ),
                    style="link"
                )
            ],
            spacing="sm"
        )
    )
)

Quick Replies

from linebot.v3.messaging.models import QuickReply, QuickReplyItem

# Text message with quick reply options
text_with_quick_reply = TextMessage(
    text="How can I help you today?",
    quick_reply=QuickReply(
        items=[
            QuickReplyItem(
                action=MessageAction(label="📋 Menu", text="Show menu")
            ),
            QuickReplyItem(
                action=PostbackAction(label="🔍 Search", data="action=search")
            ),
            QuickReplyItem(
                action=LocationAction(label="📍 Location")
            ),
            QuickReplyItem(
                action=CameraAction(label="📷 Camera")
            )
        ]
    )
)

Image Map

from linebot.v3.messaging.models import (
    ImagemapMessage, ImagemapBaseSize, ImagemapArea,
    URIImagemapAction, MessageImagemapAction
)

imagemap = ImagemapMessage(
    base_url="https://example.com/imagemap",
    alt_text="Interactive Map",
    base_size=ImagemapBaseSize(width=1040, height=585),
    actions=[
        URIImagemapAction(
            uri="https://example.com/section1",
            area=ImagemapArea(x=0, y=0, width=520, height=292)
        ),
        MessageImagemapAction(
            text="Section 2 selected",
            area=ImagemapArea(x=520, y=0, width=520, height=292)
        ),
        URIImagemapAction(
            uri="https://example.com/section3", 
            area=ImagemapArea(x=0, y=292, width=1040, height=293)
        )
    ]
)

Install with Tessl CLI

npx tessl i tessl/pypi-line-bot-sdk

docs

authentication.md

index.md

messaging.md

rich-menus.md

rich-messages.md

user-management.md

webhooks.md

tile.json