Nadia.Context (nadia v1.2.0)

View Source

Convenience helpers for working with incoming Telegram updates.

Nadia.Context does not replace the raw Bot API wrappers. It extracts the common message, chat, and user values from an update and provides small reply helpers that delegate to existing Nadia functions.

Summary

Functions

Answers the context's callback query.

Returns the effective chat id for an update or context.

Returns the chat most directly associated with an update or context.

Returns the message most directly associated with an update or context.

Returns the user most directly associated with an update or context.

Builds a context from a parsed Telegram update.

Sends a text message to the context's effective chat.

Types

t()

@type t() :: %Nadia.Context{
  callback_query: Nadia.Model.CallbackQuery.t() | nil,
  chat: Nadia.Model.Chat.t() | nil,
  chat_id: integer() | binary() | nil,
  client: Nadia.Client.t() | nil,
  from: Nadia.Model.User.t() | nil,
  inline_query: Nadia.Model.InlineQuery.t() | nil,
  message: Nadia.Model.Message.t() | nil,
  message_id: integer() | nil,
  update: Nadia.Model.Update.t()
}

Functions

answer_callback(context, options \\ [])

@spec answer_callback(
  t(),
  keyword()
) :: :ok | {:error, Nadia.Model.Error.t()}

Answers the context's callback query.

Returns the same result shape as Nadia.answer_callback_query/2.

chat_id(update)

@spec chat_id(t() | Nadia.Model.Update.t()) :: integer() | binary() | nil

Returns the effective chat id for an update or context.

effective_chat(update)

@spec effective_chat(t() | Nadia.Model.Update.t()) :: Nadia.Model.Chat.t() | nil

Returns the chat most directly associated with an update or context.

effective_message(update)

@spec effective_message(t() | Nadia.Model.Update.t()) :: Nadia.Model.Message.t() | nil

Returns the message most directly associated with an update or context.

Callback query messages are considered effective messages so callback-driven handlers can reply to the chat that produced the callback.

effective_user(update)

@spec effective_user(t() | Nadia.Model.Update.t()) :: Nadia.Model.User.t() | nil

Returns the user most directly associated with an update or context.

new(update, client_or_opts \\ [])

@spec new(Nadia.Model.Update.t(), Nadia.Client.t() | keyword() | map() | nil) :: t()

Builds a context from a parsed Telegram update.

The second argument may be a %Nadia.Client{} or options containing :client. When no client is supplied, reply helpers use Nadia's existing application-config based wrappers.

reply(context, text, options \\ [])

@spec reply(t(), binary(), keyword()) ::
  {:ok, Nadia.Model.Message.t()} | {:error, Nadia.Model.Error.t()}

Sends a text message to the context's effective chat.

Returns the same result shape as Nadia.send_message/3.