Formatters API¶
The formatters module provides utilities for formatting messages, handling text processing, creating interactive elements like buttons, and managing URLs. These utilities help create well-formatted responses across different channels.
Text Formatting¶
Utilities for processing and formatting text content.
Text formatting utilities.
Functions¶
    Remove HTML tags from text.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| text | str | Text containing HTML tags | required | 
Returns:
| Type | Description | 
|---|---|
| str | Clean text with HTML tags removed | 
    Replace special characters in text.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| text | str | Text containing special characters | required | 
| replacement | str | None | Character to use as replacement (defaults to empty string) | None | 
Returns:
| Type | Description | 
|---|---|
| str | Text with special characters replaced | 
Source code in lex_helper/formatters/text.py
              
    Substitute keys in text with their values.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| text | str | Text containing keys to substitute | required | 
| substitutions | dict[str, str] | Dictionary of key-value pairs for substitution | required | 
Returns:
| Type | Description | 
|---|---|
| str | Text with keys replaced by their values | 
Example
text = "Hello {name}!" subs = {"name": "World"} substitute_keys_in_text(text, subs) 'Hello World!'
Source code in lex_helper/formatters/text.py
              
    Truncate text to specified length.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| text | str | Text to truncate | required | 
| max_length | int | Maximum length of resulting text (including suffix) | required | 
| suffix | str | String to append to truncated text | '...' | 
Returns:
| Type | Description | 
|---|---|
| str | Truncated text with suffix if needed | 
Source code in lex_helper/formatters/text.py
              
    Normalize whitespace in text.
Replaces multiple spaces, tabs, and newlines with single spaces and strips leading/trailing whitespace.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| text | str | Text to normalize | required | 
Returns:
| Type | Description | 
|---|---|
| str | Text with normalized whitespace | 
Source code in lex_helper/formatters/text.py
              
    Split text into sentences.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| text | str | Text to split | required | 
Returns:
| Type | Description | 
|---|---|
| list[str] | List of sentences | 
Source code in lex_helper/formatters/text.py
              Button Formatting¶
Create interactive buttons and quick replies for supported channels.
Single Button¶
Multiple Buttons¶
Button formatting utilities.
Classes¶
dataclass
  
¶
    
Functions¶
create_button(text: str, value: str | None = None) -> Button
Create a button with text and optional value.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| text | str | The button text to display | required | 
| value | str | None | The button value (defaults to text if not provided) | None | 
Returns:
| Type | Description | 
|---|---|
| Button | A Button instance | 
Source code in lex_helper/formatters/buttons.py
              
create_buttons(texts: list[str], values: list[str] | None = None) -> list[Button]
Create multiple buttons from lists of texts and optional values.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| texts | list[str] | List of button texts | required | 
| values | list[str] | None | Optional list of button values (must match length of texts if provided) | None | 
Returns:
| Type | Description | 
|---|---|
| list[Button] | List of Button instances | 
Raises:
| Type | Description | 
|---|---|
| ValueError | If values list is provided but length doesn't match texts | 
Source code in lex_helper/formatters/buttons.py
              
format_buttons_for_display(buttons: list[Button], style: str = 'default') -> str
Format buttons for display in a consistent way.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| buttons | list[Button] | List of buttons to format | required | 
| style | str | Display style ("default", "compact", or "verbose") | 'default' | 
Returns:
| Type | Description | 
|---|---|
| str | Formatted string representation of buttons | 
Source code in lex_helper/formatters/buttons.py
              
buttons_to_dicts(buttons: list[Button]) -> list[dict[str, str]]
Convert a list of buttons to list of dictionaries.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| buttons | list[Button] | List of Button instances | required | 
Returns:
| Type | Description | 
|---|---|
| list[dict[str, str]] | List of button dictionaries | 
Source code in lex_helper/formatters/buttons.py
              
            Button Formatting Utilities¶
Classes¶
Functions¶
    This function formats Button objects.
