Sexy.Utils.Bot (Sexy v0.9.13)

Copy Markdown View Source

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_msg(obj)

@spec extract_msg(map()) :: map()

Extract the message map from a Telegram update.

Works with both direct messages and callback queries:

  • %{message: msg} → returns msg
  • %{callback_query: %{message: msg}} → returns msg
  • anything else → returns the input as-is

extract_user_obj(obj)

@spec extract_user_obj(map()) :: map()

Extract the user (from) map from a Telegram update.

Checks callback_query.from, then message.from, then obj.from.

get_command_name(string)

@spec get_command_name(String.t()) :: String.t()

Extract just the command name from a string (without / prefix and query).

Example

iex> Sexy.Utils.Bot.get_command_name("/start some_param")
"start"

get_message_media(msg, type)

@spec get_message_media(map(), String.t()) :: String.t() | nil

Extract the file_id from a message by media type.

Supports "video" and "photo". For photos, returns the highest-resolution version.

get_message_type(u)

@spec get_message_type(map()) :: String.t()

Detect the content type of a message.

Returns one of: "video_note", "animation", "document", "sticker", "contact", "photo", "audio", "video", "voice", "text", "unknown".

get_photo_id(photo)

@spec get_photo_id([map()]) :: String.t()

Get the file_id of the highest-resolution photo from a Telegram photo array.

paginate(list, page_index, size)

@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_comand_and_query(string)

@spec parse_comand_and_query(String.t()) :: {String.t(), map()}

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(text)

@spec wrap_text(String.t()) :: String.t()

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.

wrap_text(text, tag, lines \\ false)

@spec wrap_text(String.t(), String.t(), boolean()) :: String.t()