Skip to content

EN Context and Messaging

Andrewkydev edited this page Aug 26, 2026 · 4 revisions

Context and Messaging

Русский: RU-Context-and-Messaging

A handler receives a normalized event:

func handler(ctx *botctx.Context) error {
	ctx.Log().Info("message received",
		"platform", ctx.PlatformName(),
		"user_id", ctx.Event.UserID,
		"chat_id", ctx.Event.ChatID,
	)
	return ctx.SendText("Received: " + ctx.Text())
}

Useful fields: ID, Platform, ChatID, UserID, Username, Locale, Type, Text, Command, Payload, Attachments, MessageRef, Data, Raw.

With localization middleware attached, ctx.Locale() returns the selected request locale and ctx.T(key, args) renders a catalog message. See Localization.

Sending messages

ctx.SendText("Text")
ctx.Send("Text with buttons", &kb)
ctx.SendWithAttachments("File", nil, file)
ctx.SendMessage(platform.OutgoingMessage{
	Text: "Status",
	Embeds: []platform.Embed{{Title: "Done"}},
})

If an event contains a MessageRef, a normal send automatically becomes a reply.

ref, err := ctx.SendTextWithRef("Updating...")
if err != nil {
	return err
}
return ctx.EditByRef(ref, "Done", nil)

Edit, EditText, and EditByRef fall back to a normal send only when editing is explicitly unavailable. Operational adapter errors such as rate limits, authentication, or network failures are returned without sending a duplicate message. Adapters report the unavailable case with platform.ErrEditUnavailable; use ctx.CanEdit() to check general support.

Private messages

return ctx.SendPrivate(platform.OutgoingMessage{Text: "For your eyes only"})

SendPrivate never falls back to the current chat. When the capability is unavailable, it returns platform.ErrPrivateMessagingUnsupported.

Callbacks and common input

ctx.AnswerCallback(text) dismisses the callback loading state. ctx.Input() returns message text; for callbacks it returns the action, route, or raw payload. This is useful in scenes.

For SDK-specific functionality, use telegram.From(ctx), vk.From(ctx), or discord.From(ctx).

For ready-made callback navigation, see Pagination.

Clone this wiki locally