telegram

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package telegram provides a Telegram bot client for sending notifications and handling commands with built-in chat ID authorization.

It wraps the github.com/go-telegram/bot library and adds:

  • Chat ID whitelist middleware for access control
  • A simple Notify function for sending messages to a default chat
  • Global convenience API via Init/Start/Notify
  • Multi-instance support via the Bot struct

Quick Start

Send a one-off notification:

if err := telegram.Init(telegram.Config{
    BotToken: "123456:ABC-DEF...",
    ChatID:   "12345678",
}); err != nil {
    log.Fatal(err)
}

if err := telegram.Notify("Hello from Go!"); err != nil {
    log.Fatal(err)
}

Start the bot for receiving commands (blocking):

telegram.Start(ctx)

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotInitialized = errors.New("telegram: bot not initialized")
	ErrNoChatID       = errors.New("telegram: no default notify chat ID configured")
)

Common sentinel errors.

Functions

func AuthMiddleware

func AuthMiddleware(allowedIds []int64) bot.Middleware

AuthMiddleware creates a middleware that allows only specific chat IDs.

func Init

func Init(cfg Config) error

Init initializes the global bot instance. It is safe to call multiple times; subsequent calls replace the global instance.

func Notify

func Notify(msg string) error

Notify sends a text message to the default chat ID (first in the list) using the global bot.

func Start

func Start(ctx context.Context)

Start starts the bot long-polling. This call blocks until ctx is cancelled.

Types

type Bot

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

Bot wraps the Telegram bot client with auth and notification support.

func GetBot

func GetBot() *Bot

GetBot returns the global bot instance.

func NewBot

func NewBot(cfg Config) (*Bot, error)

NewBot creates a new Bot from the given config. Returns the Bot and any error encountered during creation.

func (*Bot) Notify

func (b *Bot) Notify(msg string) error

Notify sends a text message to the default chat ID (first in the list).

func (*Bot) RegisterHandler

func (b *Bot) RegisterHandler(handlerType bot.HandlerType, pattern string, matchType bot.MatchType, handler bot.HandlerFunc) string

RegisterHandler exposes the underlying client's RegisterHandler method.

type Config

type Config struct {
	BotToken string // Telegram bot token
	ChatID   string // Comma-separated chat IDs (first is default for Notify)
}

Config holds Telegram bot settings.

Jump to

Keyboard shortcuts

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