From c71aad0c7926bf86ef41ebb831aa5eb86eeec640 Mon Sep 17 00:00:00 2001 From: ScuroNeko Date: Tue, 3 Feb 2026 16:41:34 +0300 Subject: [PATCH] uploader --- api.go | 15 +++++++++++++ methods.go | 9 ++++++++ msg_context.go | 7 ++++++ types.go | 7 ++++++ uploader.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 uploader.go diff --git a/api.go b/api.go index e4c6645..aa9983e 100644 --- a/api.go +++ b/api.go @@ -63,3 +63,18 @@ func (r TelegramRequest[R, P]) Do(bot *Bot) (*R, error) { } return &response.Result, nil } + +func (b *Bot) GetFileByLink(link string) ([]byte, error) { + c := http.DefaultClient + u := fmt.Sprintf("https://api.telegram.org/file/bot%s/%s", b.token, link) + req, err := http.NewRequest("GET", u, nil) + if err != nil { + return nil, err + } + res, err := c.Do(req) + if err != nil { + return nil, err + } + defer res.Body.Close() + return io.ReadAll(res.Body) +} diff --git a/methods.go b/methods.go index 2a91af7..723a321 100644 --- a/methods.go +++ b/methods.go @@ -155,3 +155,12 @@ func (b *Bot) AnswerCallbackQuery(params *AnswerCallbackQueryP) (bool, error) { } return *ok, err } + +type GetFileP struct { + FileId string `json:"file_id"` +} + +func (b *Bot) GetFile(params *GetFileP) (*File, error) { + req := NewRequest[File]("getFile", params) + return req.Do(b) +} diff --git a/msg_context.go b/msg_context.go index 748bc41..319dc56 100644 --- a/msg_context.go +++ b/msg_context.go @@ -76,6 +76,10 @@ func (ctx *MsgContext) editPhotoText(messageId int, text string, kb *InlineKeybo } } func (m *AnswerMessage) EditCaption(text string) *AnswerMessage { + if m.MessageID == 0 { + m.ctx.Bot.logger.Errorln("Can't edit caption message, message id is zero") + return m + } return m.ctx.editPhotoText(m.MessageID, text, nil) } func (m *AnswerMessage) EditCaptionKeyboard(text string, kb *InlineKeyboard) *AnswerMessage { @@ -124,6 +128,9 @@ func (ctx *MsgContext) answerPhoto(photoId, text string, kb *InlineKeyboard) *An msg, err := ctx.Bot.SendPhoto(params) if err != nil { ctx.Bot.logger.Errorln(err) + return &AnswerMessage{ + ctx: ctx, Text: text, IsMedia: true, + } } return &AnswerMessage{ MessageID: msg.MessageID, ctx: ctx, Text: text, IsMedia: true, diff --git a/types.go b/types.go index f2fa3b7..d6c278f 100644 --- a/types.go +++ b/types.go @@ -172,3 +172,10 @@ type ReactionCount struct { Type *ReactionType `json:"type"` TotalCount int `json:"total_count"` } + +type File struct { + FileId string `json:"file_id"` + FileUniqueID string `json:"file_unique_id"` + FileSize int `json:"file_size,omitempty"` + FilePath string `json:"file_path,omitempty"` +} diff --git a/uploader.go b/uploader.go new file mode 100644 index 0000000..3c1fadb --- /dev/null +++ b/uploader.go @@ -0,0 +1,59 @@ +package laniakea + +import ( + "bytes" + "fmt" + "io" + "log" + "mime/multipart" + "net/http" + "strconv" +) + +type Uploader struct { + bot *Bot +} + +func NewUploader(bot *Bot) *Uploader { + return &Uploader{bot: bot} +} +func (u *Uploader) UploadPhoto(chatId int, data []byte) error { + url := fmt.Sprintf("https://api.telegram.org/bot%s/sendPhoto", u.bot.token) + + buf := bytes.NewBuffer(nil) + w := multipart.NewWriter(buf) + defer w.Close() + err := w.WriteField("chat_id", strconv.Itoa(chatId)) + if err != nil { + return err + } + fw, err := w.CreateFormFile("photo", "photo.jpg") + if err != nil { + return err + } + _, err = fw.Write(data) + if err != nil { + return err + } + err = w.Close() + if err != nil { + return err + } + + req, err := http.NewRequest("POST", url, buf) + if err != nil { + return err + } + req.Header.Set("Content-Type", w.FormDataContentType()) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return err + } + log.Println("upload", string(body)) + return nil +}