diff --git a/.gitignore b/.gitignore index f542f0c..19058f5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ tmp/ result .direnv/ http-client.private.env.json +files/ diff --git a/bot/bot.go b/bot/bot.go index 7a8861c..87070a1 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -25,6 +25,8 @@ import ( "github.com/Brawl345/gobot/plugin/delmsg" "github.com/Brawl345/gobot/plugin/echo" "github.com/Brawl345/gobot/plugin/expand" + "github.com/Brawl345/gobot/plugin/gemini" + "github.com/Brawl345/gobot/plugin/getfile" "github.com/Brawl345/gobot/plugin/google_images" "github.com/Brawl345/gobot/plugin/kaomoji" "github.com/Brawl345/gobot/plugin/manager" @@ -77,8 +79,8 @@ func New(db *sqlx.DB) (*Gobot, error) { afkService := sql.NewAfkService(db) birthdayService := sql.NewBirthdayService(db) cleverbotService := sql.NewCleverbotService(db) - //fileService := sql.NewFileService(db) - //geminiService := sql.NewGeminiService(db) + fileService := sql.NewFileService(db) + geminiService := sql.NewGeminiService(db) googleImagesService := sql.NewGoogleImagesService(db) googleImagesCleanupService := sql.NewGoogleImagesCleanupService(db) //homeService := sql.NewHomeService(db) @@ -104,8 +106,8 @@ func New(db *sqlx.DB) (*Gobot, error) { delmsg.New(), echo.New(), expand.New(), - //gemini.New(credentialService, geminiService), - //getfile.New(credentialService, fileService), + gemini.New(credentialService, geminiService), + getfile.New(credentialService, fileService), google_images.New(credentialService, googleImagesService, googleImagesCleanupService), //google_search.New(credentialService), //gps.New(geocodingService), diff --git a/plugin/todo/gemini/api.go b/plugin/gemini/api.go similarity index 100% rename from plugin/todo/gemini/api.go rename to plugin/gemini/api.go diff --git a/plugin/todo/gemini/gemini.go b/plugin/gemini/gemini.go similarity index 86% rename from plugin/todo/gemini/gemini.go rename to plugin/gemini/gemini.go index 1ba90e0..5eea6d6 100644 --- a/plugin/todo/gemini/gemini.go +++ b/plugin/gemini/gemini.go @@ -12,6 +12,7 @@ import ( "github.com/Brawl345/gobot/plugin" "github.com/Brawl345/gobot/utils" "github.com/Brawl345/gobot/utils/httpUtils" + "github.com/PaulSonOfLars/gotgbot/v2" "github.com/rs/xid" ) @@ -33,9 +34,9 @@ type ( } Service interface { - GetHistory(chat *telebot.Chat) (model.GeminiData, error) - ResetHistory(chat *telebot.Chat) error - SetHistory(chat *telebot.Chat, history string) error + GetHistory(chat *gotgbot.Chat) (model.GeminiData, error) + ResetHistory(chat *gotgbot.Chat) error + SetHistory(chat *gotgbot.Chat, history string) error } ) @@ -172,8 +173,8 @@ func (p *Plugin) onGemini(b *gotgbot.Bot, c plugin.GobotContext) error { Msg("error resetting Gemini data") } - return c.Reply(fmt.Sprintf("❌ Es ist ein Fehler aufgetreten, Konversation wird zurückgesetzt.%s", utils.EmbedGUID(guid)), - utils.DefaultSendOptions) + _, err = c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten, Konversation wird zurückgesetzt.%s", utils.EmbedGUID(guid)), utils.DefaultSendOptions) + return err } if httpError.StatusCode == 429 { @@ -187,8 +188,8 @@ func (p *Plugin) onGemini(b *gotgbot.Bot, c plugin.GobotContext) error { Str("guid", guid). Str("url", p.apiUrl). Msg("Failed to send POST request") - return c.Reply(fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), - utils.DefaultSendOptions) + _, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), utils.DefaultSendOptions) + return err } if len(response.Candidates) == 0 || @@ -255,9 +256,13 @@ func (p *Plugin) onGemini(b *gotgbot.Bot, c plugin.GobotContext) error { output += "\n\n(Token-Limit fast erreicht, Konversation wurde zurückgesetzt)" } - _, err = c.Bot().Reply(c.EffectiveMessage, output, &telebot.SendOptions{ - AllowWithoutReply: true, - DisableWebPagePreview: true, + _, err = c.EffectiveMessage.Reply(b, output, &gotgbot.SendMessageOpts{ + ReplyParameters: &gotgbot.ReplyParameters{ + AllowSendingWithoutReply: true, + }, + LinkPreviewOptions: &gotgbot.LinkPreviewOptions{ + IsDisabled: true, + }, }) return err @@ -272,25 +277,26 @@ func (p *Plugin) reset(b *gotgbot.Bot, c plugin.GobotContext) error { Str("guid", guid). Int64("chat_id", c.EffectiveChat.Id). Msg("error resetting history") - return c.Reply(fmt.Sprintf("❌ Fehler beim Zurücksetzen der Gemini-History.%s", utils.EmbedGUID(guid)), + _, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Fehler beim Zurücksetzen der Gemini-History.%s", utils.EmbedGUID(guid)), utils.DefaultSendOptions) + return err } return nil } func (p *Plugin) onReset(b *gotgbot.Bot, c plugin.GobotContext) error { - err := p.reset(c) + err := p.reset(b, c) if err != nil { return err } - _, err := c.EffectiveMessage.Reply(b, "✅", utils.DefaultSendOptions) + _, err = c.EffectiveMessage.Reply(b, "✅", utils.DefaultSendOptions) return err } func (p *Plugin) onResetAndRun(b *gotgbot.Bot, c plugin.GobotContext) error { - err := p.reset(c) + err := p.reset(b, c) if err != nil { return err } - return p.onGemini(c) + return p.onGemini(b, c) } diff --git a/plugin/todo/gemini/gemini.http b/plugin/gemini/gemini.http similarity index 100% rename from plugin/todo/gemini/gemini.http rename to plugin/gemini/gemini.http diff --git a/plugin/todo/getfile/getfile.go b/plugin/getfile/getfile.go similarity index 59% rename from plugin/todo/getfile/getfile.go rename to plugin/getfile/getfile.go index 1223c19..e1628e1 100644 --- a/plugin/todo/getfile/getfile.go +++ b/plugin/getfile/getfile.go @@ -11,6 +11,8 @@ import ( "github.com/Brawl345/gobot/model" "github.com/Brawl345/gobot/plugin" "github.com/Brawl345/gobot/utils" + "github.com/Brawl345/gobot/utils/httpUtils" + "github.com/PaulSonOfLars/gotgbot/v2" ) var log = logger.New("getfile") @@ -49,7 +51,7 @@ func (p *Plugin) Commands() []gotgbot.BotCommand { func (p *Plugin) Handlers(*gotgbot.User) []plugin.Handler { return []plugin.Handler{ &plugin.CommandHandler{ - Trigger: telebot.OnMedia, + Trigger: utils.AnyMedia, HandlerFunc: p.OnMedia, HandleEdits: true, }, @@ -62,16 +64,46 @@ func (p *Plugin) OnMedia(b *gotgbot.Bot, c plugin.GobotContext) error { var subFolder string var fileSize int64 - if c.EffectiveMessage.Media() != nil { - fileID = c.EffectiveMessage.Media().MediaFile().FileID - fileSize = c.EffectiveMessage.Media().MediaFile().FileSize - uniqueID = c.EffectiveMessage.Media().MediaFile().UniqueID - subFolder = c.EffectiveMessage.Media().MediaType() - } else { - fileID = c.EffectiveMessage.Sticker.FileID + if c.EffectiveMessage.Animation != nil { + fileID = c.EffectiveMessage.Animation.FileId + fileSize = c.EffectiveMessage.Animation.FileSize + uniqueID = c.EffectiveMessage.Animation.FileUniqueId + subFolder = "animation" + } else if c.EffectiveMessage.Audio != nil { + fileID = c.EffectiveMessage.Audio.FileId + fileSize = c.EffectiveMessage.Audio.FileSize + uniqueID = c.EffectiveMessage.Audio.FileUniqueId + subFolder = "audio" + } else if c.EffectiveMessage.Document != nil { + fileID = c.EffectiveMessage.Document.FileId + fileSize = c.EffectiveMessage.Document.FileSize + uniqueID = c.EffectiveMessage.Document.FileUniqueId + subFolder = "document" + } else if c.EffectiveMessage.Photo != nil { + fileID = c.EffectiveMessage.Photo[len(c.EffectiveMessage.Photo)-1].FileId + fileSize = c.EffectiveMessage.Photo[len(c.EffectiveMessage.Photo)-1].FileSize + uniqueID = c.EffectiveMessage.Photo[len(c.EffectiveMessage.Photo)-1].FileUniqueId + subFolder = "photo" + } else if c.EffectiveMessage.Sticker != nil { + fileID = c.EffectiveMessage.Sticker.FileId fileSize = c.EffectiveMessage.Sticker.FileSize - uniqueID = c.EffectiveMessage.Sticker.UniqueID - subFolder = c.EffectiveMessage.Sticker.MediaType() + uniqueID = c.EffectiveMessage.Sticker.FileUniqueId + subFolder = "sticker" + } else if c.EffectiveMessage.Video != nil { + fileID = c.EffectiveMessage.Video.FileId + fileSize = c.EffectiveMessage.Video.FileSize + uniqueID = c.EffectiveMessage.Video.FileUniqueId + subFolder = "video" + } else if c.EffectiveMessage.VideoNote != nil { + fileID = c.EffectiveMessage.VideoNote.FileId + fileSize = c.EffectiveMessage.VideoNote.FileSize + uniqueID = c.EffectiveMessage.VideoNote.FileUniqueId + subFolder = "videoNote" + } else if c.EffectiveMessage.Voice != nil { + fileID = c.EffectiveMessage.Voice.FileId + fileSize = c.EffectiveMessage.Voice.FileSize + uniqueID = c.EffectiveMessage.Voice.FileUniqueId + subFolder = "voice" } if fileSize > utils.MaxFilesizeDownload { @@ -97,9 +129,23 @@ func (p *Plugin) OnMedia(b *gotgbot.Bot, c plugin.GobotContext) error { return nil } - file := &telebot.File{FileID: fileID} - reader, err := c.Bot().File(file) + file, err := b.GetFile(fileID, nil) + if err != nil { + log.Err(err). + Str("fileID", fileID). + Str("uniqueID", uniqueID). + Str("mediaType", subFolder). + Msg("Failed to get file from Telegram") + return err + } + + reader, err := httpUtils.DownloadFileFromGetFile(b, file) if err != nil { + log.Err(err). + Str("fileID", fileID). + Str("uniqueID", uniqueID). + Str("mediaType", subFolder). + Msg("Failed to download file") return err } defer func(reader io.ReadCloser) { @@ -124,7 +170,7 @@ func (p *Plugin) OnMedia(b *gotgbot.Bot, c plugin.GobotContext) error { // Fix file endings if c.EffectiveMessage.Sticker != nil && - !c.EffectiveMessage.Sticker.Animated { + !c.EffectiveMessage.Sticker.IsAnimated { if !strings.HasSuffix(fileName, ".webp") && !strings.HasSuffix(fileName, ".webm") { fileName += ".webp" } diff --git a/utils/httpUtils/httpUtils.go b/utils/httpUtils/httpUtils.go index d1b3c23..078ef01 100644 --- a/utils/httpUtils/httpUtils.go +++ b/utils/httpUtils/httpUtils.go @@ -267,6 +267,10 @@ func DownloadFile(b *gotgbot.Bot, fileID string) (io.ReadCloser, error) { return nil, fmt.Errorf("failed to get file from Telegram: %w", err) } + return DownloadFileFromGetFile(b, file) +} + +func DownloadFileFromGetFile(b *gotgbot.Bot, file *gotgbot.File) (io.ReadCloser, error) { fileUrl := file.URL(b, nil) log.Debug(). Str("url", fileUrl).