Packages

Elixir library for building Telegram bots with macro-based API

Current section

Files

Jump to
telegram_ex lib types message.ex
Raw

lib/types/message.ex

defmodule TelegramEx.Types.Message do
@moduledoc """
Struct representing a Telegram Message object.
Contains incoming message fields used by handlers.
## Fields
- `:message_id` - Unique message identifier
- `:from` - Sender information (map with string keys)
- `:chat` - Chat information (map with string keys)
- `:date` - Message date as Unix timestamp
- `:text` - Message text content (nil if not a text message)
- `:photo` - List of photo sizes (nil if no photo)
- `:document` - Document attachment (nil if no document)
- `:sticker` - Sticker (nil if no sticker)
- `:video` - Video (nil if no video)
- `:voice` - Voice message (nil if no voice)
- `:caption` - Caption for media (nil if no caption)
- `:message_thread_id` - Thread ID for forum chats (nil if not in a thread)
"""
alias TelegramEx.Types.Message
@typedoc """
Message struct type.
Contains all fields from a Telegram message update.
"""
@type t :: %__MODULE__{
message_id: integer(),
from: map(),
chat: map(),
date: integer(),
text: String.t() | nil,
photo: list(map()) | nil,
document: map() | nil,
sticker: map() | nil,
video: map() | nil,
voice: map() | nil,
audio: map() | nil,
caption: String.t() | nil,
message_thread_id: integer() | nil,
reply_to_message: t() | nil
}
defstruct [
:message_id,
:from,
:chat,
:date,
:text,
:photo,
:document,
:sticker,
:video,
:audio,
:voice,
:caption,
:message_thread_id,
:reply_to_message
]
@doc """
Converts a raw Telegram API message map to a Message struct.
"""
@spec from_map(nil) :: nil
@spec from_map(map()) :: t()
def from_map(nil), do: nil
def from_map(map) do
%__MODULE__{
message_id: map["message_id"],
message_thread_id: map["message_thread_id"],
from: map["from"],
chat: map["chat"],
date: map["date"],
text: Map.get(map, "text", ""),
photo: map["photo"],
document: map["document"],
sticker: map["sticker"],
video: map["video"],
voice: map["voice"],
caption: map["caption"],
reply_to_message: Message.from_map(map["reply_to_message"])
}
end
end