Init barebones Go Telegram bot with commands folder similar to the JS bot
This commit is contained in:
19
commands/command.go
Normal file
19
commands/command.go
Normal file
@ -0,0 +1,19 @@
|
||||
package commands
|
||||
|
||||
import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
|
||||
type Command interface {
|
||||
Name() string
|
||||
Help() string
|
||||
Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI)
|
||||
}
|
||||
|
||||
var commandRegistry = make(map[string]Command)
|
||||
|
||||
func Register(cmd Command) {
|
||||
commandRegistry[cmd.Name()] = cmd
|
||||
}
|
||||
|
||||
func All() map[string]Command {
|
||||
return commandRegistry
|
||||
}
|
29
commands/help.go
Normal file
29
commands/help.go
Normal file
@ -0,0 +1,29 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"strings"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
type HelpCommand struct{}
|
||||
|
||||
func (h HelpCommand) Name() string {
|
||||
return "help"
|
||||
}
|
||||
|
||||
func (h HelpCommand) Help() string {
|
||||
return "Shows available commands."
|
||||
}
|
||||
|
||||
func (h HelpCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
var helpText strings.Builder
|
||||
for _, cmd := range All() {
|
||||
helpText.WriteString("/" + cmd.Name() + " - " + cmd.Help() + "\n")
|
||||
}
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, helpText.String())
|
||||
bot.Send(msg)
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(HelpCommand{})
|
||||
}
|
28
commands/info.go
Normal file
28
commands/info.go
Normal file
@ -0,0 +1,28 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"log"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
type InfoCommand struct{}
|
||||
|
||||
func (i InfoCommand) Name() string {
|
||||
return "info"
|
||||
}
|
||||
|
||||
func (i InfoCommand) Help() string {
|
||||
return "Displays information about the bot."
|
||||
}
|
||||
|
||||
func (i InfoCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "This bot does XYZ.")
|
||||
_, err := bot.Send(msg)
|
||||
if err != nil {
|
||||
log.Println("Failed to send info message:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(InfoCommand{})
|
||||
}
|
Reference in New Issue
Block a user