package main import ( "context" "flag" "log" "math/rand" "strings" "time" "git.fromouter.space/crunchy-rocks/emoji" "git.fromouter.space/hamcha/tg" ) type Mod struct { Description string OnInit func() OnMessage func(*tg.Broker, tg.APIMessage) OnInline func(*tg.Broker, tg.APIInlineQuery) Help func(*tg.Broker, tg.APIMessage) } var mods = map[string]Mod{ "metafora": { Description: "( ͡° ͜ʖ ͡°)", OnMessage: metafora_message, }, "viaggi": { Description: "Calcola itinerari", OnInit: viaggi_init, OnMessage: viaggi_message, Help: viaggi_help, }, "meme": { Description: "Crea macro da immagini", OnInit: meme_init, OnMessage: meme_message, }, "unsplash": { Description: "Presenta messaggi in modo hipster", OnInit: unsplash_init, OnMessage: unsplash_message, }, "macro": { Description: "Salva/leggi messaggi", OnInit: macro_init, OnMessage: macro_message, }, "snapchat": { Description: "Crea immagini stile snapchat", OnInit: snapchat_init, OnMessage: snapchat_message, }, "proverbio": { Description: "Generatore di proverbi", OnInit: proverbio_init, OnMessage: proverbio_message, }, /* "talk": { OnInit: inittalk, OnMessage: talk, }, */ "stt": { Description: "Trascrivi messaggi vocali", OnInit: stt_init, OnMessage: stt_message, }, "remind": { Description: "Reminder", OnInit: remind_init, OnMessage: remind_message, }, "search": { Description: "Cerca sul Knowledge graph", OnInit: search_init, OnMessage: search_message, }, "oroscopo": { Description: "Generatore di oroscopi", OnInit: oroscopo_init, OnMessage: oroscopo_message, }, // Config operations "tz": { Description: "Configura timezone personalizzate", OnInit: tz_init, OnMessage: tz_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.APIUpdate) { if update.Inline != nil { for _, mod := range mods { if mod.OnInline != nil { go mod.OnInline(broker, *update.Inline) } } } if update.Message != nil { for _, mod := range mods { if mod.OnMessage != nil { go mod.OnMessage(broker, *update.Message) } } } } 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 } 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 var emojis emoji.Table 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)") remindpath = flag.String("tzpath", "timezones.json", "Path to timezones 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)") emojipath := flag.String("emojis", "noto_emojis", "Path to emoji directory") flag.Parse() gapiCtx = context.Background() log.Println("[x-emoji] Loading emojis...") var err error emojis, err = emoji.ScanEmojiDirectory(*emojipath) if err != nil { log.Printf("[x-emoji] Error while loading emojis: %s\n", err.Error()) log.Println("[x-emoji] Emoji support will be disabled") } else { log.Printf("[x-emoji] Loaded %d emojis\n", len(emojis)) } 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() broker, err = tg.ConnectToBroker(*brokerAddr) assert(err) defer broker.Close() assert(tg.RunBrokerClient(broker, dispatch)) } func assert(err error) { if err != nil { panic(err) } }