package main import ( "fmt" "os" "strings" "git.fromouter.space/mcg/mlp-server-tools/draftbot" "git.fromouter.space/hamcha/tg" "github.com/spf13/viper" ) func checkErr(err error, msg string, args ...interface{}) { if err != nil { fmt.Printf("FATAL ERROR\n"+msg+":\n ", args...) fmt.Println(err.Error()) os.Exit(1) } } var api *tg.Telegram var botname string var tgapi *tgInterface func main() { viper.AutomaticEnv() bind := viper.GetString("bind") if bind == "" { panic("Missing BIND") } token := viper.GetString("token") if token == "" { panic("Missing TOKEN") } webhookURL := viper.GetString("webhookurl") if webhookURL == "" { panic("Missing WEBHOOKURL") } webhookPath := viper.GetString("webhookpath") if webhookURL == "" { panic("Missing WEBHOOKPATH") } botname = viper.GetString("botname") if botname == "" { panic("Missing BOTNAME") } initTemplates() api = tg.MakeAPIClient(token) api.SetWebhook(webhookURL) tgapi = &tgInterface{ client: api, usermap: make(map[string]int64), } bot := draftbot.NewDraftBot(tgapi, botname) tgapi.Bind(bot) api.HandleWebhook(bind, webhookPath, webhook) } func webhook(update tg.APIUpdate) { if update.Message != nil { msg := *update.Message if msg.Chat == nil { return } switch msg.Chat.Type { case tg.ChatTypeGroup, tg.ChatTypeSupergroup: if isCommand(msg, "/newdraft") { tgapi.StartDraft(msg.Chat, msg.User) return } if isCommand(msg, "/join") { tgapi.JoinDraft(msg.Chat, msg.User) return } if isCommand(msg, "/begindraft") { tgapi.StartDraft(msg.Chat, msg.User) return } case tg.ChatTypePrivate: //todo } } } func isCommand(update tg.APIMessage, cmdname string) bool { if update.Text == nil { return false } text := strings.TrimSpace(*(update.Text)) shortcmd := "/" + cmdname fullcmd := shortcmd + "@" + botname // Check short form if text == shortcmd || strings.HasPrefix(text, shortcmd+" ") { return true } // Check long form if text == fullcmd || strings.HasPrefix(text, fullcmd+" ") { return true } return false }