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 f6a5cb5 commit 4219aef
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 44 deletions.
11 changes: 7 additions & 4 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions bot/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
File renamed without changes.
File renamed without changes.
41 changes: 25 additions & 16 deletions plugin/todo/rki/rki.go → plugin/rki/rki.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

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

Expand Down Expand Up @@ -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 <code>/rki STADT</code>.",
_, err := c.EffectiveMessage.Reply(b, "❌ Gemeindeschlüssel muss kleiner als 8 Zeichen lang sein.\nSuche mit <code>/rki STADT</code>.",
utils.DefaultSendOptions())
return err
}

var response DistrictResponse
Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -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 <code>/rki STADT</code> und setze ihn mit <code>/setrki_AGS</code>.",
utils.DefaultSendOptions())
return err
}

_, err := c.EffectiveMessage.Reply(b, districtText(ags), utils.DefaultSendOptions())
_, err = c.EffectiveMessage.Reply(b, districtText(ags), utils.DefaultSendOptions())
return err
}

Expand All @@ -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
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
},
}
Expand All @@ -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).
Expand Down Expand Up @@ -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
}
File renamed without changes.
File renamed without changes.
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"

"github.com/go-shiori/go-readability"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand All @@ -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: <code>%v</code>", 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
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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 == "") {
Expand All @@ -229,6 +232,6 @@ func (p *Plugin) summarize(c plugin.GobotContext, msg *telebot.Message) error {
sb.WriteString("<b>Zusammenfassung:</b>\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
}
File renamed without changes.
1 change: 1 addition & 0 deletions utils/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4219aef

Please sign in to comment.