Skip to content

Commit

Permalink
feat: add command handlers to dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisin0 committed Jul 17, 2024
1 parent 624b00d commit 64fba3f
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions plugins/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"strings"
"unicode/utf8"

"github.com/Jisin0/TGMessageStore/config"
"github.com/Jisin0/TGMessageStore/utils/format"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers"
)

var Dispatcher *ext.Dispatcher = ext.NewDispatcher(&ext.DispatcherOpts{
Expand All @@ -26,10 +29,39 @@ const (
)

func init() {
// Dispatcher.AddHandlerToGroup(handlers.NewCommand("start", Start), commandHandlerGroup)
Dispatcher.AddHandlerToGroup(handlers.NewCommand("start", Start), commandHandlerGroup)
Dispatcher.AddHandlerToGroup(handlers.NewCommand("start", Batch), commandHandlerGroup)

// // Static Commands.
// Dispatcher.AddHandlerToGroup(handlers.NewMessage(allCommand, CommandHandler), commandHandlerGroup)
// Static Commands.
Dispatcher.AddHandlerToGroup(handlers.NewMessage(allCommand, CommandHandler), commandHandlerGroup)
}

// CommandHandler any unhandled commands
func CommandHandler(bot *gotgbot.Bot, ctx *ext.Context) error {
update := ctx.EffectiveMessage
user := ctx.EffectiveUser

cmd := strings.ToUpper(strings.Split(strings.ToLower(strings.Fields(update.GetText())[0]), "@")[0][1:])

var text string
if s, k := config.Commands[cmd]; k {
text = s
} else {
if update.Chat.Type != gotgbot.ChatTypePrivate {
return nil
}

text = config.Commands["NOTFOUND"]
}

text = format.BasicFormat(text, user)

_, err := bot.SendMessage(update.Chat.Id, text, &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML, LinkPreviewOptions: &gotgbot.LinkPreviewOptions{IsDisabled: true}, ReplyMarkup: gotgbot.InlineKeyboardMarkup{InlineKeyboard: config.Buttons[cmd]}})
if err != nil {
fmt.Println(err)
}

return nil
}

func allCommand(msg *gotgbot.Message) bool {
Expand Down

0 comments on commit 64fba3f

Please sign in to comment.