2016-02-09 10:01:05 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-05-09 13:47:49 +00:00
|
|
|
"context"
|
2016-02-09 10:01:05 +00:00
|
|
|
"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
|
|
|
|
2018-04-03 10:07:06 +00:00
|
|
|
"github.com/hamcha/tg"
|
2016-02-09 10:01:05 +00:00
|
|
|
)
|
|
|
|
|
2017-02-15 15:59:49 +00:00
|
|
|
type Mod struct {
|
2018-06-18 17:26:22 +00:00
|
|
|
Description string
|
|
|
|
OnInit func()
|
|
|
|
OnMessage func(*tg.Broker, tg.APIMessage)
|
2018-10-31 15:14:12 +00:00
|
|
|
OnInline func(*tg.Broker, tg.APIInlineQuery)
|
2018-06-18 17:26:22 +00:00
|
|
|
Help func(*tg.Broker, tg.APIMessage)
|
2017-02-15 15:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var mods = map[string]Mod{
|
|
|
|
"metafora": {
|
2018-06-18 17:26:22 +00:00
|
|
|
Description: "( ͡° ͜ʖ ͡°)",
|
|
|
|
OnMessage: metafora_message,
|
2017-02-15 15:59:49 +00:00
|
|
|
},
|
|
|
|
"viaggi": {
|
2018-06-18 17:26:22 +00:00
|
|
|
Description: "Calcola itinerari",
|
|
|
|
OnInit: viaggi_init,
|
|
|
|
OnMessage: viaggi_message,
|
|
|
|
Help: viaggi_help,
|
2017-02-15 15:59:49 +00:00
|
|
|
},
|
|
|
|
"meme": {
|
2018-06-18 17:26:22 +00:00
|
|
|
Description: "Crea macro da immagini",
|
|
|
|
OnInit: meme_init,
|
|
|
|
OnMessage: meme_message,
|
2017-02-15 15:59:49 +00:00
|
|
|
},
|
|
|
|
"unsplash": {
|
2018-06-18 17:26:22 +00:00
|
|
|
Description: "Presenta messaggi in modo hipster",
|
|
|
|
OnInit: unsplash_init,
|
|
|
|
OnMessage: unsplash_message,
|
2017-02-15 15:59:49 +00:00
|
|
|
},
|
|
|
|
"macro": {
|
2018-06-18 17:26:22 +00:00
|
|
|
Description: "Salva/leggi messaggi",
|
|
|
|
OnInit: macro_init,
|
|
|
|
OnMessage: macro_message,
|
2017-02-15 15:59:49 +00:00
|
|
|
},
|
|
|
|
"snapchat": {
|
2018-06-18 17:26:22 +00:00
|
|
|
Description: "Crea immagini stile snapchat",
|
|
|
|
OnInit: snapchat_init,
|
|
|
|
OnMessage: snapchat_message,
|
2017-02-15 15:59:49 +00:00
|
|
|
},
|
2017-03-15 20:20:24 +00:00
|
|
|
"proverbio": {
|
2018-06-18 17:26:22 +00:00
|
|
|
Description: "Generatore di proverbi",
|
|
|
|
OnInit: proverbio_init,
|
|
|
|
OnMessage: proverbio_message,
|
2017-03-15 20:20:24 +00:00
|
|
|
},
|
2018-04-03 10:07:06 +00:00
|
|
|
/*
|
|
|
|
"talk": {
|
|
|
|
OnInit: inittalk,
|
|
|
|
OnMessage: talk,
|
|
|
|
},
|
|
|
|
*/
|
2017-04-19 16:10:33 +00:00
|
|
|
"stt": {
|
2018-06-18 17:26:22 +00:00
|
|
|
Description: "Trascrivi messaggi vocali",
|
|
|
|
OnInit: stt_init,
|
|
|
|
OnMessage: stt_message,
|
2017-04-19 16:10:33 +00:00
|
|
|
},
|
2017-05-04 14:54:38 +00:00
|
|
|
"remind": {
|
2018-06-18 17:26:22 +00:00
|
|
|
Description: "Reminder",
|
|
|
|
OnInit: remind_init,
|
|
|
|
OnMessage: remind_message,
|
2017-05-04 14:54:38 +00:00
|
|
|
},
|
2017-05-09 13:47:49 +00:00
|
|
|
"search": {
|
2018-06-18 17:26:22 +00:00
|
|
|
Description: "Cerca sul Knowledge graph",
|
|
|
|
OnInit: search_init,
|
|
|
|
OnMessage: search_message,
|
2017-05-09 13:47:49 +00:00
|
|
|
},
|
2017-08-07 13:19:14 +00:00
|
|
|
"oroscopo": {
|
2018-06-18 17:26:22 +00:00
|
|
|
Description: "Generatore di oroscopi",
|
|
|
|
OnInit: oroscopo_init,
|
|
|
|
OnMessage: oroscopo_message,
|
2017-08-07 13:19:14 +00:00
|
|
|
},
|
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
|
|
|
}
|
|
|
|
|
2018-10-31 15:14:12 +00:00
|
|
|
func dispatch(broker *tg.Broker, update tg.APIUpdate) {
|
2018-11-02 14:20:10 +00:00
|
|
|
if update.Inline != nil {
|
|
|
|
for _, mod := range mods {
|
|
|
|
if mod.OnInline != nil {
|
|
|
|
go mod.OnInline(broker, *update.Inline)
|
|
|
|
}
|
2018-10-31 15:14:12 +00:00
|
|
|
}
|
2018-11-02 14:20:10 +00:00
|
|
|
}
|
|
|
|
if update.Message != nil {
|
|
|
|
for _, mod := range mods {
|
|
|
|
if mod.OnMessage != nil {
|
|
|
|
go mod.OnMessage(broker, *update.Message)
|
|
|
|
}
|
2017-02-15 15:59:49 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2018-06-18 17:26:22 +00:00
|
|
|
text := strings.TrimSpace(*(update.Text))
|
|
|
|
|
|
|
|
shortcmd := "/" + cmdname
|
|
|
|
fullcmd := shortcmd + "@" + *botname
|
2018-05-24 15:48:35 +00:00
|
|
|
|
2018-06-18 17:26:22 +00:00
|
|
|
// Check short form
|
|
|
|
if text == shortcmd || strings.HasPrefix(text, shortcmd+" ") {
|
2018-05-24 15:48:35 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-06-18 17:26:22 +00:00
|
|
|
// Check long form
|
|
|
|
if text == fullcmd || strings.HasPrefix(text, fullcmd+" ") {
|
2018-05-24 15:48:35 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2016-02-09 10:46:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 15:20:49 +00:00
|
|
|
var broker *tg.Broker
|
2016-02-09 10:46:40 +00:00
|
|
|
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-05-09 11:34:52 +00:00
|
|
|
var talktoken *string
|
2017-04-20 09:47:18 +00:00
|
|
|
var gapifile *string
|
2017-05-09 13:47:49 +00:00
|
|
|
var gapikey *string
|
|
|
|
var gapiCtx context.Context
|
2017-08-07 13:19:14 +00:00
|
|
|
var oropath *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-05-04 14:54:38 +00:00
|
|
|
remindpath = flag.String("remindpath", "reminders.json", "Path to reminder db (JSON)")
|
2017-03-15 20:20:24 +00:00
|
|
|
proverbi = flag.String("proverbi", "proverbi.txt", "Path to proverbi pairs (separated by /)")
|
2017-05-09 11:37:59 +00:00
|
|
|
talktoken = flag.String("apiai", "@apiai.token", "api.ai token")
|
2017-05-09 13:47:49 +00:00
|
|
|
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)")
|
2017-08-07 13:19:14 +00:00
|
|
|
oropath = flag.String("oropath", "/astri", "Path to oroscopo corpus directory")
|
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-05-09 13:47:49 +00:00
|
|
|
gapiCtx = context.Background()
|
|
|
|
|
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())
|
|
|
|
|
2017-04-21 15:25:35 +00:00
|
|
|
initmods()
|
|
|
|
|
2017-04-21 15:20:49 +00:00
|
|
|
var err error
|
2017-05-04 14:54:38 +00:00
|
|
|
broker, err = tg.ConnectToBroker(*brokerAddr)
|
|
|
|
assert(err)
|
|
|
|
defer broker.Close()
|
|
|
|
|
|
|
|
assert(tg.RunBrokerClient(broker, dispatch))
|
2016-02-09 10:01:05 +00:00
|
|
|
}
|
2016-02-19 16:35:43 +00:00
|
|
|
|
|
|
|
func assert(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|