Skip to content

Commit

Permalink
Begin migration to gotgbot library
Browse files Browse the repository at this point in the history
Telebot is not maintained anymore and gotgbot is automatically updated
  • Loading branch information
Brawl345 committed Jan 21, 2024
1 parent 6c2235f commit 0c1ea28
Show file tree
Hide file tree
Showing 113 changed files with 1,356 additions and 1,312 deletions.
315 changes: 137 additions & 178 deletions bot/bot.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package bot

import (
"github.com/Brawl345/gobot/plugin/gemini"
"github.com/Brawl345/gobot/plugin/speech_to_text"
"github.com/Brawl345/gobot/plugin/summarize"
"net/http"
"os"
"sort"
"strings"
Expand All @@ -13,240 +9,203 @@ import (
"github.com/Brawl345/gobot/logger"
"github.com/Brawl345/gobot/model/sql"
"github.com/Brawl345/gobot/plugin"
"github.com/Brawl345/gobot/plugin/about"
"github.com/Brawl345/gobot/plugin/afk"
"github.com/Brawl345/gobot/plugin/alive"
"github.com/Brawl345/gobot/plugin/allow"
"github.com/Brawl345/gobot/plugin/amazon_ref_cleaner"
"github.com/Brawl345/gobot/plugin/birthdays"
"github.com/Brawl345/gobot/plugin/calc"
"github.com/Brawl345/gobot/plugin/cleverbot"
"github.com/Brawl345/gobot/plugin/covid"
"github.com/Brawl345/gobot/plugin/creds"
"github.com/Brawl345/gobot/plugin/currency"
"github.com/Brawl345/gobot/plugin/dcrypt"
"github.com/Brawl345/gobot/plugin/delmsg"
"github.com/Brawl345/gobot/plugin/echo"
"github.com/Brawl345/gobot/plugin/expand"
"github.com/Brawl345/gobot/plugin/getfile"
"github.com/Brawl345/gobot/plugin/google_images"
"github.com/Brawl345/gobot/plugin/google_search"
"github.com/Brawl345/gobot/plugin/gps"
"github.com/Brawl345/gobot/plugin/home"
"github.com/Brawl345/gobot/plugin/id"
"github.com/Brawl345/gobot/plugin/ids"
"github.com/Brawl345/gobot/plugin/kaomoji"
"github.com/Brawl345/gobot/plugin/manager"
"github.com/Brawl345/gobot/plugin/myanimelist"
"github.com/Brawl345/gobot/plugin/notify"
"github.com/Brawl345/gobot/plugin/quotes"
"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/stats"
"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"
"gopkg.in/telebot.v3"
)

var log = logger.New("bot")

type (
Gobot struct {
Telebot *telebot.Bot
GoTgBot *gotgbot.Bot
updater *ext.Updater
}
)

func New(db *sqlx.DB) (*Gobot, error) {
bot, err := telebot.NewBot(telebot.Settings{
Client: &http.Client{Timeout: time.Minute},
Token: strings.TrimSpace(os.Getenv("BOT_TOKEN")),
Poller: GetPoller(),
OnError: OnError,
})
if err != nil {
return nil, err
}

// Calling "remove webook" even if no webhook is set so pending updates can be dropped
err = bot.RemoveWebhook(true)
if err != nil {
return nil, err
}

// General services
chatService := sql.NewChatService(db)
credentialService := sql.NewCredentialService(db)
geocodingService := sql.NewGeocodingService()
//geocodingService := sql.NewGeocodingService()
pluginService := sql.NewPluginService(db)
userService := sql.NewUserService(db)
chatsPluginsService := sql.NewChatsPluginsService(db, chatService, pluginService)
chatsUsersService := sql.NewChatsUsersService(db, chatService, userService)

// Plugin-specific services
afkService := sql.NewAfkService(db)
birthdayService := sql.NewBirthdayService(db)
cleverbotService := sql.NewCleverbotService(db)
fileService := sql.NewFileService(db)
geminiService := sql.NewGeminiService(db)
googleImagesService := sql.NewGoogleImagesService(db)
googleImagesCleanupService := sql.NewGoogleImagesCleanupService(db)
homeService := sql.NewHomeService(db)
notifyService := sql.NewNotifyService(db)
quoteService := sql.NewQuoteService(db)
randomService := sql.NewRandomService(db)
reminderService := sql.NewReminderService(db)
rkiService := sql.NewRKIService(db)

allowService, err := sql.NewAllowService(chatService, userService)
if err != nil {
return nil, err
}
managerSrvce, err := NewManagerService(chatsPluginsService, pluginService)
if err != nil {
return nil, err
}

managerService, err := NewManagerService(chatsPluginsService, pluginService)
// Bot itself
bot, err := gotgbot.NewBot(strings.TrimSpace(os.Getenv("BOT_TOKEN")), nil)
if err != nil {
return nil, err
}

dispatcher := ext.NewDispatcher(&ext.DispatcherOpts{
Processor: NewProcessor(allowService, chatsUsersService, managerSrvce, userService),
})
updater := ext.NewUpdater(dispatcher, &ext.UpdaterOpts{
UnhandledErrFunc: OnError,
})

// Plugin-specific services
//afkService := sql.NewAfkService(db)
//birthdayService := sql.NewBirthdayService(db)
//cleverbotService := sql.NewCleverbotService(db)
//fileService := sql.NewFileService(db)
//geminiService := sql.NewGeminiService(db)
googleImagesService := sql.NewGoogleImagesService(db)
googleImagesCleanupService := sql.NewGoogleImagesCleanupService(db)
//homeService := sql.NewHomeService(db)
//notifyService := sql.NewNotifyService(db)
//quoteService := sql.NewQuoteService(db)
//randomService := sql.NewRandomService(db)
//reminderService := sql.NewReminderService(db)
//rkiService := sql.NewRKIService(db)

plugins := []plugin.Plugin{
about.New(),
afk.New(afkService),
alive.New(),
allow.New(allowService),
amazon_ref_cleaner.New(),
birthdays.New(bot, birthdayService),
calc.New(),
cleverbot.New(credentialService, cleverbotService),
covid.New(),
creds.New(credentialService),
currency.New(),
dcrypt.New(),
delmsg.New(),
//about.New(),
//afk.New(afkService),
//alive.New(),
//allow.New(allowService),
//amazon_ref_cleaner.New(),
//birthdays.New(bot, birthdayService),
//calc.New(),
//cleverbot.New(credentialService, cleverbotService),
//covid.New(),
//creds.New(credentialService),
//currency.New(),
//dcrypt.New(),
//delmsg.New(),
echo.New(),
expand.New(),
gemini.New(credentialService, geminiService),
getfile.New(credentialService, fileService),
//expand.New(),
//gemini.New(credentialService, geminiService),
//getfile.New(credentialService, fileService),
google_images.New(credentialService, googleImagesService, googleImagesCleanupService),
google_search.New(credentialService),
gps.New(geocodingService),
home.New(geocodingService, homeService),
id.New(),
ids.New(chatsUsersService),
//google_search.New(credentialService),
//gps.New(geocodingService),
//home.New(geocodingService, homeService),
//id.New(),
//ids.New(chatsUsersService),
kaomoji.New(),
manager.New(managerService),
myanimelist.New(credentialService),
notify.New(notifyService),
quotes.New(quoteService),
randoms.New(randomService),
reminders.New(bot, reminderService),
replace.New(),
rki.New(rkiService),
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),
manager.New(managerSrvce),
//myanimelist.New(credentialService),
//notify.New(notifyService),
//quotes.New(quoteService),
//randoms.New(randomService),
//reminders.New(bot, reminderService),
//replace.New(),
//rki.New(rkiService),
//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),
}
managerService.SetPlugins(plugins)
managerSrvce.SetPlugins(plugins)

log.Info().Msgf("Loaded %d plugins", len(plugins))

var commands []telebot.Command
var commands []gotgbot.BotCommand
for _, plg := range plugins {
commands = append(commands, plg.Commands()...)
}
sort.Slice(commands, func(i, j int) bool {
return commands[i].Text < commands[j].Text
return commands[i].Command < commands[j].Command
})
if commands != nil {
if len(commands) > 100 {
log.Warn().Msg("Too many commands, some will be ignored")
commands = commands[:100]
}
err = bot.SetCommands(commands)
_, err = bot.SetMyCommands(commands, nil)
if err != nil {
log.Err(err).Msg("Failed to set commands")
}
}

b := &Gobot{
Telebot: bot,
}

d := NewDispatcher(allowService, chatsUsersService, managerService, userService)

_, shouldPrintMsgs := os.LookupEnv("PRINT_MSGS")
if shouldPrintMsgs {
b.Telebot.Use(PrintMessage)
// TODO
//b.Telebot.Use(PrintMessage)
}

b.Telebot.Handle(telebot.OnText, d.OnText)
b.Telebot.Handle(telebot.OnEdited, d.OnText)
b.Telebot.Handle(telebot.OnMedia, d.OnText)
b.Telebot.Handle(telebot.OnContact, d.OnText)
b.Telebot.Handle(telebot.OnLocation, d.OnText)
b.Telebot.Handle(telebot.OnVenue, d.OnText)
b.Telebot.Handle(telebot.OnGame, d.OnText)
b.Telebot.Handle(telebot.OnDice, d.OnText)
b.Telebot.Handle(telebot.OnUserJoined, d.OnUserJoined)
b.Telebot.Handle(telebot.OnUserLeft, d.OnUserLeft)
b.Telebot.Handle(telebot.OnCallback, d.OnCallback)
b.Telebot.Handle(telebot.OnQuery, d.OnInlineQuery)

b.Telebot.Handle(telebot.OnPinned, d.NullRoute)
b.Telebot.Handle(telebot.OnNewGroupTitle, d.NullRoute)
b.Telebot.Handle(telebot.OnNewGroupPhoto, d.NullRoute)
b.Telebot.Handle(telebot.OnGroupPhotoDeleted, d.NullRoute)
b.Telebot.Handle(telebot.OnGroupCreated, d.NullRoute)

return b, nil
}

