uploader
This commit is contained in:
15
api.go
15
api.go
@@ -63,3 +63,18 @@ func (r TelegramRequest[R, P]) Do(bot *Bot) (*R, error) {
|
|||||||
}
|
}
|
||||||
return &response.Result, nil
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -155,3 +155,12 @@ func (b *Bot) AnswerCallbackQuery(params *AnswerCallbackQueryP) (bool, error) {
|
|||||||
}
|
}
|
||||||
return *ok, err
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -76,6 +76,10 @@ func (ctx *MsgContext) editPhotoText(messageId int, text string, kb *InlineKeybo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (m *AnswerMessage) EditCaption(text string) *AnswerMessage {
|
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)
|
return m.ctx.editPhotoText(m.MessageID, text, nil)
|
||||||
}
|
}
|
||||||
func (m *AnswerMessage) EditCaptionKeyboard(text string, kb *InlineKeyboard) *AnswerMessage {
|
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)
|
msg, err := ctx.Bot.SendPhoto(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Bot.logger.Errorln(err)
|
ctx.Bot.logger.Errorln(err)
|
||||||
|
return &AnswerMessage{
|
||||||
|
ctx: ctx, Text: text, IsMedia: true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return &AnswerMessage{
|
return &AnswerMessage{
|
||||||
MessageID: msg.MessageID, ctx: ctx, Text: text, IsMedia: true,
|
MessageID: msg.MessageID, ctx: ctx, Text: text, IsMedia: true,
|
||||||
|
|||||||
7
types.go
7
types.go
@@ -172,3 +172,10 @@ type ReactionCount struct {
|
|||||||
Type *ReactionType `json:"type"`
|
Type *ReactionType `json:"type"`
|
||||||
TotalCount int `json:"total_count"`
|
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"`
|
||||||
|
}
|
||||||
|
|||||||
59
uploader.go
Normal file
59
uploader.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user