Parameters: buttons (List[Button]): A list of Button objects to be formatted.
Returns: List[Button]: A list of formatted Button objects.
Source code in lex_helper/formatters/format_buttons.py
              URL Handling¶
Utilities for processing and formatting URLs.
URL Formatting¶
URL formatting utilities.
Functions¶
    Check if a URL is valid.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| url | str | URL to validate | required | 
Returns:
| Type | Description | 
|---|---|
| bool | True if URL is valid, False otherwise | 
Source code in lex_helper/formatters/url.py
              
            
    Normalize a URL by adding scheme if missing.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| url | str | URL to normalize | required | 
| default_scheme | str | Scheme to add if missing (default: "https") | 'https' | 
Returns:
| Type | Description | 
|---|---|
| str | Normalized URL | 
Source code in lex_helper/formatters/url.py
              
    Extract domain from URL.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| url | str | URL to extract domain from | required | 
Returns:
| Type | Description | 
|---|---|
| str | None | Domain name or None if URL is invalid | 
Source code in lex_helper/formatters/url.py
              
            
build_url(scheme: str, netloc: str, path: str = '', params: str = '', query: str = '', fragment: str = '') -> str
Build a URL from components.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| scheme | str | URL scheme (e.g., "https") | required | 
| netloc | str | Network location/hostname | required | 
| path | str | URL path | '' | 
| params | str | URL parameters | '' | 
| query | str | Query string | '' | 
| fragment | str | Fragment identifier | '' | 
Returns:
| Type | Description | 
|---|---|
| str | Complete URL | 
Source code in lex_helper/formatters/url.py
              
    Clean a URL by removing unnecessary components.
Removes fragments, normalizes scheme, ensures single slashes.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
| url | str | URL to clean | required | 
Returns:
| Type | Description | 
|---|---|
| str | Cleaned URL | 
Source code in lex_helper/formatters/url.py
              URL Validation¶
URL Conversion¶
Text Processing Utilities¶
Additional text processing and cleaning utilities.
HTML Tag Removal¶
Functions¶
    This function removes <p></p> HTML tags from the text and trims
from the end.
Parameters:
text (str): The text from which <p></p> HTML tags should be removed.
Returns:
str: The text without the <p></p> HTML tags and trailing
.
Source code in lex_helper/formatters/remove_html_tags.py
              Special Character Handling¶
Functions¶
    This function replaces &," from the text.
Parameters: text (str): The text from which special characters should be removed.
Returns: str: The text without the special characters.
Source code in lex_helper/formatters/replace_special_characters.py
              Template Substitution¶
Usage Examples¶
Text Formatting¶
from lex_helper.formatters.text import remove_html_tags, substitute_keys_in_text
# Clean HTML from text
clean_text = remove_html_tags("<p>Hello <b>world</b>!</p>")
# Result: "Hello world!"
# Substitute variables in text
template = "Hello {name}, your order #{order_id} is ready!"
substitutions = {"name": "Alice", "order_id": "12345"}
message = substitute_keys_in_text(template, substitutions)
# Result: "Hello Alice, your order #12345 is ready!"
Button Creation¶
from lex_helper.formatters.buttons import create_buttons
from lex_helper.formatters.button import create_button
# Create a single button
button = create_button("Click me", "button_value")
# Create multiple buttons
buttons = create_buttons([
    ("Option 1", "value1"),
    ("Option 2", "value2"),
    ("Option 3", "value3")
])
URL Processing¶
from lex_helper.formatters.url import format_url
from lex_helper.formatters.is_valid_url import is_valid_url
# Validate URL
if is_valid_url("https://example.com"):
    formatted = format_url("https://example.com", "Visit Example")
Channel Compatibility¶
Different formatters work with different channels:
- Text formatters: Work with all channels
- Button formatters: Work with Lex and web channels, fallback to text for SMS
- URL formatters: Automatically adapt based on channel capabilities
Next: Explore the Exceptions API for error handling utilities.