mlpdraftbot/main.go

118 lines
2.2 KiB
Go
Raw Normal View History

2019-08-22 09:02:06 +00:00
package main
import (
2019-08-22 12:58:24 +00:00
"crypto/tls"
2019-08-22 09:02:06 +00:00
"fmt"
2019-08-23 15:09:25 +00:00
"math/rand"
2019-08-22 12:58:24 +00:00
"net/http"
2019-08-22 09:02:06 +00:00
"os"
2019-08-22 12:40:48 +00:00
"strings"
2019-08-23 15:09:25 +00:00
"time"
2019-08-22 12:40:48 +00:00
2019-08-26 10:28:48 +00:00
"git.fromouter.space/mcg/draftbot"
2019-08-22 09:02:06 +00:00
"git.fromouter.space/hamcha/tg"
2019-08-22 15:23:30 +00:00
"github.com/go-kit/kit/log"
2019-08-22 09:02:06 +00:00
"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
2019-08-22 12:40:48 +00:00
var botname string
var tgapi *tgInterface
2019-08-22 15:23:30 +00:00
var logger log.Logger
2019-08-22 09:02:06 +00:00
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")
}
2019-08-22 12:40:48 +00:00
botname = viper.GetString("botname")
if botname == "" {
panic("Missing BOTNAME")
}
initTemplates()
2019-08-23 15:09:25 +00:00
// Seed RNG
rand.Seed(time.Now().UnixNano())
2019-08-22 15:23:30 +00:00
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
2019-08-22 12:58:24 +00:00
// Ignore CA errors
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
2019-08-22 09:02:06 +00:00
api = tg.MakeAPIClient(token)
api.SetWebhook(webhookURL)
2019-08-22 12:40:48 +00:00
tgapi = &tgInterface{
2019-08-23 13:39:52 +00:00
client: api,
usermap: make(map[string]*draftUser),
sessions: make(map[int64]*draftSession),
2019-08-22 12:40:48 +00:00
}
bot := draftbot.NewDraftBot(tgapi, botname)
2019-08-22 15:23:30 +00:00
bot.Logger = logger
2019-08-22 12:40:48 +00:00
tgapi.Bind(bot)
2019-08-22 15:23:30 +00:00
logger.Log("err", api.HandleWebhook(bind, webhookPath, webhook))
2019-08-22 09:02:06 +00:00
}
func webhook(update tg.APIUpdate) {
2019-08-22 12:40:48 +00:00
if update.Message != nil {
msg := *update.Message
if msg.Chat == nil {
return
}
2019-08-23 13:39:52 +00:00
tgapi.OnMessage(msg)
return
2019-08-22 12:40:48 +00:00
}
}
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
}
2019-08-22 09:02:06 +00:00
2019-08-22 12:40:48 +00:00
return false
2019-08-22 09:02:06 +00:00
}