From 7f483056e025fd02f7b71d8696a4811239f12b30 Mon Sep 17 00:00:00 2001 From: krau <71133316+krau@users.noreply.github.com> Date: Wed, 28 May 2025 15:57:10 +0800 Subject: [PATCH] feat: send media to telegram, close #47 --- bot/handle_send.go | 95 ++++++++++++++++++++++++++++++++++++++++++++++ bot/handlers.go | 1 + bot/utils.go | 8 ++++ 3 files changed, 104 insertions(+) create mode 100644 bot/handle_send.go diff --git a/bot/handle_send.go b/bot/handle_send.go new file mode 100644 index 0000000..ffb654c --- /dev/null +++ b/bot/handle_send.go @@ -0,0 +1,95 @@ +package bot + +import ( + "fmt" + "strconv" + "strings" + + "github.com/celestix/gotgproto/dispatcher" + "github.com/celestix/gotgproto/ext" + tgtypes "github.com/celestix/gotgproto/types" + "github.com/gotd/td/tg" +) + +func copyMediaToChat(ctx *ext.Context, msg *tg.Message, chatID int64) (*tgtypes.Message, error) { + media, ok := msg.GetMedia() + if !ok { + return nil, fmt.Errorf("获取媒体失败") + } + + req := &tg.MessagesSendMediaRequest{ + InvertMedia: msg.InvertMedia, + Message: msg.Message, + } + + switch m := media.(type) { + case *tg.MessageMediaDocument: + document, ok := m.Document.AsNotEmpty() + if !ok { + return nil, ErrEmptyDocument + } + inputMedia := &tg.InputMediaDocument{ + ID: document.AsInput(), + } + inputMedia.SetFlags() + req.Media = inputMedia + + case *tg.MessageMediaPhoto: + photo, ok := m.Photo.AsNotEmpty() + if !ok { + return nil, ErrEmptyPhoto + } + inputMedia := &tg.InputMediaPhoto{ + ID: photo.AsInput(), + } + inputMedia.SetFlags() + req.Media = inputMedia + + default: + return nil, fmt.Errorf("不支持的媒体类型: %T", media) + } + + req.SetEntities(msg.Entities) + req.SetFlags() + + return ctx.SendMedia(chatID, req) +} + +func sendFileToTelegram(ctx *ext.Context, update *ext.Update) error { + args := strings.Split(string(update.CallbackQuery.Data), " ") + if len(args) < 3 { + ctx.AnswerCallback(&tg.MessagesSetBotCallbackAnswerRequest{ + QueryID: update.CallbackQuery.QueryID, + Alert: true, + Message: "参数错误", + CacheTime: 5, + }) + return dispatcher.EndGroups + } + fileChatID, _ := strconv.Atoi(args[1]) + fileMessageID, _ := strconv.Atoi(args[2]) + fileMessage, err := GetTGMessage(ctx, int64(fileChatID), fileMessageID) + if err != nil { + ctx.AnswerCallback(&tg.MessagesSetBotCallbackAnswerRequest{ + QueryID: update.CallbackQuery.QueryID, + Alert: true, + Message: "无法获取文件消息", + CacheTime: 5, + }) + return dispatcher.EndGroups + } + _, err = copyMediaToChat(ctx, fileMessage, update.EffectiveChat().GetID()) + if err != nil { + ctx.AnswerCallback(&tg.MessagesSetBotCallbackAnswerRequest{ + QueryID: update.CallbackQuery.QueryID, + Alert: true, + Message: fmt.Sprintf("发送文件失败: %s", err), + CacheTime: 5, + }) + } else { + ctx.AnswerCallback(&tg.MessagesSetBotCallbackAnswerRequest{ + QueryID: update.CallbackQuery.QueryID, + }) + } + return dispatcher.EndGroups +} diff --git a/bot/handlers.go b/bot/handlers.go index 8d87701..4876914 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -29,5 +29,6 @@ func RegisterHandlers(dispatcher dispatcher.Dispatcher) { dispatcher.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix("add"), AddToQueue)) dispatcher.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix("set_default"), setDefaultStorage)) dispatcher.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix("cancel"), cancelTask)) + dispatcher.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix("send_here"), sendFileToTelegram)) dispatcher.AddHandler(handlers.NewMessage(filters.Message.Media, handleFileMessage)) } diff --git a/bot/utils.go b/bot/utils.go index 50a114b..410dbe0 100644 --- a/bot/utils.go +++ b/bot/utils.go @@ -71,6 +71,14 @@ func getSelectStorageMarkup(userChatID int64, fileChatID, fileMessageID int) (*t row.Buttons = buttons[i:min(i+3, len(buttons))] markup.Rows = append(markup.Rows, row) } + markup.Rows = append(markup.Rows, tg.KeyboardButtonRow{ + Buttons: []tg.KeyboardButtonClass{ + &tg.KeyboardButtonCallback{ + Text: "发送到当前聊天", + Data: []byte(fmt.Sprintf("send_here %d %d", fileChatID, fileMessageID)), + }, + }, + }) return markup, nil }