177 lines
4.1 KiB
Go
177 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"log"
|
|
"math/rand"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hamcha/clessy/tg"
|
|
)
|
|
|
|
type Mod struct {
|
|
OnInit func()
|
|
OnMessage func(*tg.Broker, tg.APIMessage)
|
|
}
|
|
|
|
var mods = map[string]Mod{
|
|
"metafora": {
|
|
OnMessage: metafora_message,
|
|
},
|
|
"dantes": {
|
|
OnMessage: dantes_message,
|
|
},
|
|
"viaggi": {
|
|
OnInit: viaggi_init,
|
|
OnMessage: viaggi_message,
|
|
},
|
|
"meme": {
|
|
OnInit: meme_init,
|
|
OnMessage: meme_message,
|
|
},
|
|
"unsplash": {
|
|
OnInit: unsplash_init,
|
|
OnMessage: unsplash_message,
|
|
},
|
|
"macro": {
|
|
OnInit: macro_init,
|
|
OnMessage: macro_message,
|
|
},
|
|
"snapchat": {
|
|
OnInit: snapchat_init,
|
|
OnMessage: snapchat_message,
|
|
},
|
|
"proverbio": {
|
|
OnInit: proverbio_init,
|
|
OnMessage: proverbio_message,
|
|
},
|
|
"talk": {
|
|
OnInit: talk_init,
|
|
OnMessage: talk_message,
|
|
},
|
|
"stt": {
|
|
OnInit: stt_init,
|
|
OnMessage: stt_message,
|
|
},
|
|
"remind": {
|
|
OnInit: remind_init,
|
|
OnMessage: remind_message,
|
|
},
|
|
"search": {
|
|
OnInit: search_init,
|
|
OnMessage: search_message,
|
|
},
|
|
"oroscopo": {
|
|
OnInit: oroscopo_init,
|
|
OnMessage: oroscopo_message,
|
|
},
|
|
}
|
|
|
|
func initmods() {
|
|
var enabledmods []string
|
|
for name, mod := range mods {
|
|
log.Printf("Initializing %s..", name)
|
|
if mod.OnInit != nil {
|
|
mod.OnInit()
|
|
}
|
|
enabledmods = append(enabledmods, name)
|
|
}
|
|
log.Println("===============================")
|
|
log.Println("All mods have been initialized!")
|
|
log.Println("===============================")
|
|
log.Println("Active modules: " + strings.Join(enabledmods, ", "))
|
|
}
|
|
|
|
func dispatch(broker *tg.Broker, update tg.APIMessage) {
|
|
for _, mod := range mods {
|
|
if mod.OnMessage != nil {
|
|
go mod.OnMessage(broker, update)
|
|
}
|
|
}
|
|
}
|
|
|
|
func isCommand(update tg.APIMessage, cmdname string) bool {
|
|
if update.Text == nil {
|
|
return false
|
|
}
|
|
|
|
text := *(update.Text)
|
|
|
|
// Check without bot suffix
|
|
prefix := "/" + cmdname
|
|
if text == prefix || strings.HasPrefix(text, prefix+" ") {
|
|
return true
|
|
}
|
|
|
|
// Check with bot suffix
|
|
prefix = "/" + cmdname + "@" + *botname
|
|
if text == prefix || strings.HasPrefix(text, prefix+" ") {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
var broker *tg.Broker
|
|
var botname *string
|
|
var impact *string
|
|
var gillmt *string
|
|
var sourcesans *string
|
|
var proverbi *string
|
|
var talktoken *string
|
|
var gapifile *string
|
|
var gapikey *string
|
|
var gapiCtx context.Context
|
|
var oropath *string
|
|
|
|
func main() {
|
|
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
|
|
botname = flag.String("botname", "maudbot", "Bot name for /targetet@commands")
|
|
impact = flag.String("impact", "impact.ttf", "Path to impact.ttf (Impact font)")
|
|
gillmt = flag.String("gillmt", "gill.ttf", "Path to gill.ttf (Gill Sans MT font)")
|
|
sourcesans = flag.String("sourcesans", "source.ttf", "Path to source.ttf (Source Sans Pro font)")
|
|
macropath = flag.String("macropath", "macros.json", "Path to macros db (JSON)")
|
|
remindpath = flag.String("remindpath", "reminders.json", "Path to reminder db (JSON)")
|
|
proverbi = flag.String("proverbi", "proverbi.txt", "Path to proverbi pairs (separated by /)")
|
|
talktoken = flag.String("apiai", "@apiai.token", "api.ai token")
|
|
gapifile = flag.String("gapifile", "gapi.json", "Google API Service Credentials file (for STT)")
|
|
gapikey = flag.String("gapikey", "@gapi.key", "Google API key (for search/KG)")
|
|
oropath = flag.String("oropath", "/astri", "Path to oroscopo corpus directory")
|
|
disable := flag.String("disable", "", "Blacklist mods (separated by comma)")
|
|
enable := flag.String("enable", "", "Whitelist mods (separated by comma)")
|
|
flag.Parse()
|
|
|
|
gapiCtx = context.Background()
|
|
|
|
if *disable != "" {
|
|
for _, modname := range strings.Split(*disable, ",") {
|
|
modname = strings.TrimSpace(modname)
|
|
delete(mods, modname)
|
|
}
|
|
} else if *enable != "" {
|
|
newmods := make(map[string]Mod)
|
|
for _, modname := range strings.Split(*enable, ",") {
|
|
newmods[modname] = mods[modname]
|
|
}
|
|
mods = newmods
|
|
}
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
initmods()
|
|
|
|
var err error
|
|
broker, err = tg.ConnectToBroker(*brokerAddr)
|
|
assert(err)
|
|
defer broker.Close()
|
|
|
|
assert(tg.RunBrokerClient(broker, dispatch))
|
|
}
|
|
|
|
func assert(err error) {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|