telegram

package
v1.18.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package telegram – errors.go defines structured error types for the Telegram Bot API, enabling callers to classify and handle API failures.

Package telegram implements the Telegram channel for DevClaw using the Telegram Bot API directly via HTTP — no external dependencies.

Features:

  • Long polling for updates (getUpdates)
  • Send/receive text, images, audio, video, documents, voice notes
  • Typing indicators (sendChatAction)
  • Reactions (setMessageReaction, Bot API 7.0+)
  • Media download via getFile
  • HTML formatting for rich messages
  • Group and DM support

Index

Constants

View Source
const (
	ButtonStyleDefault = ""
	ButtonStylePrimary = "primary" // blue
	ButtonStyleSuccess = "success" // green
	ButtonStyleDanger  = "danger"  // red
)

ButtonStyle is the visual style of an inline keyboard button. Telegram Bot API 9.4+ supports native styles; older clients may fall back to emoji prefixes.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// InstanceID identifies this instance ("" for default, e.g. "alerts" for named).
	// Set automatically from the config key in telegram_instances.
	InstanceID string `yaml:"instance_id,omitempty"`

	// Token is the Telegram Bot API token (from @BotFather).
	Token string `yaml:"token"`

	// AllowedChats restricts which chat IDs the bot responds to.
	// Empty means respond to all chats.
	AllowedChats []int64 `yaml:"allowed_chats"`

	// RespondToGroups enables responding in group chats.
	RespondToGroups bool `yaml:"respond_to_groups"`

	// RespondToDMs enables responding in direct messages.
	RespondToDMs bool `yaml:"respond_to_dms"`

	// SendTyping sends "typing..." indicators while processing.
	SendTyping bool `yaml:"send_typing"`

	// ParseMode sets the default parse mode for outgoing messages ("HTML" or "Markdown").
	ParseMode string `yaml:"parse_mode"`

	// ReactionNotifications controls when user reactions are surfaced as system events.
	// "off" (default): ignore reactions
	// "own": only reactions to bot messages
	// "all": all reactions in allowed chats
	ReactionNotifications string `yaml:"reaction_notifications"`
}

Config holds Telegram channel configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type InlineButton

type InlineButton struct {
	Text         string `json:"text"`
	CallbackData string `json:"callback_data,omitempty"`
	URL          string `json:"url,omitempty"`
	Style        string `json:"style,omitempty"` // "primary", "success", "danger", or ""
}

InlineButton represents an inline keyboard button. Use via OutgoingMessage.Metadata["telegram_buttons"] as []InlineButton. Each button needs either CallbackData or URL; Style is optional.

type Telegram

type Telegram struct {
	// contains filtered or unexported fields
}

Telegram implements channels.Channel, channels.MediaChannel, channels.PresenceChannel, and channels.ReactionChannel.

func New

func New(cfg Config, logger *slog.Logger) *Telegram

New creates a new Telegram channel instance.

func (*Telegram) BaseType added in v1.16.0

func (t *Telegram) BaseType() string

BaseType returns "telegram".

func (*Telegram) BotID added in v1.16.5

func (t *Telegram) BotID() int64

BotID returns the bot's numeric user ID (available after Connect).

func (*Telegram) BotUsername added in v1.16.5

func (t *Telegram) BotUsername() string

BotUsername returns the bot's @username (available after Connect).

func (*Telegram) Connect

func (t *Telegram) Connect(ctx context.Context) error

Connect starts the long-polling loop for receiving updates.

func (*Telegram) Disconnect

func (t *Telegram) Disconnect() error

Disconnect stops the polling loop and closes the messages channel.

func (*Telegram) DownloadMedia

func (t *Telegram) DownloadMedia(ctx context.Context, msg *channels.IncomingMessage) ([]byte, string, error)

DownloadMedia downloads media from an incoming message.

func (*Telegram) Health

func (t *Telegram) Health() channels.HealthStatus

Health returns the channel health status.

func (*Telegram) InstanceID added in v1.16.0

func (t *Telegram) InstanceID() string

InstanceID returns the instance identifier ("" for default).

func (*Telegram) IsBotMessage added in v1.16.0

func (t *Telegram) IsBotMessage(chatID, messageID string) bool

IsBotMessage returns true if the given message was sent by the bot. Implements channels.SentMessageTracker.

func (*Telegram) IsConnected

func (t *Telegram) IsConnected() bool

IsConnected returns true if the bot is connected.

func (*Telegram) MarkRead

func (t *Telegram) MarkRead(ctx context.Context, chatID string, messageIDs []string) error

MarkRead is a no-op for Telegram (bots can't mark messages as read).

func (*Telegram) Name

func (t *Telegram) Name() string

Name returns the channel name. For the default instance this is "telegram"; for named instances it returns "telegram:<instance_id>".

func (*Telegram) Receive

func (t *Telegram) Receive() <-chan *channels.IncomingMessage

Receive returns the incoming messages channel.

func (*Telegram) Send

func (t *Telegram) Send(ctx context.Context, to string, message *channels.OutgoingMessage) error

Send sends a text message to the specified chat. If message.EditMessageID is set, edits the existing message instead.

func (*Telegram) SendMedia

func (t *Telegram) SendMedia(ctx context.Context, to string, media *channels.MediaMessage) error

SendMedia sends a media message to the specified chat.

func (*Telegram) SendPresence

func (t *Telegram) SendPresence(ctx context.Context, available bool) error

SendPresence is a no-op for Telegram.

func (*Telegram) SendReaction

func (t *Telegram) SendReaction(ctx context.Context, chatID, messageID, emoji string) error

SendReaction sends a reaction emoji to a specific message (Bot API 7.0+).

func (*Telegram) SendTyping

func (t *Telegram) SendTyping(ctx context.Context, to string) error

SendTyping sends a "typing..." chat action. Implements a circuit breaker: after 10 consecutive auth errors, typing indicators are silently suspended to prevent hammering the API with an invalid token and triggering bot exclusion.

func (*Telegram) SetReactionNotifications added in v1.16.5

func (t *Telegram) SetReactionNotifications(v string)

SetReactionNotifications updates the reaction notifications setting on the live instance.

func (*Telegram) SetRespondToDMs added in v1.16.5

func (t *Telegram) SetRespondToDMs(v bool)

SetRespondToDMs updates the respond-to-DMs setting on the live instance.

func (*Telegram) SetRespondToGroups added in v1.16.5

func (t *Telegram) SetRespondToGroups(v bool)

SetRespondToGroups updates the respond-to-groups setting on the live instance.

func (*Telegram) SetSendTyping added in v1.16.5

func (t *Telegram) SetSendTyping(v bool)

SetSendTyping updates the typing indicator setting on the live instance.

type TelegramAPIError added in v1.16.0

type TelegramAPIError struct {
	// Method is the Bot API method that failed (e.g. "sendMessage").
	Method string

	// HTTPStatus is the HTTP response status code.
	HTTPStatus int

	// ErrorCode is the Telegram-specific error code from the JSON response.
	ErrorCode int

	// Description is the human-readable error description from Telegram.
	Description string
}

TelegramAPIError represents a structured error from the Telegram Bot API.

func (*TelegramAPIError) Error added in v1.16.0

func (e *TelegramAPIError) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL