From 4219aef4120e65e8e99c0b91ae3d9456ed8fead9 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Tue, 23 Jan 2024 21:56:44 +0100 Subject: [PATCH] Migrate moar plugins --- bot/bot.go | 11 +++-- bot/processor.go | 2 + plugin/{todo => }/rki/api.go | 0 plugin/{todo => }/rki/jwd.go | 0 plugin/{todo => }/rki/rki.go | 41 +++++++++++------- plugin/{todo => }/rki/rki.http | 0 plugin/{todo => }/speech_to_text/api.go | 0 .../speech_to_text/example-voice.ogg | Bin .../speech_to_text/speech_to_text.go | 16 ++++--- .../speech_to_text/speech_to_text.http | 0 plugin/{todo => }/summarize/api.go | 0 plugin/{todo => }/summarize/summarize.go | 37 ++++++++-------- plugin/{todo => }/summarize/summarize.http | 0 utils/const.go | 1 + 14 files changed, 64 insertions(+), 44 deletions(-) rename plugin/{todo => }/rki/api.go (100%) rename plugin/{todo => }/rki/jwd.go (100%) rename plugin/{todo => }/rki/rki.go (85%) rename plugin/{todo => }/rki/rki.http (100%) rename plugin/{todo => }/speech_to_text/api.go (100%) rename plugin/{todo => }/speech_to_text/example-voice.ogg (100%) rename plugin/{todo => }/speech_to_text/speech_to_text.go (88%) rename plugin/{todo => }/speech_to_text/speech_to_text.http (100%) rename plugin/{todo => }/summarize/api.go (100%) rename plugin/{todo => }/summarize/summarize.go (82%) rename plugin/{todo => }/summarize/summarize.http (100%) diff --git a/bot/bot.go b/bot/bot.go index 65a4916..47ef6ae 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -41,7 +41,10 @@ import ( "github.com/Brawl345/gobot/plugin/randoms" "github.com/Brawl345/gobot/plugin/reminders" "github.com/Brawl345/gobot/plugin/replace" + "github.com/Brawl345/gobot/plugin/rki" + "github.com/Brawl345/gobot/plugin/speech_to_text" "github.com/Brawl345/gobot/plugin/stats" + "github.com/Brawl345/gobot/plugin/summarize" "github.com/PaulSonOfLars/gotgbot/v2" "github.com/PaulSonOfLars/gotgbot/v2/ext" "github.com/jmoiron/sqlx" @@ -100,7 +103,7 @@ func New(db *sqlx.DB) (*Gobot, error) { quoteService := sql.NewQuoteService(db) randomService := sql.NewRandomService(db) reminderService := sql.NewReminderService(db) - //rkiService := sql.NewRKIService(db) + rkiService := sql.NewRKIService(db) plugins := []plugin.Plugin{ about.New(), @@ -134,10 +137,10 @@ func New(db *sqlx.DB) (*Gobot, error) { randoms.New(randomService), reminders.New(bot, reminderService), replace.New(), - //rki.New(rkiService), - //speech_to_text.New(credentialService), + rki.New(rkiService), + speech_to_text.New(credentialService), stats.New(chatsUsersService), - //summarize.New(credentialService), + summarize.New(credentialService), //twitter.New(), //upload_by_url.New(), //urbandictionary.New(), diff --git a/bot/processor.go b/bot/processor.go index fc14b54..711e17c 100644 --- a/bot/processor.go +++ b/bot/processor.go @@ -143,6 +143,8 @@ func (p *Processor) onMessage(b *gotgbot.Bot, ctx *ext.Context) error { matched = command == utils.LocationMsg case msg.Venue != nil: matched = command == utils.VenueMsg + case msg.Voice != nil: + matched = command == utils.VoiceMsg } if !matched && utils.ContainsMedia(msg) { diff --git a/plugin/todo/rki/api.go b/plugin/rki/api.go similarity index 100% rename from plugin/todo/rki/api.go rename to plugin/rki/api.go diff --git a/plugin/todo/rki/jwd.go b/plugin/rki/jwd.go similarity index 100% rename from plugin/todo/rki/jwd.go rename to plugin/rki/jwd.go diff --git a/plugin/todo/rki/rki.go b/plugin/rki/rki.go similarity index 85% rename from plugin/todo/rki/rki.go rename to plugin/rki/rki.go index e715370..32fe966 100644 --- a/plugin/todo/rki/rki.go +++ b/plugin/rki/rki.go @@ -9,6 +9,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" ) @@ -22,9 +23,9 @@ type ( } Service interface { - DelAGS(user *telebot.User) error - GetAGS(user *telebot.User) (string, error) - SetAGS(user *telebot.User, ags string) error + DelAGS(user *gotgbot.User) error + GetAGS(user *gotgbot.User) (string, error) + SetAGS(user *gotgbot.User, ags string) error } ) @@ -97,8 +98,9 @@ func onNational(b *gotgbot.Bot, c plugin.GobotContext) error { Str("guid", guid). Str("url", url). Msg("error getting RKI data") - return c.Reply(fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), + _, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), utils.DefaultSendOptions()) + return err } var sb strings.Builder @@ -178,7 +180,7 @@ func onNational(b *gotgbot.Bot, c plugin.GobotContext) error { ), ) - _, err := c.EffectiveMessage.Reply(b, sb.String(), utils.DefaultSendOptions()) + _, err = c.EffectiveMessage.Reply(b, sb.String(), utils.DefaultSendOptions()) return err } @@ -199,8 +201,9 @@ func onDistrictSearch(b *gotgbot.Bot, c plugin.GobotContext) error { Str("guid", guid). Str("url", url). Msg("error getting RKI data") - return c.Reply(fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), + _, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), utils.DefaultSendOptions()) + return err } var foundDistricts []District @@ -231,7 +234,7 @@ func onDistrictSearch(b *gotgbot.Bot, c plugin.GobotContext) error { ) } - _, err := c.EffectiveMessage.Reply(b, sb.String(), utils.DefaultSendOptions()) + _, err = c.EffectiveMessage.Reply(b, sb.String(), utils.DefaultSendOptions()) return err } @@ -340,8 +343,9 @@ func (p *Plugin) setRkiAGS(b *gotgbot.Bot, c plugin.GobotContext) error { ags := c.Matches[1] if len(ags) > 8 { - return c.Reply("❌ Gemeindeschlüssel muss kleiner als 8 Zeichen lang sein.\nSuche mit /rki STADT.", + _, err := c.EffectiveMessage.Reply(b, "❌ Gemeindeschlüssel muss kleiner als 8 Zeichen lang sein.\nSuche mit /rki STADT.", utils.DefaultSendOptions()) + return err } var response DistrictResponse @@ -358,8 +362,9 @@ func (p *Plugin) setRkiAGS(b *gotgbot.Bot, c plugin.GobotContext) error { Str("guid", guid). Str("url", url). Msg("error getting RKI data") - return c.Reply(fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), + _, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), utils.DefaultSendOptions()) + return err } if len(response.Districts) == 0 { @@ -376,11 +381,12 @@ func (p *Plugin) setRkiAGS(b *gotgbot.Bot, c plugin.GobotContext) error { Int64("user_id", c.EffectiveUser.Id). Str("ags", ags). Msg("error while saving AGS") - return c.Reply(fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(xid.New().String())), + _, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), utils.DefaultSendOptions()) + return err } - _, err := c.EffectiveMessage.Reply(b, "✅ Du kannst jetzt /myrki nutzen.", utils.DefaultSendOptions()) + _, err = c.EffectiveMessage.Reply(b, "✅ Du kannst jetzt /myrki nutzen.", utils.DefaultSendOptions()) return err } @@ -394,17 +400,19 @@ func (p *Plugin) onMyRKI(b *gotgbot.Bot, c plugin.GobotContext) error { Str("guid", guid). Int64("user_id", c.EffectiveUser.Id). Msg("error while getting AGS") - return c.Reply(fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), + _, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), utils.DefaultSendOptions()) + return err } if ags == "" { - return c.Reply("❌ Du hast keinen Gemeindeschlüssel gespeichert."+ + _, err := c.EffectiveMessage.Reply(b, "❌ Du hast keinen Gemeindeschlüssel gespeichert."+ "\nSuche mit /rki STADT und setze ihn mit /setrki_AGS.", utils.DefaultSendOptions()) + return err } - _, err := c.EffectiveMessage.Reply(b, districtText(ags), utils.DefaultSendOptions()) + _, err = c.EffectiveMessage.Reply(b, districtText(ags), utils.DefaultSendOptions()) return err } @@ -417,10 +425,11 @@ func (p *Plugin) delRKI(b *gotgbot.Bot, c plugin.GobotContext) error { Str("guid", guid). Int64("user_id", c.EffectiveUser.Id). Msg("error while deleting AGS") - return c.Reply(fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), + _, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), utils.DefaultSendOptions()) + return err } - _, err := c.EffectiveMessage.Reply(b, "✅", utils.DefaultSendOptions()) + _, err = c.EffectiveMessage.Reply(b, "✅", utils.DefaultSendOptions()) return err } diff --git a/plugin/todo/rki/rki.http b/plugin/rki/rki.http similarity index 100% rename from plugin/todo/rki/rki.http rename to plugin/rki/rki.http diff --git a/plugin/todo/speech_to_text/api.go b/plugin/speech_to_text/api.go similarity index 100% rename from plugin/todo/speech_to_text/api.go rename to plugin/speech_to_text/api.go diff --git a/plugin/todo/speech_to_text/example-voice.ogg b/plugin/speech_to_text/example-voice.ogg similarity index 100% rename from plugin/todo/speech_to_text/example-voice.ogg rename to plugin/speech_to_text/example-voice.ogg diff --git a/plugin/todo/speech_to_text/speech_to_text.go b/plugin/speech_to_text/speech_to_text.go similarity index 88% rename from plugin/todo/speech_to_text/speech_to_text.go rename to plugin/speech_to_text/speech_to_text.go index e736425..9d3e8a1 100644 --- a/plugin/todo/speech_to_text/speech_to_text.go +++ b/plugin/speech_to_text/speech_to_text.go @@ -11,6 +11,7 @@ import ( "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("speech_to_text") @@ -49,7 +50,7 @@ func (p *Plugin) Commands() []gotgbot.BotCommand { func (p *Plugin) Handlers(*gotgbot.User) []plugin.Handler { return []plugin.Handler{ &plugin.CommandHandler{ - Trigger: telebot.OnVoice, + Trigger: utils.VoiceMsg, HandlerFunc: p.OnVoice, }, } @@ -72,12 +73,12 @@ func (p *Plugin) OnVoice(b *gotgbot.Bot, c plugin.GobotContext) error { if c.EffectiveMessage.Voice.Duration > MaxDuration { log.Warn(). - Int("duration", c.EffectiveMessage.Voice.Duration). + Int64("duration", c.EffectiveMessage.Voice.Duration). Msg(fmt.Sprintf("Voice message is longer than %d seconds", MaxDuration)) return nil } - file, err := c.Bot().File(&telebot.File{FileID: c.EffectiveMessage.Voice.FileID}) + file, err := httpUtils.DownloadFile(b, c.EffectiveMessage.Voice.FileId) if err != nil { log.Err(err). Interface("file", c.EffectiveMessage.Voice). @@ -169,9 +170,10 @@ func (p *Plugin) OnVoice(b *gotgbot.Bot, c plugin.GobotContext) error { sb.WriteString(apiResponse.Text) } - return c.Reply(sb.String(), &telebot.SendOptions{ - AllowWithoutReply: true, - DisableWebPagePreview: true, - DisableNotification: true, + _, err = c.EffectiveMessage.Reply(b, sb.String(), &gotgbot.SendMessageOpts{ + ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true}, + LinkPreviewOptions: &gotgbot.LinkPreviewOptions{IsDisabled: true}, + DisableNotification: true, }) + return err } diff --git a/plugin/todo/speech_to_text/speech_to_text.http b/plugin/speech_to_text/speech_to_text.http similarity index 100% rename from plugin/todo/speech_to_text/speech_to_text.http rename to plugin/speech_to_text/speech_to_text.http diff --git a/plugin/todo/summarize/api.go b/plugin/summarize/api.go similarity index 100% rename from plugin/todo/summarize/api.go rename to plugin/summarize/api.go diff --git a/plugin/todo/summarize/summarize.go b/plugin/summarize/summarize.go similarity index 82% rename from plugin/todo/summarize/summarize.go rename to plugin/summarize/summarize.go index 119cec6..1e455ea 100644 --- a/plugin/todo/summarize/summarize.go +++ b/plugin/summarize/summarize.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" "github.com/go-shiori/go-readability" @@ -91,7 +92,7 @@ func (p *Plugin) Handlers(botInfo *gotgbot.User) []plugin.Handler { } func (p *Plugin) onSummarize(b *gotgbot.Bot, c plugin.GobotContext) error { - return p.summarize(c, c.EffectiveMessage) + return p.summarize(b, c, c.EffectiveMessage) } func (p *Plugin) onReply(b *gotgbot.Bot, c plugin.GobotContext) error { @@ -103,34 +104,34 @@ func (p *Plugin) onReply(b *gotgbot.Bot, c plugin.GobotContext) error { return nil } - if strings.HasPrefix(c.EffectiveMessage.ReplyTo.Text, "/su") || - strings.HasPrefix(c.EffectiveMessage.ReplyTo.Caption, "/su") { + if strings.HasPrefix(c.EffectiveMessage.ReplyToMessage.Text, "/su") || + strings.HasPrefix(c.EffectiveMessage.ReplyToMessage.Caption, "/su") { _, err := c.EffectiveMessage.Reply(b, "😠", utils.DefaultSendOptions()) return err } - if c.EffectiveMessage.ReplyTo.Sender.IsBot { + if c.EffectiveMessage.ReplyToMessage.From.IsBot { _, err := c.EffectiveMessage.Reply(b, "😠", utils.DefaultSendOptions()) return err } - return p.summarize(c, c.EffectiveMessage.ReplyTo) + return p.summarize(b, c, c.EffectiveMessage.ReplyToMessage) } -func (p *Plugin) summarize(c plugin.GobotContext, msg *telebot.Message) error { +func (p *Plugin) summarize(b *gotgbot.Bot, c plugin.GobotContext, msg *gotgbot.Message) error { _, _ = c.EffectiveChat.SendAction(b, utils.ChatActionTyping, nil) var urls []string for _, entity := range utils.AnyEntities(msg) { - if entity.Type == telebot.EntityURL { - urls = append(urls, msg.EntityText(entity)) - } else if entity.Type == telebot.EntityTextLink { - urls = append(urls, entity.URL) + if utils.EntityType(entity.Type) == utils.EntityTypeURL { + urls = append(urls, msg.ParseEntity(entity).Url) + } else if utils.EntityType(entity.Type) == utils.EntityTextLink { + urls = append(urls, entity.Url) } } if len(urls) == 0 { - _, err := c.Bot().Reply(msg, "❌ Keine Links gefunden", utils.DefaultSendOptions()) + _, err := msg.Reply(b, "❌ Keine Links gefunden", utils.DefaultSendOptions()) return err } @@ -142,21 +143,21 @@ func (p *Plugin) summarize(c plugin.GobotContext, msg *telebot.Message) error { Str("url", url). Msg("Failed to extract text content from URL") - _, err := c.Bot().Reply(msg, + _, err := msg.Reply(b, fmt.Sprintf("❌ Text konnte nicht extrahiert werden: %v", utils.Escape(err.Error())), utils.DefaultSendOptions()) return err } if len(article.TextContent) < MinArticleLength { - _, err := c.Bot().Reply(msg, + _, err := msg.Reply(b, "❌ Artikel-Inhalt ist zu kurz.", utils.DefaultSendOptions()) return err } if len(article.TextContent) > MaxArticleLength { - _, err := c.Bot().Reply(msg, + _, err := msg.Reply(b, "❌ Artikel-Inhalt ist zu lang.", utils.DefaultSendOptions()) return err @@ -201,8 +202,9 @@ func (p *Plugin) summarize(c plugin.GobotContext, msg *telebot.Message) error { Str("guid", guid). Str("url", url). Msg("Failed to send POST request") - return c.Reply(fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), + _, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), utils.DefaultSendOptions()) + return err } if response.Error.Type != "" { @@ -213,8 +215,9 @@ func (p *Plugin) summarize(c plugin.GobotContext, msg *telebot.Message) error { Str("message", response.Error.Message). Str("type", response.Error.Type). Msg("Got error from OpenAI API") - return c.Reply(fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), + _, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)), utils.DefaultSendOptions()) + return err } if len(response.Choices) == 0 || (len(response.Choices) > 0 && response.Choices[0].Message.Content == "") { @@ -229,6 +232,6 @@ func (p *Plugin) summarize(c plugin.GobotContext, msg *telebot.Message) error { sb.WriteString("Zusammenfassung:\n") sb.WriteString(utils.Escape(response.Choices[0].Message.Content)) - _, err = c.Bot().Reply(msg, sb.String(), utils.DefaultSendOptions()) + _, err = msg.Reply(b, sb.String(), utils.DefaultSendOptions()) return err } diff --git a/plugin/todo/summarize/summarize.http b/plugin/summarize/summarize.http similarity index 100% rename from plugin/todo/summarize/summarize.http rename to plugin/summarize/summarize.http diff --git a/utils/const.go b/utils/const.go index fe9f66b..55300da 100644 --- a/utils/const.go +++ b/utils/const.go @@ -13,6 +13,7 @@ const ( LocationMsg MessageTrigger = "\agobot_location" PhotoMsg MessageTrigger = "\agobot_photo" VenueMsg MessageTrigger = "\agobot_venue" + VoiceMsg MessageTrigger = "\agobot_voice" AnyMedia MessageTrigger = "\agobot_media" Day = 24 * time.Hour