tg

package module
v0.0.0-...-6a383b9 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2024 License: MIT Imports: 8 Imported by: 0

README

Telegram Bot API

An incomplete hand-crafted Telegram Bot API client implementation.

Example

tg:main* λ go build ./cmd/example
tg:main* λ ./example -token "token"
time=2024-08-29T13:33:43.969+03:00 level=INFO msg="user info" id=1122334455 username=TestBot first_name=Test last_name="" is_bot=true
time=2024-08-29T13:33:49.850+03:00 level=INFO msg=message id=8 time=2024-08-29T13:33:50.000+03:00 chat.id=1122334455 chat.type=private chat.title="" sender.id=1122334455 sender.username=testusername sender.first_name=John sender.last_name=Doe sender.lang_code=en text=test
^Ctime=2024-08-29T13:33:52.870+03:00 level=INFO msg=done

Documentation

Index

Constants

View Source
const BaseURL = "https://api.telegram.org/bot"

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON[T any](payload []byte) (T, error)

func Updates

func Updates(ctx context.Context, c Caller, limit int, timeout time.Duration, onErr OnError) <-chan Update

Types

type Caller

type Caller interface {
	Call(ctx context.Context, value Sendable) (json.RawMessage, error)
}

type Chat

type Chat struct {
	ID    int64    `json:"id"`
	Title string   `json:"title"`
	Type  ChatType `json:"type"`
}

type ChatType

type ChatType string
const (
	ChatTypePrivate    ChatType = "private"
	ChatTypeGroup      ChatType = "group"
	ChatTypeSupergroup ChatType = "supergroup"
	ChatTypeChannel    ChatType = "channel"
)

type Client

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

func New

func New(token string, opts ...Option) *Client

func (*Client) Call

func (c *Client) Call(ctx context.Context, value Sendable) (json.RawMessage, error)

type Error

type Error struct {
	Code    int
	Message string
}

func (Error) Error

func (e Error) Error() string

type HTTPClientDoer

type HTTPClientDoer interface {
	Do(*http.Request) (*http.Response, error)
}

type InlineKeyboardButton

type InlineKeyboardButton struct {
	Text         string `json:"text"`
	CallbackData string `json:"callback_data"`
}

func NewInlineKeyboardButtonRow

func NewInlineKeyboardButtonRow(buttons ...InlineKeyboardButton) []InlineKeyboardButton

type InlineKeyboardMarkup

type InlineKeyboardMarkup struct {
	InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
}

func NewInlineKeyboard

func NewInlineKeyboard(rows ...[]InlineKeyboardButton) InlineKeyboardMarkup

type KeyboardButton

type KeyboardButton struct {
	Text string `json:"text"`
}

func NewKeyboardButtonRow

func NewKeyboardButtonRow(buttons ...KeyboardButton) []KeyboardButton

type KeyboardMarkup

type KeyboardMarkup struct {
	Keyboard        [][]KeyboardButton `json:"keyboard"`
	ResizeKeyboard  bool               `json:"resize_keyboard"`
	OneTimeKeyboard bool               `json:"one_time_keyboard"`
}

func NewKeyboard

func NewKeyboard(rows ...[]KeyboardButton) KeyboardMarkup

type LinkPreviewOptions

type LinkPreviewOptions struct {
	Disabled         bool   `json:"is_disabled"`
	URL              string `json:"url,omitempty"`
	PreferSmallMedia bool   `json:"prefer_small_media"`
	PreferLargeMedia bool   `json:"prefer_large_media"`
	ShowAboveText    bool   `json:"show_above_text"`
}

type Message

type Message struct {
	ID               int64  `json:"message_id"`
	Unixtime         int64  `json:"date"`
	Chat             Chat   `json:"chat"`
	From             *User  `json:"from"`
	Text             string `json:"text"`
	GroupChatCreated bool   `json:"group_chat_created"`
	NewChatTitle     string `json:"new_chat_title,omitempty"`
	NewChatMembers   []User `json:"new_chat_members,omitempty"`
	LeftChatMember   *User  `json:"left_chat_member,omitempty"`
}

func SendMessage

func SendMessage[R Recipient, M ReplyMarkup](ctx context.Context,
	c Caller,
	chatID int64,
	text string,
	opts ...OptionMessage[R, M],
) (*Message, error)

func (*Message) Method

func (m *Message) Method() string

func (*Message) Params

func (m *Message) Params() json.RawMessage

func (*Message) Sender

func (m *Message) Sender() *User

func (*Message) Time

func (m *Message) Time() time.Time

type MessageConfig

