2016-02-09 10:01:05 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2017-02-15 15:59:49 +00:00
|
|
|
"log"
|
2017-03-15 20:19:03 +00:00
|
|
|
"math/rand"
|
2016-02-09 10:46:40 +00:00
|
|
|
"strings"
|
2017-03-15 20:19:03 +00:00
|
|
|
"time"
|
2016-02-09 10:01:05 +00:00
|
|
|
|
2016-02-09 14:55:37 +00:00
|
|
|
"github.com/hamcha/clessy/tg"
|
2016-02-09 10:01:05 +00:00
|
|
|
)
|
|
|
|
|
2017-02-15 15:59:49 +00:00
|
|
|
type Mod struct {
|
|
|
|
OnInit func()
|
|
|
|
OnMessage func(*tg.Broker, tg.APIMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
var mods = map[string]Mod{
|
|
|
|
"metafora": {
|
|
|
|
OnMessage: metafora,
|
|
|
|
},
|
|
|
|
"viaggi": {
|
|
|
|
OnInit: initviaggi,
|
|
|
|
OnMessage: viaggi,
|
|
|
|
},
|
|
|
|
"meme": {
|
|
|
|
OnInit: initmeme,
|
|
|
|
OnMessage: memegen,
|
|
|
|
},
|
|
|
|
"unsplash": {
|
|
|
|
OnInit: initunsplash,
|
|
|
|
OnMessage: unsplash,
|
|
|
|
},
|
|
|
|
"macro": {
|
|
|
|
OnInit: initmacro,
|
|
|
|
OnMessage: macro,
|
|
|
|
},
|
|
|
|
"snapchat": {
|
|
|
|
OnInit: initsnapchat,
|
|
|
|
OnMessage: snapchat,
|
|
|
|
},
|
2017-03-15 20:20:24 +00:00
|
|
|
"proverbio": {
|
|
|
|
OnInit: initproverbio,
|
|
|
|
OnMessage: proverbio,
|
|
|
|
},
|
2017-04-19 16:10:33 +00:00
|
|
|
"talk": {
|
|
|
|
OnInit: inittalk,
|
|
|
|
OnMessage: talk,
|
|
|
|
},
|
|
|
|
"stt": {
|
2017-04-20 09:47:18 +00:00
|
|
|
OnInit: initstt,
|
2017-04-19 16:10:33 +00:00
|
|
|
OnMessage: stt,
|
|
|
|
},
|
2017-02-15 15:59:49 +00:00
|
|
|
}
|
|
|
|
|
2016-02-15 15:28:41 +00:00
|
|
|
func initmods() {
|
2017-03-15 20:20:07 +00:00
|
|
|
var enabledmods []string
|
2017-02-15 15:59:49 +00:00
|
|
|
for name, mod := range mods {
|
|
|
|
log.Printf("Initializing %s..", name)
|
|
|
|
if mod.OnInit != nil {
|
|
|
|
mod.OnInit()
|
|
|
|
}
|
2017-03-15 20:20:07 +00:00
|
|
|
enabledmods = append(enabledmods, name)
|
2017-02-15 15:59:49 +00:00
|
|
|
}
|
2017-03-15 20:20:07 +00:00
|
|
|
log.Println("===============================")
|
|
|
|
log.Println("All mods have been initialized!")
|
|
|
|
log.Println("===============================")
|
|
|
|
log.Println("Active modules: " + strings.Join(enabledmods, ", "))
|
2016-02-15 15:28:41 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 10:01:05 +00:00
|
|
|
func dispatch(broker *tg.Broker, update tg.APIMessage) {
|
2017-02-15 15:59:49 +00:00
|
|
|
for _, mod := range mods {
|
|
|
|
if mod.OnMessage != nil {
|
|
|
|
mod.OnMessage(broker, update)
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 10:01:05 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 10:46:40 +00:00
|
|
|
func isCommand(update tg.APIMessage, cmdname string) bool {
|
|
|
|
if update.Text == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
text := *(update.Text)
|
|
|
|
return strings.HasPrefix(text, "/"+cmdname+"@"+*botname) || (strings.HasPrefix(text, "/"+cmdname) && !strings.Contains(text, "@"))
|
|
|
|
}
|
|
|
|
|
|
|
|
var botname *string
|
2016-02-19 16:35:43 +00:00
|
|
|
var impact *string
|
2016-06-20 15:38:00 +00:00
|
|
|
var gillmt *string
|
2017-02-15 15:59:49 +00:00
|
|
|
var sourcesans *string
|
2017-03-15 20:20:24 +00:00
|
|
|
var proverbi *string
|
2017-04-19 16:10:33 +00:00
|
|
|
var wittoken *string
|
2017-04-20 09:47:18 +00:00
|
|
|
var gapifile *string
|
2016-02-09 10:46:40 +00:00
|
|
|
|
2016-02-09 10:01:05 +00:00
|
|
|
func main() {
|
|
|
|
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
|
2016-02-09 10:46:40 +00:00
|
|
|
botname = flag.String("botname", "maudbot", "Bot name for /targetet@commands")
|
2016-02-19 16:35:43 +00:00
|
|
|
impact = flag.String("impact", "impact.ttf", "Path to impact.ttf (Impact font)")
|
2016-06-20 15:38:00 +00:00
|
|
|
gillmt = flag.String("gillmt", "gill.ttf", "Path to gill.ttf (Gill Sans MT font)")
|
2017-02-15 15:59:49 +00:00
|
|
|
sourcesans = flag.String("sourcesans", "source.ttf", "Path to source.ttf (Source Sans Pro font)")
|
2017-01-09 21:58:12 +00:00
|
|
|
macropath = flag.String("macropath", "macros.json", "Path to macros db (JSON)")
|
2017-03-15 20:20:24 +00:00
|
|
|
proverbi = flag.String("proverbi", "proverbi.txt", "Path to proverbi pairs (separated by /)")
|
2017-04-19 16:10:33 +00:00
|
|
|
wittoken = flag.String("wit", "", "Wit.ai token")
|
2017-04-20 09:47:18 +00:00
|
|
|
gapifile = flag.String("gapi", "", "Google API Service Credentials file")
|
2017-04-19 16:10:33 +00:00
|
|
|
disable := flag.String("disable", "", "Blacklist mods (separated by comma)")
|
|
|
|
enable := flag.String("enable", "", "Whitelist mods (separated by comma)")
|
2016-02-09 10:01:05 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2017-04-19 16:10:33 +00:00
|
|
|
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
|
2017-02-15 15:59:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 20:19:03 +00:00
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
|
2016-02-15 15:28:41 +00:00
|
|
|
initmods()
|
|
|
|
|
2016-02-09 10:01:05 +00:00
|
|
|
err := tg.CreateBrokerClient(*brokerAddr, dispatch)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 16:35:43 +00:00
|
|
|
|
|
|
|
func assert(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|