Channels API¶
The channels module provides functionality for formatting responses across different communication channels like SMS, web chat, and voice interfaces. Each channel has specific constraints and capabilities that are handled automatically.
Overview¶
lex-helper supports multiple channels with automatic formatting based on the channel's capabilities:
- SMS: Text-only with length constraints and link handling
- Web/Lex: Rich formatting with buttons, cards, and media
- Voice: Speech-optimized responses with SSML support
Channel Formatting¶
The main entry point for channel-aware formatting.
Base Channel¶
Abstract base class for all channel implementations.
Base channel interface for message formatting.
Classes¶
    
              Bases: ABC
Abstract base class for channel-specific message formatting.
Functions¶
abstractmethod
  
¶
    Format a single message for the specific channel.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| message | LexMessages | The Lex message to format | required | 
Returns:
| Type | Description | 
|---|---|
| LexBaseResponse | The formatted message string | 
abstractmethod
  
¶
    Format a list of messages for the specific channel.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| messages | list[LexMessages] | List of Lex messages to format | required | 
Returns:
| Type | Description | 
|---|---|
| list[LexBaseResponse] | List of formatted message strings | 
Source code in lex_helper/channels/base.py
              
            
    Format a plain text message.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| message | LexPlainText | The plain text message to format | required | 
Returns:
| Type | Description | 
|---|---|
| LexBaseResponse | The formatted plain text | 
Source code in lex_helper/channels/base.py
              
            
    Format an image response card.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| card | LexImageResponseCard | The image card to format | required | 
Returns:
| Type | Description | 
|---|---|
| LexBaseResponse | The formatted card text | 
Source code in lex_helper/channels/base.py
              
    Format a custom payload message.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| payload | LexCustomPayload | The custom payload to format | required | 
Returns:
| Type | Description | 
|---|---|
| LexBaseResponse | The formatted payload text, or None if payload cannot be formatted | 
Source code in lex_helper/channels/base.py
              Lex Channel¶
Default Lex channel with full rich formatting support.
Lex-specific channel implementation.
Classes¶
    
              Bases: Channel
Channel implementation for Lex-specific message formatting.
Functions¶
    Format a single Lex message.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| message | LexMessages | The Lex message to format | required | 
Returns:
| Type | Description | 
|---|---|
| LexBaseResponse | The formatted message string | 
Source code in lex_helper/channels/lex.py
              
    Format a list of Lex messages.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| messages | list[LexMessages] | List of Lex messages to format | required | 
Returns:
| Type | Description | 
|---|---|
| list[LexBaseResponse] | List of formatted message strings | 
Source code in lex_helper/channels/lex.py
              
            
    Format a Lex plain text message.
Overrides the base implementation to handle Lex-specific formatting.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| message | LexPlainText | The plain text message to format | required | 
Returns:
| Type | Description | 
|---|---|
| LexBaseResponse | The formatted plain text | 
Source code in lex_helper/channels/lex.py
              
    Format a Lex image response card.
Overrides the base implementation to handle Lex-specific formatting.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| card | LexImageResponseCard | The image card to format | required | 
Returns:
| Type | Description | 
|---|---|
| LexBaseResponse | The formatted card text | 
Source code in lex_helper/channels/lex.py
              
    Format a Lex custom payload message.
Overrides the base implementation to handle Lex-specific formatting.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| payload | LexCustomPayload | The custom payload to format | required | 
Returns:
| Type | Description | 
|---|---|
| LexBaseResponse | The formatted payload text | 
Source code in lex_helper/channels/lex.py
              SMS Channel¶
SMS-specific formatting with text constraints and optimizations.
SMS-specific channel implementation.
Classes¶
    
              Bases: Channel
Channel implementation for SMS-specific message formatting.
Functions¶
    Format a single message for SMS.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| message | LexMessages | The Lex message to format | required | 
Returns:
| Type | Description | 
|---|---|
| LexBaseResponse | The formatted message string | 
Source code in lex_helper/channels/sms.py
              
    Format a list of messages for SMS.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| messages | list[LexMessages] | List of Lex messages to format | required | 
Returns:
| Type | Description | 
|---|---|
| list[LexBaseResponse] | List of formatted message strings | 
Source code in lex_helper/channels/sms.py
              
    Format a plain text message for SMS.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| message | LexPlainText | The plain text message to format | required | 
Returns:
| Type | Description | 
|---|---|
| LexBaseResponse | The formatted plain text | 
Source code in lex_helper/channels/sms.py
              
    Format an image response card for SMS.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| card | LexImageResponseCard | The image card to format | required | 
Returns:
| Type | Description | 
|---|---|
| LexBaseResponse | The formatted card text | 
Source code in lex_helper/channels/sms.py
              
    Format a custom payload message for SMS.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| payload | LexCustomPayload | The custom payload to format | required | 
Returns:
| Type | Description | 
|---|---|
| LexBaseResponse | The formatted payload text | 
Source code in lex_helper/channels/sms.py
              Usage Examples¶
Basic Channel Formatting¶
from lex_helper.channels.channel_formatting import format_for_channel
from lex_helper.core.types import LexResponse
# Create a response
response = LexResponse(...)
# Format for SMS (will strip rich formatting)
sms_response = format_for_channel(response, "sms")
# Format for web (preserves rich formatting)
web_response = format_for_channel(response, "lex")
Channel-Specific Responses¶
from lex_helper.channels.sms import SmsChannel
from lex_helper.channels.lex import LexChannel
# SMS channel automatically handles text length limits
sms_channel = SmsChannel()
formatted_sms = sms_channel.format_response(response)
# Lex channel supports rich formatting
lex_channel = LexChannel()
formatted_lex = lex_channel.format_response(response)
Channel Detection¶
The library automatically detects the channel from the Lex request and applies appropriate formatting. You can also explicitly specify the channel when needed.
Next: Explore the Formatters API for message formatting utilities.