telegram

package
v1.7.4 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 28 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package telegram is the Telegram messaging provider, a bot, whose chats are channels. It receives messages by long polling, so the server needs no address on the internet.

Index

Constants

View Source
const (
	// Name is the provider's name
	Name = "telegram"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Error added in v1.7.2

type Error struct {
	Method      string
	Code        int
	Description string
	Retry       time.Duration
	MigrateTo   int64
}

Error is the Bot API's error, of a method: its code, which is an HTTP status, such as 429, its description, and what it says to do, which is how long to wait before calling it again, when it's rate limited, or the supergroup a group became, whose chat is used in its place

func (*Error) Error added in v1.7.2

func (e *Error) Error() string

Error returns the method, and its description, such as telegram sendMessage: Too Many Requests: retry after 5

func (*Error) RetryAfter added in v1.7.2

func (e *Error) RetryAfter() time.Duration

RetryAfter returns how long to wait before calling the method again, when it's rate limited, or zero

func (*Error) Unwrap added in v1.7.2

func (e *Error) Unwrap() error

Unwrap returns the kind of error its code is, such as ErrForbidden, for a token which isn't the bot's, and ErrServiceUnavailable, when it's rate limited, or Telegram fails, with a 5xx

type Opt added in v1.7.2

type Opt func(*opt) error

Opt is an option of the bot

func WithClientOpts added in v1.7.2

func WithClientOpts(opts ...client.ClientOpt) Opt

WithClientOpts are options of go-client, such as client.OptUserAgent, or client.OptTracer, whose spans are of each of the bot's requests, or client.OptEndpoint, for tests, which apply to the bot's requests

func WithTracer added in v1.7.2

func WithTracer(tracer trace.Tracer) Opt

WithTracer sets the tracer of what the bot does, such as sending a message, which can be more than one request, and waiting while it's rate limited, or none, when it's nil. The spans of its requests are client.OptTracer's, with WithClientOpts.

type Telegram

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

Telegram is a Telegram bot, whose chats are channels

func New

func New(token string, opts ...Opt) (*Telegram, error)

New returns the bot with the token, from BotFather, with the options, such as its tracer, and the client options, such as the user agent, or an endpoint for tests. It doesn't call Telegram: the token is checked, and the bot's username, which its links have, is read, once it receives messages, or makes a link, so a bot which Telegram can't be reached for yet, such as while it's rate limited, is still added, and is reached later.

func (*Telegram) Connect added in v1.7.2

func (t *Telegram) Connect(ctx context.Context) (_ string, err error)

Connect checks the token, reading the bot's username, when it isn't known yet, and returns it, such as @ada_bot

func (*Telegram) Delete added in v1.5.0

func (t *Telegram) Delete(ctx context.Context, address, id string) (err error)

Delete deletes the message, which the bot sent

func (*Telegram) Edit added in v1.5.0

func (t *Telegram) Edit(ctx context.Context, address, id, text string) (err error)

Edit changes the text of the message, which the bot sent, to the text, as it is, without formatting, and does nothing when it's the same

func (t *Telegram) Link(ctx context.Context, code string) (_ string, err error)

Link returns the link which opens a chat with the bot, whose Start button sends it the code, or why it can't, such as that Telegram can't be reached, or the token isn't a bot's, when its username isn't known

func (*Telegram) Name

func (*Telegram) Name() string

Name returns the provider's name, telegram

func (*Telegram) Receive

func (t *Telegram) Receive(ctx context.Context, fn func(context.Context, messaging.Message) error) error

Receive calls the function with each message people send the bot, until the context is cancelled, when it returns nil, or updates can't be read. Messages from bots, and without text, are skipped. A message is received once, whatever the function returns.

func (*Telegram) Send

func (t *Telegram) Send(ctx context.Context, address, text string) (_ []messaging.Sent, err error)

Send sends the text, which is Markdown, to the chat, formatted as Telegram's HTML, as more than one message, when it's longer than one can be, broken between paragraphs, or other blocks, and as plain text, when Telegram can't parse its HTML, and returns the messages it was sent as. Links don't have previews.

func (*Telegram) SendFile added in v1.5.0

func (t *Telegram) SendFile(ctx context.Context, address string, file messaging.File, open func() (io.ReadCloser, error), caption string) (_ []messaging.Sent, err error)

SendFile sends the file to the chat, as a photo, when it's an image Telegram shows as one, a voice message, when it's Ogg, or Opus, audio, a track, when it's MP3, or M4A, audio, and otherwise as a document, with the caption, which is Markdown, under it, formatted, when it fits, or as a message before it, when it doesn't, and returns the messages it was sent as. A caption whose formatting Telegram can't parse is sent as plain text, with the file again. A file which is larger than a bot can send is a note, with its name, after its caption.

func (*Telegram) SetLocation added in v1.5.0

func (t *Telegram) SetLocation(fn func() *time.Location)

SetLocation sets the function which returns the time zone the names of files which were sent without names are in, such as the person's, or nil, when it isn't known, when they're in UTC

func (*Telegram) Typing

func (t *Telegram) Typing(ctx context.Context, address string) error

Typing shows "typing…" in the chat, for five seconds, or until a message is sent. It isn't shown again, when it's rate limited, as it's shown again soon anyway.

func (*Telegram) Username

func (t *Telegram) Username() string

Username returns the bot's username, once it's known, or empty

Jump to

Keyboard shortcuts

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