telegram

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatMarkdown

func FormatMarkdown(text string) string

FormatMarkdown converts standard Markdown to Telegram Markdown v1.

Telegram v1 supports: *bold*, _italic_, `code`, ```pre```, [text](url). Standard Markdown features not in v1 (e.g. ~~strike~~) are stripped. Content inside code blocks (```) is preserved without transformation.

Types

type ApprovalProvider

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

ApprovalProvider implements approval.Provider for Telegram using InlineKeyboard buttons.

func NewApprovalProvider

func NewApprovalProvider(bot BotAPI, timeout time.Duration) *ApprovalProvider

NewApprovalProvider creates a Telegram approval provider.

func (*ApprovalProvider) CanHandle

func (p *ApprovalProvider) CanHandle(sessionKey string) bool

CanHandle returns true for session keys starting with "telegram:".

func (*ApprovalProvider) HandleCallback

func (p *ApprovalProvider) HandleCallback(query *tgbotapi.CallbackQuery)

HandleCallback processes an InlineKeyboard callback query for approval.

func (*ApprovalProvider) RequestApproval

RequestApproval sends an InlineKeyboard message to the chat and waits for a callback.

type BotAPI

type BotAPI interface {
	GetUpdatesChan(config tgbotapi.UpdateConfig) tgbotapi.UpdatesChannel
	Send(c tgbotapi.Chattable) (tgbotapi.Message, error)
	Request(c tgbotapi.Chattable) (*tgbotapi.APIResponse, error)
	GetFile(config tgbotapi.FileConfig) (tgbotapi.File, error)
	StopReceivingUpdates()
	GetSelf() tgbotapi.User
}

BotAPI defines the interface for Telegram Bot API operations.

type Channel

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

Channel implements Telegram bot

func New

func New(cfg Config) (*Channel, error)

New creates a new Telegram channel

func (*Channel) DownloadFile

func (c *Channel) DownloadFile(fileID string) ([]byte, error)

DownloadFile downloads a file by file ID

func (*Channel) GetApprovalProvider

func (c *Channel) GetApprovalProvider() *ApprovalProvider

GetApprovalProvider returns the channel's approval provider for composite registration.

func (*Channel) Send

func (c *Channel) Send(chatID int64, msg *OutgoingMessage) error

Send sends a message. When ParseMode is not set, standard Markdown is auto-converted to Telegram v1 and sent with ParseMode "Markdown". If the API rejects the formatted text, the original text is re-sent as plain text.

func (*Channel) SetHandler

func (c *Channel) SetHandler(handler MessageHandler)

SetHandler sets the message handler

func (*Channel) Start

func (c *Channel) Start(ctx context.Context) error

Start starts listening for updates

func (*Channel) StartTyping

func (c *Channel) StartTyping(ctx context.Context, chatID int64) func()

StartTyping sends a typing indicator to the chat and refreshes it periodically until the returned stop function is called or ctx is cancelled. The returned stop function is safe to call multiple times.

func (*Channel) Stop

func (c *Channel) Stop()

Stop stops the channel

type Config

type Config struct {
	BotToken           string
	Allowlist          []int64      // allowed user/chat IDs (empty = all)
	ApprovalTimeoutSec int          // 0 = default 30s
	APIEndpoint        string       // optional, for testing
	HTTPClient         *http.Client // optional, for testing
	Bot                BotAPI       // optional, for testing
}

Config holds Telegram channel configuration

type IncomingMessage

type IncomingMessage struct {
	MessageID   int
	ChatID      int64
	UserID      int64
	Username    string
	Text        string
	ReplyToID   int
	HasMedia    bool
	MediaType   string
	MediaFileID string
}

IncomingMessage represents a message from Telegram

type MessageHandler

type MessageHandler func(ctx context.Context, msg *IncomingMessage) (*OutgoingMessage, error)

MessageHandler handles incoming messages

type OutgoingMessage

type OutgoingMessage struct {
	Text           string
	ReplyToID      int
	ParseMode      string // "Markdown", "HTML"
	DisablePreview bool
}

OutgoingMessage represents a message to send

type TelegramBot

type TelegramBot struct {
	*tgbotapi.BotAPI
}

TelegramBot is an adapter for *tgbotapi.BotAPI.

func NewTelegramBot

func NewTelegramBot(b *tgbotapi.BotAPI) *TelegramBot

NewTelegramBot creates a new TelegramBot adapter.

func (*TelegramBot) GetSelf

func (b *TelegramBot) GetSelf() tgbotapi.User

GetSelf returns the bot user.

Jump to

Keyboard shortcuts

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