Telegram bot for drafting MLP:CCG (@mlpdraftbot)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

117 lines
2.2 KiB

package main
import (
"crypto/tls"
"fmt"
"math/rand"
"net/http"
"os"
"strings"
"time"
"git.fromouter.space/mcg/draftbot"
"git.fromouter.space/hamcha/tg"
"github.com/go-kit/kit/log"
"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
var logger log.Logger
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()
// Seed RNG
rand.Seed(time.Now().UnixNano())
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
// Ignore CA errors
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
api = tg.MakeAPIClient(token)
api.SetWebhook(webhookURL)
tgapi = &tgInterface{
client: api,
usermap: make(map[string]*draftUser),
sessions: make(map[int64]*draftSession),
}
bot := draftbot.NewDraftBot(tgapi, botname)
bot.Logger = logger
tgapi.Bind(bot)
logger.Log("err", api.HandleWebhook(bind, webhookPath, webhook))
}
func webhook(update tg.APIUpdate) {
if update.Message != nil {
msg := *update.Message
if msg.Chat == nil {
return
}
tgapi.OnMessage(msg)
return
}
}
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
}