Documentation
¶
Overview ¶
Package galigo provides a unified Go library for Telegram Bot API.
galigo combines receiving updates (webhook/long polling) and sending messages into a single, modern library with built-in resilience patterns.
Quick Start ¶
bot, err := galigo.New(token,
galigo.WithPolling(30, 100),
galigo.WithRetries(5),
)
if err != nil {
log.Fatal(err)
}
defer bot.Close()
for update := range bot.Updates() {
bot.SendMessage(ctx, update.Message.Chat.ID, "Echo: "+update.Message.Text)
}
Separate Receiver/Sender ¶
For microservices that only need one capability:
// Only receive updates import "github.com/prilive-com/galigo/receiver" recv, _ := receiver.New(token) // Only send messages import "github.com/prilive-com/galigo/sender" send, _ := sender.New(token)
Shared Types ¶
All Telegram types are in the tg subpackage:
import "github.com/prilive-com/galigo/tg" var msg tg.Message var user tg.User
Features ¶
- Dual mode: webhook or long polling
- Circuit breaker with sony/gobreaker
- Per-chat and global rate limiting
- Retry with exponential backoff and crypto jitter
- TLS 1.2+ enforcement
- Token auto-redaction in logs and errors
- Structured logging with slog
- OpenTelemetry-ready
- Go 1.22+ features: integer range loops, improved generics
Index ¶
- type Bot
- func (b *Bot) Acknowledge(ctx context.Context, cb *tg.CallbackQuery) error
- func (b *Bot) Answer(ctx context.Context, cb *tg.CallbackQuery, opts ...sender.AnswerOption) error
- func (b *Bot) Close() error
- func (b *Bot) Copy(ctx context.Context, e tg.Editable, toChatID tg.ChatID, ...) (*tg.MessageID, error)
- func (b *Bot) Delete(ctx context.Context, e tg.Editable) error
- func (b *Bot) Edit(ctx context.Context, e tg.Editable, text string, opts ...sender.EditOption) (*tg.Message, error)
- func (b *Bot) Forward(ctx context.Context, e tg.Editable, toChatID tg.ChatID, ...) (*tg.Message, error)
- func (b *Bot) IsHealthy() bool
- func (b *Bot) SendMessage(ctx context.Context, chatID tg.ChatID, text string, opts ...SendOption) (*tg.Message, error)
- func (b *Bot) SendPhoto(ctx context.Context, chatID tg.ChatID, photo string, opts ...PhotoOption) (*tg.Message, error)
- func (b *Bot) SendPhotoFile(ctx context.Context, chatID tg.ChatID, photo sender.InputFile, ...) (*tg.Message, error)
- func (b *Bot) Sender() *sender.Client
- func (b *Bot) Start(ctx context.Context) error
- func (b *Bot) Stop()
- func (b *Bot) Updates() <-chan tg.Update
- func (b *Bot) WebhookHandler() *receiver.WebhookHandler
- type Option
- func WithAllowedUpdates(types ...string) Option
- func WithDeleteWebhook(delete bool) Option
- func WithLogger(logger *slog.Logger) Option
- func WithPolling(timeout, limit int) Option
- func WithPollingMaxErrors(max int) Option
- func WithRateLimit(globalRPS float64, burst int) Option
- func WithRetries(max int) Option
- func WithUpdateBufferSize(size int) Option
- func WithWebhook(port int, secret string) Option
- type PhotoOption
- type SendOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
Bot is the unified Telegram bot client combining receiver and sender.
func (*Bot) Acknowledge ¶
Acknowledge silently acknowledges a callback query.
func (*Bot) Answer ¶
func (b *Bot) Answer(ctx context.Context, cb *tg.CallbackQuery, opts ...sender.AnswerOption) error
Answer answers a callback query.
func (*Bot) Close ¶
Close releases all resources. Close is idempotent; subsequent calls are no-ops.
In polling mode, Close() waits for the poll loop to exit (via Stop()), then closes the updates channel. It is safe to range over Updates() until it closes.
In webhook mode, the updates channel is NOT closed because the HTTP server may still be accepting connections. Stop your HTTP server first, then call Close(). If you need to drain the channel after stopping the server, use a timeout select loop.
func (*Bot) Copy ¶
func (b *Bot) Copy(ctx context.Context, e tg.Editable, toChatID tg.ChatID, opts ...sender.CopyOption) (*tg.MessageID, error)
Copy copies a message.
func (*Bot) Edit ¶
func (b *Bot) Edit(ctx context.Context, e tg.Editable, text string, opts ...sender.EditOption) (*tg.Message, error)
Edit edits a message text.
func (*Bot) Forward ¶
func (b *Bot) Forward(ctx context.Context, e tg.Editable, toChatID tg.ChatID, opts ...sender.ForwardOption) (*tg.Message, error)
Forward forwards a message.
func (*Bot) SendMessage ¶
func (b *Bot) SendMessage(ctx context.Context, chatID tg.ChatID, text string, opts ...SendOption) (*tg.Message, error)
SendMessage sends a text message.
func (*Bot) SendPhoto ¶
func (b *Bot) SendPhoto(ctx context.Context, chatID tg.ChatID, photo string, opts ...PhotoOption) (*tg.Message, error)
SendPhoto sends a photo from URL or file_id. For uploading files, use SendPhotoFile.
func (*Bot) SendPhotoFile ¶
func (b *Bot) SendPhotoFile(ctx context.Context, chatID tg.ChatID, photo sender.InputFile, opts ...PhotoOption) (*tg.Message, error)
SendPhotoFile sends a photo from InputFile (supports upload).
func (*Bot) WebhookHandler ¶
func (b *Bot) WebhookHandler() *receiver.WebhookHandler
WebhookHandler returns the HTTP handler for webhook mode.
type Option ¶
type Option func(*botConfig)
Option configures the Bot.
func WithAllowedUpdates ¶
WithAllowedUpdates filters update types.
func WithDeleteWebhook ¶
WithDeleteWebhook deletes existing webhook before polling.
func WithPolling ¶
WithPolling configures long polling mode.
func WithPollingMaxErrors ¶
WithPollingMaxErrors sets max consecutive errors.
func WithRateLimit ¶
WithRateLimit sets rate limiting.
func WithUpdateBufferSize ¶
WithUpdateBufferSize sets the updates channel buffer size.
func WithWebhook ¶
WithWebhook configures webhook mode.
type PhotoOption ¶
type PhotoOption func(*sender.SendPhotoRequest)
PhotoOption configures send photo requests.
func WithPhotoCaption ¶
func WithPhotoCaption(caption string) PhotoOption
WithPhotoCaption sets the photo caption.
func WithPhotoKeyboard ¶
func WithPhotoKeyboard(kb *tg.InlineKeyboardMarkup) PhotoOption
WithPhotoKeyboard sets the reply keyboard.
func WithPhotoParseMode ¶
func WithPhotoParseMode(mode tg.ParseMode) PhotoOption
WithPhotoParseMode sets parse mode for caption.
type SendOption ¶
type SendOption func(*sender.SendMessageRequest)
SendOption configures send message requests.
func WithKeyboard ¶
func WithKeyboard(kb *tg.InlineKeyboardMarkup) SendOption
WithKeyboard sets the reply keyboard.
func WithParseMode ¶
func WithParseMode(mode tg.ParseMode) SendOption
WithParseMode sets the parse mode.
func WithReplyTo ¶
func WithReplyTo(messageID int) SendOption
WithReplyTo sets the reply-to message ID.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
galigo-testbot
command
|
|
|
examples
|
|
|
echo
command
Example: Simple echo bot using galigo
|
Example: Simple echo bot using galigo |
|
internal
|
|
|
scrub
Package scrub provides security helpers for removing sensitive data from errors.
|
Package scrub provides security helpers for removing sensitive data from errors. |
|
testutil
Package testutil provides testing utilities for galigo.
|
Package testutil provides testing utilities for galigo. |
|
validate
Package validate provides validation utilities for Telegram API requests.
|
Package validate provides validation utilities for Telegram API requests. |
|
Package receiver provides Telegram update receiving via webhook or long polling.
|
Package receiver provides Telegram update receiving via webhook or long polling. |
|
Package sender provides Telegram message sending with resilience features.
|
Package sender provides Telegram message sending with resilience features. |
|
Package tg provides core Telegram types shared by receiver and sender.
|
Package tg provides core Telegram types shared by receiver and sender. |