api

package
v0.0.0-...-6dcbb19 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package api provides API for calling Telegram for TGO.

This API package describes the main part of communication with Telegram Bot API.

The response represents the API response from Telegram with respectful result and error values.

Caller interface represents the general logic of sending requests to API and receiving responses from it. Currently, TGO provides only valyala/fasthttp implementation, but your own can be defined and specified via tgo.BotOption's.

RequestConstructor interface represents a general way of constructing RequestData used in Caller. Currently, TGO provides an only default implementation that uses goccy/go-json instead of encoding/json and std mime/multipart package.

NamedReader interface represents a general way of sending files that are provided to RequestConstructor. As io.Reader can be provided any valid reader and name method should return a unique name for every file in one request, otherwise not all files will be sent properly.

Index

Constants

View Source
const (
	// ContentTypeJSON http content type
	ContentTypeJSON = "application/json"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Caller

type Caller interface {
	Call(url string, data *RequestData) (*Response, error)
}

Caller represents way to call API with request

type DefaultConstructor

type DefaultConstructor struct{}

DefaultConstructor default implementation of RequestConstructor

func (DefaultConstructor) JSONRequest

func (d DefaultConstructor) JSONRequest(parameters interface{}) (*RequestData, error)

JSONRequest is default implementation

func (DefaultConstructor) MultipartRequest

func (d DefaultConstructor) MultipartRequest(parameters map[string]string, filesParameters map[string]NamedReader) (
	*RequestData, error,
)

MultipartRequest is default implementation

type Error

type Error struct {
	Description string              `json:"description,omitempty"`
	ErrorCode   int                 `json:"error_code,omitempty"`
	Parameters  *ResponseParameters `json:"parameters,omitempty"`
}

Error represents error from telegram API

func (Error) Error

func (a Error) Error() string

Error converts Error to human-readable string

type FasthttpAPICaller

type FasthttpAPICaller struct {
	Client *fasthttp.Client
}

FasthttpAPICaller fasthttp implementation of Caller

func (FasthttpAPICaller) Call

func (a FasthttpAPICaller) Call(url string, data *RequestData) (*Response, error)

Call is fasthttp implementation

type NamedReader

type NamedReader interface {
	io.Reader
	Name() string
}

NamedReader represents a way to send files (or other data). Implemented by os.File. Note: Name method should return unique names for all files sent in one request.

Warning: Since for sending data (files) reader data will be copied, using same reader multiple times as is will not work. For os.File you can use file.Seek(0, io.SeekStart) to prepare for new request.

type RequestConstructor

type RequestConstructor interface {
	JSONRequest(parameters interface{}) (*RequestData, error)
	MultipartRequest(parameters map[string]string, filesParameters map[string]NamedReader) (*RequestData, error)
}

RequestConstructor represents way to construct API request

type RequestData

type RequestData struct {
	ContentType string
	Buffer      *bytes.Buffer
}

RequestData represents data needed to execute request

type Response

type Response struct {
	Ok     bool            `json:"ok"`
	Result json.RawMessage `json:"result,omitempty"`
	*Error
}

Response represents response returned by Telegram API

func (Response) String

func (r Response) String() string

type ResponseParameters

type ResponseParameters struct {
	// MigrateToChatID - Optional. The group has been migrated to a supergroup with the specified identifier.
	// This number may have more than 32 significant bits and some programming languages may have difficulty/silent
	// defects in interpreting it. But it has at most 52 significant bits, so a signed 64-bit integer or
	// double-precision float type are safe for storing this identifier.
	MigrateToChatID int64 `json:"migrate_to_chat_id,omitempty"`

	// RetryAfter - Optional. In case of exceeding flood control, the number of seconds left to wait before the
	// request can be repeated
	RetryAfter int `json:"retry_after,omitempty"`
}

ResponseParameters - Describes why a request was unsuccessful.

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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