Skip to content

Commit

Permalink
Migrate the rest of the plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Brawl345 committed Jan 24, 2024
1 parent 4219aef commit 238ab91
Show file tree
Hide file tree
Showing 27 changed files with 197 additions and 151 deletions.
21 changes: 14 additions & 7 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ import (
"github.com/Brawl345/gobot/plugin/speech_to_text"
"github.com/Brawl345/gobot/plugin/stats"
"github.com/Brawl345/gobot/plugin/summarize"
"github.com/Brawl345/gobot/plugin/twitter"
"github.com/Brawl345/gobot/plugin/upload_by_url"
"github.com/Brawl345/gobot/plugin/urbandictionary"
"github.com/Brawl345/gobot/plugin/weather"
"github.com/Brawl345/gobot/plugin/wikipedia"
"github.com/Brawl345/gobot/plugin/worldclock"
"github.com/Brawl345/gobot/plugin/youtube"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -141,13 +148,13 @@ func New(db *sqlx.DB) (*Gobot, error) {
speech_to_text.New(credentialService),
stats.New(chatsUsersService),
summarize.New(credentialService),
//twitter.New(),
//upload_by_url.New(),
//urbandictionary.New(),
//weather.New(geocodingService, homeService),
//wikipedia.New(),
//worldclock.New(credentialService, geocodingService),
//youtube.New(credentialService),
twitter.New(),
upload_by_url.New(),
urbandictionary.New(),
weather.New(geocodingService, homeService),
wikipedia.New(),
worldclock.New(credentialService, geocodingService),
youtube.New(credentialService),
}
managerSrvce.SetPlugins(plugins)

Expand Down
1 change: 1 addition & 0 deletions plugin/dcrypt/dcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (p *Plugin) OnFile(b *gotgbot.Bot, c plugin.GobotContext) error {
Caption: "🔑 Hier sind deine entschlüsselten Links!",
ReplyParameters: &gotgbot.ReplyParameters{
AllowSendingWithoutReply: true,
MessageId: c.EffectiveMessage.MessageId,
},
DisableNotification: true,
ParseMode: gotgbot.ParseModeHTML,
Expand Down
2 changes: 2 additions & 0 deletions plugin/google_images/google_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func (p *Plugin) doImageSearch(b *gotgbot.Bot, c *plugin.GobotContext) error {
Caption: caption,
ReplyParameters: &gotgbot.ReplyParameters{
AllowSendingWithoutReply: true,
MessageId: c.EffectiveMessage.MessageId,
},
DisableNotification: true,
ParseMode: gotgbot.ParseModeHTML,
Expand All @@ -213,6 +214,7 @@ func (p *Plugin) doImageSearch(b *gotgbot.Bot, c *plugin.GobotContext) error {
Caption: caption,
ReplyParameters: &gotgbot.ReplyParameters{
AllowSendingWithoutReply: true,
MessageId: c.EffectiveMessage.MessageId,
},
DisableNotification: true,
ParseMode: gotgbot.ParseModeHTML,
Expand Down
8 changes: 4 additions & 4 deletions plugin/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (p *Plugin) onGetHome(b *gotgbot.Bot, c plugin.GobotContext) error {
Int64("user_id", c.EffectiveUser.Id).
Str("guid", guid).
Msg("error getting home")
_, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Ein Fehler ist 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
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func (p *Plugin) onHomeSet(b *gotgbot.Bot, c plugin.GobotContext) error {
Err(err).
Str("guid", guid).
Msg("error getting location")
_, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Ein Fehler ist 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
}
Expand All @@ -121,7 +121,7 @@ func (p *Plugin) onHomeSet(b *gotgbot.Bot, c plugin.GobotContext) error {
Int64("user_id", c.EffectiveUser.Id).
Str("guid", guid).
Msg("error setting home")
_, err := c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Ein Fehler ist 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
}
Expand All @@ -145,7 +145,7 @@ func (p *Plugin) onDeleteHome(b *gotgbot.Bot, c plugin.GobotContext) error {
Int64("user_id", c.EffectiveUser.Id).
Str("guid", guid).
Msg("error deleting home")
_, err = c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Ein Fehler ist 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
}
Expand Down
8 changes: 4 additions & 4 deletions plugin/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (p *Plugin) enableNotify(b *gotgbot.Bot, c plugin.GobotContext) error {
Int64("user_id", c.EffectiveUser.Id).
Str("guid", guid).
Msg("error during enabled check")
_, err = c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Ein Fehler ist aufgetreten.%s", utils.EmbedGUID(guid)),
_, err = c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)),
utils.DefaultSendOptions())
}

Expand All @@ -215,7 +215,7 @@ func (p *Plugin) enableNotify(b *gotgbot.Bot, c plugin.GobotContext) error {
Int64("user_id", c.EffectiveUser.Id).
Str("guid", guid).
Msg("error while enabling notifications")
_, err = c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Ein Fehler ist aufgetreten.%s", utils.EmbedGUID(guid)),
_, err = c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)),
utils.DefaultSendOptions())
}

Expand All @@ -233,7 +233,7 @@ func (p *Plugin) disableNotify(b *gotgbot.Bot, c plugin.GobotContext) error {
Int64("user_id", c.EffectiveUser.Id).
Str("guid", guid).
Msg("error during enabled check")
_, err = c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Ein Fehler ist aufgetreten.%s", utils.EmbedGUID(guid)),
_, err = c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Es ist ein Fehler aufgetreten.%s", utils.EmbedGUID(guid)),
utils.DefaultSendOptions())
}

Expand All @@ -250,7 +250,7 @@ func (p *Plugin) disableNotify(b *gotgbot.Bot, c plugin.GobotContext) error {
Int64("user_id", c.EffectiveUser.Id).
Str("guid", guid).
Msg("error while disabling notifications")
_, err = c.EffectiveMessage.Reply(b, fmt.Sprintf("❌ Ein Fehler ist 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
}
Expand Down
3 changes: 2 additions & 1 deletion plugin/todo/twitter/api.go → plugin/twitter/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package twitter

import (
"fmt"
"github.com/Brawl345/gobot/utils"
"strconv"
"strings"
"time"

"github.com/Brawl345/gobot/utils"
)

const (
Expand Down
119 changes: 73 additions & 46 deletions plugin/todo/twitter/twitter.go → plugin/twitter/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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 Down Expand Up @@ -191,12 +192,15 @@ func (p *Plugin) OnStatus(b *gotgbot.Bot, c plugin.GobotContext) error {

if result.Typename == "TweetUnavailable" {
if result.Reason == "NsfwLoggedOut" {
return c.Reply(fmt.Sprintf("https://vxtwitter.com/_/status/%s", tweetID), &telebot.SendOptions{
AllowWithoutReply: true,
DisableWebPagePreview: false,
DisableNotification: true,
ParseMode: telebot.ModeHTML,
})
_, err = c.EffectiveMessage.Reply(b,
fmt.Sprintf("https://vxtwitter.com/_/status/%s", tweetID),
&gotgbot.SendMessageOpts{
ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true},
DisableNotification: true,
ParseMode: gotgbot.ParseModeHTML,
},
)
return err
} else if result.Reason == "Protected" {
_, err := c.EffectiveMessage.Reply(b, "🔓 Der Account-Inhaber hat beschränkt, wer seine Tweets ansehen kann.", utils.DefaultSendOptions())
return err
Expand All @@ -211,11 +215,11 @@ func (p *Plugin) OnStatus(b *gotgbot.Bot, c plugin.GobotContext) error {
return err
}

sendOptions := &telebot.SendOptions{
AllowWithoutReply: true,
DisableWebPagePreview: true,
DisableNotification: true,
ParseMode: telebot.ModeHTML,
sendOptions := &gotgbot.SendMessageOpts{
ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true},
LinkPreviewOptions: &gotgbot.LinkPreviewOptions{IsDisabled: true},
DisableNotification: true,
ParseMode: gotgbot.ParseModeHTML,
}
var sb strings.Builder
timezone := utils.GermanTimezone()
Expand Down Expand Up @@ -410,14 +414,15 @@ func (p *Plugin) OnStatus(b *gotgbot.Bot, c plugin.GobotContext) error {
// Media
media := result.Legacy.ExtendedEntities.Media
if len(media) == 1 && (media[0].IsPhoto() || media[0].IsGIF()) { // One picture or GIF = send as preview
sendOptions.DisableWebPagePreview = false
return c.Reply(
sendOptions.LinkPreviewOptions.IsDisabled = false
_, err := c.EffectiveMessage.Reply(b,
utils.EmbedImage(media[0].Link())+sb.String(),
sendOptions,
)
return err
}

err = c.Reply(sb.String(), sendOptions)
_, err = c.EffectiveMessage.Reply(b, sb.String(), sendOptions)
if err != nil {
return err
}
Expand All @@ -434,25 +439,23 @@ func (p *Plugin) OnStatus(b *gotgbot.Bot, c plugin.GobotContext) error {

if len(media) > 0 && len(media) != len(gifs) {
// Try album (photos + videos, no GIFs) first
_ = c.Notify(telebot.UploadingPhoto)
album := make([]telebot.Inputtable, 0, len(media))
_, _ = c.EffectiveChat.SendAction(b, utils.ChatActionUploadPhoto, nil)
album := make([]gotgbot.InputMedia, 0, len(media))

for _, medium := range media {
if medium.IsPhoto() {
album = append(album, &telebot.Photo{Caption: medium.Caption(), File: telebot.FromURL(medium.Link())})
album = append(album, gotgbot.InputMediaPhoto{Caption: medium.Caption(), Media: medium.Link()})
} else if medium.IsVideo() {
album = append(album, &telebot.Video{
Caption: medium.Caption(),
File: telebot.FromURL(medium.Link()),
})
album = append(album, gotgbot.InputMediaVideo{Caption: medium.Caption(), Media: medium.Link()})
}
}

err := c.SendAlbum(album, telebot.Silent)
_, err := b.SendMediaGroup(c.EffectiveChat.Id, album, &gotgbot.SendMediaGroupOpts{DisableNotification: true,
RequestOpts: &gotgbot.RequestOpts{Timeout: 30 * time.Second}})
if err != nil {
// Group send failed - sending media manually as seperate messages
log.Err(err).Msg("Error while sending album")
msg, err := c.Bot().Reply(c.EffectiveMessage,
msg, err := c.EffectiveMessage.Reply(b,
"<i>🕒 Medien werden heruntergeladen und gesendet...</i>",
utils.DefaultSendOptions(),
)
Expand All @@ -463,17 +466,20 @@ func (p *Plugin) OnStatus(b *gotgbot.Bot, c plugin.GobotContext) error {

for _, medium := range media {
if medium.IsPhoto() {
_ = c.Notify(telebot.UploadingPhoto)
_, _ = c.EffectiveChat.SendAction(b, utils.ChatActionUploadPhoto, nil)
} else {
_ = c.Notify(telebot.UploadingVideo)
_, _ = c.EffectiveChat.SendAction(b, utils.ChatActionUploadVideo, nil)
}

func() {
resp, err := httpUtils.HttpClient.Get(medium.Link())
log.Info().Str("url", medium.Link()).Msg("Downloading")
if err != nil {
log.Err(err).Str("url", medium.Link()).Msg("Error while downloading")
err := c.Reply(medium.Caption(), telebot.Silent, telebot.AllowWithoutReply)
_, err := c.EffectiveMessage.Reply(b, medium.Caption(), &gotgbot.SendMessageOpts{
ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true},
DisableNotification: true,
})
if err != nil {
log.Err(err).Str("url", medium.Link()).Msg("Error while replying with link")
}
Expand All @@ -488,51 +494,67 @@ func (p *Plugin) OnStatus(b *gotgbot.Bot, c plugin.GobotContext) error {
}(resp.Body)

if medium.IsPhoto() {
err = c.Reply(&telebot.Photo{File: telebot.FromReader(resp.Body)},
telebot.Silent, telebot.AllowWithoutReply)
_, err = b.SendPhoto(c.EffectiveChat.Id, resp.Body, &gotgbot.SendPhotoOpts{
ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true,
MessageId: c.EffectiveMessage.MessageId},
DisableNotification: true,
})
} else {
err = c.Reply(&telebot.Video{
Caption: medium.Caption(),
File: telebot.FromReader(resp.Body),
Streaming: true,
}, telebot.Silent, telebot.AllowWithoutReply)
_, err = b.SendVideo(c.EffectiveChat.Id, resp.Body, &gotgbot.SendVideoOpts{
Caption: medium.Caption(),
ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true,
MessageId: c.EffectiveMessage.MessageId},
DisableNotification: true,
SupportsStreaming: true,
})
}
if err != nil {
// Last resort: Send URL as text
log.Err(err).Str("url", medium.Link()).Msg("Error while replying with downloaded medium")
err := c.Reply(medium.Caption(), telebot.Silent, telebot.AllowWithoutReply)
_, err := c.EffectiveMessage.Reply(b, medium.Caption(), &gotgbot.SendMessageOpts{
ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true},
DisableNotification: true,
})
if err != nil {
log.Err(err).Str("url", medium.Link()).Msg("Error while sending medium link")
}
}
}()
}

_ = c.Bot().Delete(msg)
_, _ = msg.Delete(b, nil)
}
}

// Now to GIFs...
if len(gifs) > 0 {
_ = c.Notify(telebot.UploadingVideo)
_, _ = c.EffectiveChat.SendAction(b, utils.ChatActionUploadVideo, nil)
for _, gif := range gifs {

err = c.Reply(&telebot.Animation{
Caption: gif.Caption(),
File: telebot.FromURL(gif.Link()),
}, telebot.Silent, telebot.AllowWithoutReply)
_, err := b.SendAnimation(c.EffectiveChat.Id,
gif.Link(),
&gotgbot.SendAnimationOpts{
Caption: gif.Caption(),
ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true,
MessageId: c.EffectiveMessage.MessageId},
DisableNotification: true,
},
)

if err != nil {
func() {
_ = c.Notify(telebot.UploadingVideo)
_, _ = c.EffectiveChat.SendAction(b, utils.ChatActionUploadVideo, nil)

log.Err(err).Str("url", gif.Link()).Msg("Error while sending gif through Telegram")

resp, err := httpUtils.HttpClient.Get(gif.Link())
log.Info().Str("url", gif.Link()).Msg("Downloading gif")
if err != nil {
log.Err(err).Str("url", gif.Link()).Msg("Error while downloading gif")
err := c.Reply(gif.Caption(), telebot.Silent, telebot.AllowWithoutReply)
_, err := c.EffectiveMessage.Reply(b, gif.Caption(), &gotgbot.SendMessageOpts{
ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true},
DisableNotification: true,
})
if err != nil {
log.Err(err).Str("url", gif.Link()).Msg("Error while replying with link")
}
Expand All @@ -546,15 +568,20 @@ func (p *Plugin) OnStatus(b *gotgbot.Bot, c plugin.GobotContext) error {
}
}(resp.Body)

err = c.Reply(&telebot.Animation{
_, err = b.SendAnimation(c.EffectiveChat.Id, resp.Body, &gotgbot.SendAnimationOpts{
Caption: gif.Caption(),
File: telebot.FromReader(resp.Body),
}, telebot.Silent, telebot.AllowWithoutReply)
ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true,
MessageId: c.EffectiveMessage.MessageId},
DisableNotification: true,
})

if err != nil {
// Last resort: Send URL as text
log.Err(err).Str("url", gif.Link()).Msg("Error while replying with downloaded gif")
err := c.Reply(gif.Caption(), telebot.Silent, telebot.AllowWithoutReply)
_, err := c.EffectiveMessage.Reply(b, gif.Caption(), &gotgbot.SendMessageOpts{
ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true},
DisableNotification: true,
})
if err != nil {
log.Err(err).Str("url", gif.Link()).Msg("Error while sending gif link")
}
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 238ab91

Please sign in to comment.