78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"example.com/m/adapter/backend"
|
|
"example.com/m/bot"
|
|
"example.com/m/handlers"
|
|
"example.com/m/pkg/logger"
|
|
"example.com/m/screens"
|
|
"github.com/NicoNex/echotron/v3"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func main() {
|
|
logger.Init(logger.Config{
|
|
AppName: "tgex-bot",
|
|
AppVersion: "v0.0.1",
|
|
PrettyConsole: true,
|
|
})
|
|
|
|
botToken := mustEnv("TELEGRAM__TOKEN")
|
|
loginURL := mustEnv("LOGIN_URL")
|
|
baseURL := os.Getenv("BACKEND__BASE_URL")
|
|
|
|
backendClient := backend.New(backend.Config{
|
|
BaseURL: baseURL,
|
|
LoginURL: loginURL,
|
|
})
|
|
|
|
var commandRouter = func(command string) bot.State {
|
|
switch command {
|
|
case "/start":
|
|
return &screens.MainMenu{}
|
|
case "/start login":
|
|
return &screens.Login{}
|
|
}
|
|
panic("Unknown command: " + command)
|
|
}
|
|
|
|
newBot := func(chatID int64) echotron.Bot {
|
|
return bot.NewBot(chatID, botToken, commandRouter, backendClient, handlers.HandleGlobalCallback)
|
|
}
|
|
|
|
dsp := echotron.NewDispatcher(botToken, newBot)
|
|
|
|
log.Info().Msg("bot starting...")
|
|
|
|
updateOpts := echotron.UpdateOptions{
|
|
AllowedUpdates: []echotron.UpdateType{
|
|
echotron.MessageUpdate,
|
|
echotron.CallbackQueryUpdate,
|
|
echotron.MyChatMemberUpdate,
|
|
echotron.ChatMemberUpdate,
|
|
echotron.UpdateType("chat_join_request"),
|
|
},
|
|
}
|
|
|
|
for {
|
|
if err := dsp.PollOptions(false, updateOpts); err != nil {
|
|
log.Error().Err(err).Msg("dsp.Poll failed, retrying in 5 seconds...")
|
|
time.Sleep(5 * time.Second)
|
|
continue
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
func mustEnv(key string) string {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
log.Fatal().Str("env", key).Msg("missing required environment variable")
|
|
}
|
|
|
|
return value
|
|
}
|