api

package
v0.0.0-...-ca9d3f5 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

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

DefaultAPIURL is a default url for telegram api.

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

DefaultFileURL is a default url for downloading files.

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON[T any](r io.Reader) (*T, []byte, error)

DecodeJSON decodes reader into object or returns raw json data if error occured.

func Request

func Request[T any](a *API, method string, d *Data) (T, error)

Request performs a request to the Bot API with background context.

func RequestContext

func RequestContext[T any](ctx context.Context, a *API, method string, data *Data) (result T, err error)

RequestContext performs a request to the Bot API.

Types

type API

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

API provides access to the Telegram Bot API.

func New

func New(token string, apiURL, fileURL string, client *http.Client) (*API, error)

New creates a new API instance. If apiURL or fileURL are empty, the Telegram defaults are used. If client is nil, the http.DefaultClient is used.

func NewDefault

func NewDefault(token string) (*API, error)

NewDefault creates a new API instance with default values.

func (*API) DownloadFile

func (a *API) DownloadFile(path string) (io.ReadCloser, error)

DownloadFile downloads a file from the server with background context.

func (*API) DownloadFileContext

func (a *API) DownloadFileContext(ctx context.Context, path string) (io.ReadCloser, error)

DownloadFileContext downloads a file from the server.

func (*API) Request

func (a *API) Request(method string, d *Data) error

Request performs a request to the Bot API with background context, but doesn't parse the result.

func (*API) RequestContext

func (a *API) RequestContext(ctx context.Context, method string, d *Data) error

RequestContext performs a request to the Bot API but doesn't parse the result.

type Data

type Data struct {
	// contains query parameters and files that does not need to be uploaded.
	Params map[string]string

	// key is a multipart field name.
	Files map[string]*tg.InputFile
	// contains filtered or unexported fields
}

Data contains query parameters and files data.

func NewData

func NewData() *Data

NewData creates new Data object.

func (*Data) AddAttach

func (d *Data) AddAttach(file tg.Inputtable)

AddAttach links a file to the multipart field and adds it. If the file is not *tg.InputFile, it does nothing.

func (*Data) AddFile

func (d *Data) AddFile(field string, file tg.Inputtable)

AddFile adds file.

func (*Data) Data

func (d *Data) Data() (string, io.Reader)

Data encodes the values into “URL encoded” form or multipart/form-data.

func (*Data) Set

func (d *Data) Set(k, v string, force ...bool) *Data

Set sets the key to value.

func (*Data) SetBool

func (d *Data) SetBool(key string, v bool, force ...bool) *Data

SetBool sets the key to bool value.

func (*Data) SetFile

func (d *Data) SetFile(field string, file, thumb tg.Inputtable)

SetFile sets file with thumbnail.

func (*Data) SetFloat

func (d *Data) SetFloat(key string, v float32, force ...bool) *Data

SetFloat sets the key to float value.

func (*Data) SetFloat64

func (d *Data) SetFloat64(key string, v float64, force ...bool) *Data

SetFloat64 sets the key to float64 value.

func (*Data) SetInt

func (d *Data) SetInt(key string, v int, force ...bool) *Data

SetInt sets the key to int value.

func (*Data) SetInt64

func (d *Data) SetInt64(key string, v int64, force ...bool) *Data

SetInt64 sets the key to int64 value.

func (*Data) SetJSON

func (d *Data) SetJSON(key string, v any) *Data

SetJSON sets the key to JSON value.

type DownloadError

type DownloadError struct {
	Status int
	Path   string
	Err    error
}

DownloadError represents download error.

func (*DownloadError) Error

func (e *DownloadError) Error() string

func (*DownloadError) Unwrap

func (e *DownloadError) Unwrap() error

type Empty

type Empty struct{}

Empty type is used to avoid spending resources on unmarshaling.

func (*Empty) UnmarshalJSON

func (e *Empty) UnmarshalJSON([]byte) error

UnmarshalJSON implements json.Unmarshaler.

type Error

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

Error represents a telegram api error and also contains method and data.

func (Error) Error

func (e Error) Error() string

func (*Error) Is

func (e *Error) Is(err error) bool

Is implements errors.Is interface.

func (Error) Unwrap

func (e Error) Unwrap() error

type JSONError

type JSONError struct {
	Status   int
	Response []byte
	// contains filtered or unexported fields
}

JSONError represents JSON error.

func (*JSONError) Error

func (e *JSONError) Error() string

func (JSONError) Unwrap

func (e JSONError) Unwrap() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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