This commit is contained in:
2026-02-05 12:16:25 +03:00
parent 7a6f135487
commit 60f09e940a
11 changed files with 319 additions and 110 deletions

31
api.go
View File

@@ -7,8 +7,27 @@ import (
"io"
"net/http"
"strings"
"git.nix13.pw/scuroneko/slog"
)
type Api struct {
token string
logger *slog.Logger
}
func NewAPI(token string) *Api {
l := slog.CreateLogger().Level(GetLoggerLevel()).Prefix("API")
l.AddWriter(l.CreateJsonStdoutWriter())
return &Api{
token: token,
logger: l,
}
}
func (api *Api) CloseApi() {
api.logger.Close()
}
type ApiResponse[R any] struct {
Ok bool `json:"ok"`
Description string `json:"description,omitempty"`
@@ -24,21 +43,21 @@ type TelegramRequest[R, P any] struct {
func NewRequest[R, P any](method string, params P) TelegramRequest[R, P] {
return TelegramRequest[R, P]{method: method, params: params}
}
func (r TelegramRequest[R, P]) Do(bot *Bot) (*R, error) {
func (r TelegramRequest[R, P]) Do(api *Api) (*R, error) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(r.params)
if err != nil {
return nil, err
}
if bot.requestLogger != nil {
bot.requestLogger.Debugln(strings.ReplaceAll(fmt.Sprintf(
if api.logger != nil {
api.logger.Debugln(strings.ReplaceAll(fmt.Sprintf(
"POST https://api.telegram.org/bot%s/%s %s",
"<TOKEN>", r.method, buf.String(),
), "\n", ""))
}
req, err := http.Post(fmt.Sprintf("https://api.telegram.org/bot%s/%s", bot.token, r.method), "application/json", &buf)
req, err := http.Post(fmt.Sprintf("https://api.telegram.org/bot%s/%s", api.token, r.method), "application/json", &buf)
if err != nil {
return nil, err
}
@@ -48,8 +67,8 @@ func (r TelegramRequest[R, P]) Do(bot *Bot) (*R, error) {
return nil, err
}
if bot.requestLogger != nil {
bot.requestLogger.Debugln(fmt.Sprintf("RES %s %s", r.method, string(data)))
if api.logger != nil {
api.logger.Debugln(fmt.Sprintf("RES %s %s", r.method, string(data)))
}
response := new(ApiResponse[R])