Skip to content

Commit

Permalink
Migrate moar plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Brawl345 committed Jan 23, 2024
1 parent 109b0ec commit 3ef6187
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ tmp/
result
.direnv/
http-client.private.env.json
files/
10 changes: 6 additions & 4 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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),
Expand Down
File renamed without changes.
36 changes: 21 additions & 15 deletions plugin/todo/gemini/gemini.go → plugin/gemini/gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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 ||
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
File renamed without changes.
72 changes: 59 additions & 13 deletions plugin/todo/getfile/getfile.go → plugin/getfile/getfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
},
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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"
}
Expand Down
4 changes: 4 additions & 0 deletions utils/httpUtils/httpUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit 3ef6187

Please sign in to comment.