tg

package module
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 12 Imported by: 0

README

tg

Go Report Card Go Reference GitHub release (latest SemVer)

Another 😩 simple library (and cli 😉) for sending text messages via telegram bot

Documentation

Index

Constants

View Source
const (
	MarkdownV2ParseMode = "MarkdownV2"
	MarkdownParseMode   = "Markdown"
	HTMLParseMode       = "HTML"
)
View Source
const MaxTextSize int = 4096

Variables

View Source
var (
	ErrEmptyChatID = errors.New("empty chat_id")
	ErrEmptyText   = errors.New("empty text")
	ErrTextTooLong = errors.New("text too long")
)
View Source
var (
	ErrIncorrectScheme = errors.New("incorrect scheme")
	ErrEmptyHost       = errors.New("empty host")
)
View Source
var (
	ErrValueNil             = errors.New("value is nil")
	ErrValueNotPtr          = errors.New("value not ptr")
	ErrValueNotStructOrBool = errors.New("value not struct or bool")
)
View Source
var ErrHTTPClientNil = errors.New("httpclient is nil")
View Source
var ErrIncorrectMessageID = errors.New("incorrect message_id")
View Source
var ErrIncorrectMessageThreadID = errors.New("incorrect message_thread_id")
View Source
var ErrIncorrentToken = errors.New("incorrect token")
View Source
var ErrUnknownParseMode = errors.New("unknown parse_mode")

Functions

This section is empty.

Types

type BaseMessage added in v0.10.0

type BaseMessage struct {
	ChatID    int64     `json:"chat_id"`
	Text      string    `json:"text"`
	ParseMode ParseMode `json:"parse_mode,omitempty"`
}

func (*BaseMessage) Validate added in v0.10.0

func (bm *BaseMessage) Validate() error

type Client added in v0.10.0

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

func NewClient added in v0.10.0

func NewClient(token string, options ...Option) (*Client, error)

func (*Client) API added in v0.10.0

func (c *Client) API(ctx context.Context, method string, req, resp any) error

func (*Client) DeleteMessage added in v0.10.0

func (c *Client) DeleteMessage(ctx context.Context, chatID, messageID int64) (bool, error)

func (*Client) EditMessage added in v0.10.0

func (c *Client) EditMessage(ctx context.Context,
	chatID, messageID int64, text string, opts ...EditOption,
) (*Message, error)

func (*Client) GetMe added in v0.10.0

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

func (*Client) SendMessage added in v0.10.0

func (c *Client) SendMessage(ctx context.Context,
	chatID int64, text string, opts ...SendOption,
) (*Message, error)

type DeleteMessage added in v0.10.0

type DeleteMessage struct {
	ChatID    int64 `json:"chat_id"`
	MessageID int64 `json:"message_id"`
}

func NewDeleteMessage added in v0.10.0

func NewDeleteMessage(chatID int64, messageID int64) (*DeleteMessage, error)

func (*DeleteMessage) Validate added in v0.10.0

func (dm *DeleteMessage) Validate() error

type EditMessage added in v0.10.0

type EditMessage struct {
	MessageID int64 `json:"message_id"`
	BaseMessage
}

func NewEditMessage added in v0.10.0

func NewEditMessage(chatID int64, messageID int64, text string, opts ...EditOption) (*EditMessage, error)

func (*EditMessage) Validate added in v0.10.0

func (em *EditMessage) Validate() error

type EditOption added in v0.10.0

type EditOption func(*EditMessage)

func ParseModeEditOption added in v0.10.0

func ParseModeEditOption(mode ParseMode) EditOption

type HTTPClient added in v0.5.0

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

type Message

type Message struct {
	MessageID int64 `json:"message_id"`
	Date      int   `json:"date"`
}

type Option added in v0.6.0

type Option func(*Client) error

func WithAPIServer added in v0.10.0

func WithAPIServer(server string) Option

func WithHTTPClient added in v0.10.0

func WithHTTPClient(client HTTPClient) Option

type ParseMode added in v0.5.0

type ParseMode string

func (ParseMode) Validate added in v0.10.0

func (m ParseMode) Validate() error

type Response added in v0.10.0

type Response struct {
	Result interface{} `json:"result,omitempty"`
	ResponseError
}

type ResponseError added in v0.10.0

type ResponseError struct {
	Ok          bool   `json:"ok"`
	ErrorCode   int    `json:"error_code,omitempty"`
	Description string `json:"description,omitempty"`
	Parameters  struct {
		RetryAfter int `json:"retry_after,omitempty"`
	} `json:"parameters,omitempty"`
}

func (ResponseError) Error added in v0.10.0

func (r ResponseError) Error() string

type SendMessage added in v0.10.0

type SendMessage struct {
	BaseMessage
	MessageThreadID       int64 `json:"message_thread_id,omitempty"`
	DisableWebPagePreview bool  `json:"disable_web_page_preview,omitempty"`
	DisableNotification   bool  `json:"disable_notification,omitempty"`
	ProtectContent        bool  `json:"protect_content,omitempty"`
}

func NewSendMessage added in v0.10.0

func NewSendMessage(chatID int64, text string, opts ...SendOption) (*SendMessage, error)

func (*SendMessage) Validate added in v0.10.0

func (sm *SendMessage) Validate() error

type SendOption added in v0.10.0

type SendOption func(*SendMessage)

func DisableNotificationSendOption added in v0.10.0

func DisableNotificationSendOption(disable bool) SendOption

func DisableWebPagePreviewSendOption added in v0.10.0

func DisableWebPagePreviewSendOption(disable bool) SendOption

func MessageThreadIDSendOption added in v0.10.0

func MessageThreadIDSendOption(threadID int64) SendOption

func ParseModeSendOption added in v0.10.0

func ParseModeSendOption(mode ParseMode) SendOption

func ProtectContentSendOption added in v0.10.0

func ProtectContentSendOption(protect bool) SendOption

type TG

type TG interface {
	GetMe(ctx context.Context) (*User, error)
	SendMessage(ctx context.Context, chatID int64, text string, opts ...SendOption) (*Message, error)
	EditMessage(ctx context.Context, chatID, messageID int64, text string, opts ...EditOption) (*Message, error)
	DeleteMessage(ctx context.Context, chatID, messageID int64) (bool, error)
}

type User

type User struct {
	ID        int64  `json:"id"`
	FirstName string `json:"first_name"`
	UserName  string `json:"username,omitempty"`
}

Directories

Path Synopsis
cmd
tg

Jump to

Keyboard shortcuts

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