func GetPoller() telebot.Poller {
allowedUpdates := []string{"message", "edited_message", "callback_query", "inline_query"}

webhookPort := strings.TrimSpace(os.Getenv("PORT"))
webhookURL := strings.TrimSpace(os.Getenv("WEBHOOK_PUBLIC_URL"))

if webhookPort == "" || webhookURL == "" {
log.Debug().Msg("Using long polling")
return &telebot.LongPoller{
AllowedUpdates: allowedUpdates,
Timeout: 10 * time.Second,
}
err = updater.StartPolling(bot, &ext.PollingOpts{
DropPendingUpdates: true,
GetUpdatesOpts: &gotgbot.GetUpdatesOpts{
AllowedUpdates: []string{"message", "edited_message", "callback_query", "inline_query"},
Timeout: 10,
RequestOpts: &gotgbot.RequestOpts{
Timeout: time.Second * 15,
},
},
})
if err != nil {
return nil, err
}

log.Debug().
Str("port", webhookPort).
Str("webhook_public_url", webhookURL).
Msg("Using webhook")

webhookSecret := strings.TrimSpace(os.Getenv("WEBHOOK_SECRET"))

if webhookSecret == "" {
log.Warn().Msg("WEBHOOK_SECRET not set, it's STRONGLY RECOMMENDED to set one!")
b := &Gobot{
GoTgBot: bot,
updater: updater,
}

return &telebot.Webhook{
Listen: ":" + webhookPort,
AllowedUpdates: allowedUpdates,
SecretToken: webhookSecret,
MaxConnections: 50,
DropUpdates: true,
Endpoint: &telebot.WebhookEndpoint{
PublicURL: webhookURL,
},
}
return b, nil
}

func (b *Gobot) Start() {
b.updater.Idle()
}

// TODO: Webhook
//func GetPoller() telebot.Poller {
// allowedUpdates := []string{"message", "edited_message", "callback_query", "inline_query"}
//
// webhookPort := strings.TrimSpace(os.Getenv("PORT"))
// webhookURL := strings.TrimSpace(os.Getenv("WEBHOOK_PUBLIC_URL"))
//
// if webhookPort == "" || webhookURL == "" {
// log.Debug().Msg("Using long polling")
// return &telebot.LongPoller{
// AllowedUpdates: allowedUpdates,
// Timeout: 10 * time.Second,
// }
// }
//
// log.Debug().
// Str("port", webhookPort).
// Str("webhook_public_url", webhookURL).
// Msg("Using webhook")
//
// webhookSecret := strings.TrimSpace(os.Getenv("WEBHOOK_SECRET"))
//
// if webhookSecret == "" {
// log.Warn().Msg("WEBHOOK_SECRET not set, it's STRONGLY RECOMMENDED to set one!")
// }
//
// return &telebot.Webhook{
// Listen: ":" + webhookPort,
// AllowedUpdates: allowedUpdates,
// SecretToken: webhookSecret,
// MaxConnections: 50,
// DropUpdates: true,
// Endpoint: &telebot.WebhookEndpoint{
// PublicURL: webhookURL,
// },
// }
//}
Loading

0 comments on commit 0c1ea28

Please sign in to comment.