Init barebones Go Telegram bot with commands folder similar to the JS bot
This commit is contained in:
40
main.go
Normal file
40
main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user