Helpers for working with Telegram bot updates: command parsing, user/message extraction, media handling, text formatting, and pagination.
Command parsing
iex> Sexy.Utils.Bot.parse_comand_and_query("/buy id=42-page=1")
{"buy", %{id: 42, page: 1}}
iex> Sexy.Utils.Bot.get_command_name("/start deep_link")
"start"Update extraction
# Works with both message and callback_query updates
msg = Sexy.Utils.Bot.extract_msg(update)
user = Sexy.Utils.Bot.extract_user_obj(update)Media detection
type = Sexy.Utils.Bot.get_message_type(update)
# => "text", "photo", "video", "animation", "document", etc.Pagination
page_items = Sexy.Utils.Bot.paginate(all_items, 2, 10)
# => items 11-20
Summary
Functions
Extract the message map from a Telegram update.
Extract the user (from) map from a Telegram update.
Extract just the command name from a string (without / prefix and query).
Extract the file_id from a message by media type.
Detect the content type of a message.
Get the file_id of the highest-resolution photo from a Telegram photo array.
Paginate a list by page index (1-based) and page size.
Parse a command string into {command_name, query_map}.
Wrap text in decorative <code> borders for Telegram HTML messages.
Functions
Extract the message map from a Telegram update.
Works with both direct messages and callback queries:
%{message: msg}→ returnsmsg%{callback_query: %{message: msg}}→ returnsmsg- anything else → returns the input as-is
Extract the user (from) map from a Telegram update.
Checks callback_query.from, then message.from, then obj.from.
Extract just the command name from a string (without / prefix and query).
Example
iex> Sexy.Utils.Bot.get_command_name("/start some_param")
"start"
Extract the file_id from a message by media type.
Supports "video" and "photo". For photos, returns the highest-resolution version.
Detect the content type of a message.
Returns one of: "video_note", "animation", "document", "sticker",
"contact", "photo", "audio", "video", "voice", "text", "unknown".
Get the file_id of the highest-resolution photo from a Telegram photo array.
@spec paginate(list(), pos_integer(), pos_integer()) :: list()
Paginate a list by page index (1-based) and page size.
Example
iex> Sexy.Utils.Bot.paginate(Enum.to_list(1..50), 2, 10)
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Parse a command string into {command_name, query_map}.
Example
iex> Sexy.Utils.Bot.parse_comand_and_query("/buy id=42-amount=100")
{"buy", %{id: 42, amount: 100}}
Wrap text in decorative <code> borders for Telegram HTML messages.
With one argument, wraps in lines. With a tag, wraps in the specified HTML tag.