From 2c4ac42019be3a92e1d53a0deb9f6abbddeaeb94 Mon Sep 17 00:00:00 2001 From: bdnugget Date: Wed, 28 May 2025 12:08:44 +0200 Subject: [PATCH] Init barebones Go Telegram bot with commands folder similar to the JS bot --- README.md | 2 +- commands/command.go | 19 +++++++++++++++++++ commands/help.go | 29 +++++++++++++++++++++++++++++ commands/info.go | 28 ++++++++++++++++++++++++++++ go.mod | 5 +++++ go.sum | 2 ++ main.go | 40 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 commands/command.go create mode 100644 commands/help.go create mode 100644 commands/info.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/README.md b/README.md index c6d4b3e..1c89776 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # nignoggobot -Rewrite of nignogbot in Go \ No newline at end of file +Rewrite of @nignogbot Telegram bot, but in Go diff --git a/commands/command.go b/commands/command.go new file mode 100644 index 0000000..f69eb29 --- /dev/null +++ b/commands/command.go @@ -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 +} diff --git a/commands/help.go b/commands/help.go new file mode 100644 index 0000000..03003e2 --- /dev/null +++ b/commands/help.go @@ -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{}) +} diff --git a/commands/info.go b/commands/info.go new file mode 100644 index 0000000..65ea030 --- /dev/null +++ b/commands/info.go @@ -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{}) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7c0b51a --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module nignoggobot + +go 1.24.3 + +require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..db8e45c --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= diff --git a/main.go b/main.go new file mode 100644 index 0000000..4b94a40 --- /dev/null +++ b/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "log" + "os" + + "nignoggobot/commands" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + + _ "nignoggobot/commands" // ensure init() is called +) + +func main() { + bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_TOKEN")) + log.Println(os.Getenv("TELEGRAM_TOKEN")) + if err != nil { + log.Fatal(err) + } + + log.Printf("Authorized on account %s", bot.Self.UserName) + + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + updates := bot.GetUpdatesChan(u) + + for update := range updates { + if update.Message == nil || !update.Message.IsCommand() { + continue + } + + cmdName := update.Message.Command() + if cmd, ok := commands.All()[cmdName]; ok { + cmd.Execute(update, bot) + } else { + msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Unknown command.") + bot.Send(msg) + } + } +}