type MessageConfig[R Recipient, M ReplyMarkup] struct {
	ChatID              int64               `json:"chat_id"`
	ThreadID            int64               `json:"message_thread_id,omitempty"`
	Text                string              `json:"text"`
	ParseMode           ParseMode           `json:"parse_mode,omitempty"`
	Entities            []MessageEntity     `json:"entities,omitempty"`
	LinkPreviewOptions  *LinkPreviewOptions `json:"LinkPreviewOptions,omitempty"`
	DisableNotification bool                `json:"disable_notification"`
	ProtectContent      bool                `json:"protect_content"`
	ReplyParameters     *ReplyParameters[R] `json:"reply_parameters,omitempty"`
	ReplyMarkup         M                   `json:"reply_markup,omitempty"`
}

func (MessageConfig[R, M]) Method

func (c MessageConfig[R, M]) Method() string

func (MessageConfig[R, M]) Params

func (c MessageConfig[R, M]) Params() json.RawMessage

type MessageEntity

type MessageEntity struct {
	Type          MessageEntityType `json:"type"`
	Offset        int64             `json:"offset"`
	Length        int64             `json:"length"`
	URL           string            `json:"url"`
	User          *User             `json:"user,omitempty"`
	Language      string            `json:"language,omitempty"`
	CustomEmojiID string            `json:"custom_emoji_id,omitempty"`
}

type MessageEntityType

type MessageEntityType string
const (
	MessageEntityyMention      MessageEntityType = "mention"
	MessageEntityHashtag       MessageEntityType = "hashtag"
	MessageEntityCashtag       MessageEntityType = "cashtag"
	MessageEntityBotCommand    MessageEntityType = "bot_command"
	MessageEntityURL           MessageEntityType = "url"
	MessageEntityEmail         MessageEntityType = "email"
	MessageEntityPhoneNumber   MessageEntityType = "phone_number"
	MessageEntityBold          MessageEntityType = "bold"
	MessageEntityItalic        MessageEntityType = "italic"
	MessageEntityUnderlime     MessageEntityType = "underline"
	MessageEntityStrikethrough MessageEntityType = "strikethrough"
	MessageEntitySpoiler       MessageEntityType = "spoiler"
	MessageEntityBlockquote    MessageEntityType = "blockquote"
	MessageEntityCode          MessageEntityType = "code"
	MessageEntityPre           MessageEntityType = "pre"
	MessageEntityTextLink      MessageEntityType = "text_link"
	MessageEntityTextMention   MessageEntityType = "text_mention"
	MessageEntityCustomEmoji   MessageEntityType = "custom_emoji"
)

type OnError

type OnError func(req RequestGetUpdates, err error) (stop bool)

type Option

type Option func(*Client)

func WithBaseURL

func WithBaseURL(baseURL string) Option

func WithHTTPClient

func WithHTTPClient(httpClient HTTPClientDoer) Option

func WithLogger

func WithLogger(logger *slog.Logger) Option

type OptionMessage

type OptionMessage[R Recipient, M ReplyMarkup] func(c *MessageConfig[R, M])

func WithKeyboard

func WithKeyboard[R Recipient](markup KeyboardMarkup) OptionMessage[R, KeyboardMarkup]

type ParseMode

type ParseMode string
const (
	ParseModeHTML       ParseMode = "HTML"
	ParseModeMarkdownV2 ParseMode = "MarkdownV2"
	ParseModeMarkdown   ParseMode = "Markdown"
)

type Recipient

type Recipient interface {
	~int64 | ~string
}

type ReplyMarkup

type ReplyMarkup interface {
	KeyboardMarkup | InlineKeyboardMarkup
}

type ReplyParameters

type ReplyParameters[T Recipient] struct {
	MessageID int64 `json:"message_id"`
	ChatID    T     `json:"chat_id"`
}

type RequestGetMe

type RequestGetMe struct{}

func (RequestGetMe) Method

func (r RequestGetMe) Method() string

func (RequestGetMe) Params

func (r RequestGetMe) Params() json.RawMessage

type RequestGetUpdates

type RequestGetUpdates struct {
	Offset         int64    `json:"offset"`
	Limit          int      `json:"limit"`
	Timeout        int      `json:"timeout"`
	AllowedUpdates []string `json:"allowed_updates,omitempty"`
}

func (RequestGetUpdates) Method

func (RequestGetUpdates) Method() string

func (RequestGetUpdates) Params

func (r RequestGetUpdates) Params() json.RawMessage

type Sendable

type Sendable interface {
	Method() string
	Params() json.RawMessage
}

type Update

type Update struct {
	ID      int64    `json:"update_id"`
	Message *Message `json:"message"`
}

func GetUpdates

func GetUpdates(ctx context.Context, c Caller, req RequestGetUpdates) ([]Update, error)

type User

type User struct {
	ID            int64  `json:"id"`
	FirstName     string `json:"first_name"`
	LastName      string `json:"last_name"`
	Username      string `json:"username"`
	LanguageCode  string `json:"language_code"`
	IsBot         bool   `json:"is_bot"`
	CanJoinGroups bool   `json:"can_join_groups"`
}

func GetMe

func GetMe(ctx context.Context, c Caller) (*User, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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