diff --git a/mods/dantes.go b/mods/dantes.go new file mode 100644 index 0000000..5499627 --- /dev/null +++ b/mods/dantes.go @@ -0,0 +1,24 @@ +package main + +import "github.com/hamcha/clessy/tg" + +var nomifamosi = []string{ + "Voltaire", "Lucio Dalla", "Newton", "Cartesio", "Socrate", "Nietzsche", + "Pascoli", "Platone", +} + +var tipibrutti = []string{ + "lo scassapalle", "l'archibugio", "il minchione", "il saputello", +} + +func dantes(broker *tg.Broker, update tg.APIMessage) { + +} + +/* +MA TU SEI FUORI + +ATTENZIONE RAGAZZI, E' ARRIVATO + +Scusa se faccio , ma hai detto una stronzata +*/ diff --git a/mods/main.go b/mods/main.go index a5abe12..f84ee2b 100644 --- a/mods/main.go +++ b/mods/main.go @@ -43,6 +43,13 @@ var mods = map[string]Mod{ OnInit: initproverbio, OnMessage: proverbio, }, + "talk": { + OnInit: inittalk, + OnMessage: talk, + }, + "stt": { + OnMessage: stt, + }, } func initmods() { @@ -82,6 +89,7 @@ var impact *string var gillmt *string var sourcesans *string var proverbi *string +var wittoken *string func main() { brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port") @@ -91,12 +99,22 @@ func main() { 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)") proverbi = flag.String("proverbi", "proverbi.txt", "Path to proverbi pairs (separated by /)") - disable := flag.String("disable", "", "Disable mods (separated by comma)") + wittoken = flag.String("wit", "", "Wit.ai token") + disable := flag.String("disable", "", "Blacklist mods (separated by comma)") + enable := flag.String("enable", "", "Whitelist mods (separated by comma)") flag.Parse() - for _, modname := range strings.Split(*disable, ",") { - modname = strings.TrimSpace(modname) - delete(mods, modname) + 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()) diff --git a/mods/stt.go b/mods/stt.go new file mode 100644 index 0000000..4848593 --- /dev/null +++ b/mods/stt.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "log" + + "encoding/base64" + + "io/ioutil" + + "github.com/hamcha/clessy/tg" +) + +func stt(broker *tg.Broker, update tg.APIMessage) { + if isCommand(update, "stt") { + // Make replies work + if update.ReplyTo != nil && update.ReplyTo.Voice != nil { + update.Voice = update.ReplyTo.Voice + } + + broker.SendTextMessage(update.Chat, fmt.Sprintf("Ok! Eccoti intanto delle informazioni sul file: \nDurata: %ds\nDimensione: %d\nTipo: %s", update.Voice.Duration, update.Voice.FileSize, *update.Voice.MimeType), &update.MessageID) + + broker.GetFile(update.Voice.FileID, func(broker *tg.Broker, data tg.BrokerUpdate) { + if data.Type == tg.BError { + log.Printf("[stt] Received error from broker: %s\n", *data.Error) + broker.SendTextMessage(update.Chat, "ERRORE! @hamcha controlla la console!", &update.MessageID) + return + } + + bytes, err := base64.StdEncoding.DecodeString(*data.Bytes) + if err != nil { + log.Printf("[stt] Base64 decode error: %s\n", err.Error()) + broker.SendTextMessage(update.Chat, "ERRORE! @hamcha controlla la console!", &update.MessageID) + return + } + + err = ioutil.WriteFile(update.Voice.FileID, bytes, 0755) + if err != nil { + log.Printf("[stt] Save to file error: %s\n", err.Error()) + broker.SendTextMessage(update.Chat, "ERRORE! @hamcha controlla la console!", &update.MessageID) + return + } + + broker.SendTextMessage(update.Chat, fmt.Sprintf("Salvata registrazione su disco! (fname %s)", update.Voice.FileID), &update.MessageID) + }) + } +} diff --git a/mods/talk.go b/mods/talk.go new file mode 100644 index 0000000..0dccee5 --- /dev/null +++ b/mods/talk.go @@ -0,0 +1,58 @@ +package main + +import ( + "errors" + "fmt" + + "github.com/hamcha/clessy/tg" + "github.com/kurrik/witgo/v1/witgo" +) + +type ClessyHandler struct { +} + +var wit *witgo.Witgo +var witHandler ClessyHandler + +func inittalk() { + // Disable if token is not provided + if *wittoken == "" { + panic(errors.New("No or empty wit token provided (-wit)")) + } + + witgo.NewWitgo(witgo.NewClient(*wittoken), &witHandler) +} + +func talk(broker *tg.Broker, update tg.APIMessage) { +} + +func (h *ClessyHandler) Action(session *witgo.Session, action string) (response *witgo.Session, err error) { + response = session + response.Context.Set("forecast", "sunny") + return +} + +func (h *ClessyHandler) Say(session *witgo.Session, msg string) (response *witgo.Session, err error) { + response = session + fmt.Printf("< %v\n", msg) + return +} + +func (h *ClessyHandler) Merge(session *witgo.Session, entities witgo.EntityMap) (response *witgo.Session, err error) { + var ( + value string + ) + response = session + if value, err = entities.FirstEntityValue("location"); err != nil { + response.Context = witgo.Context{} + err = nil + } else { + response.Context.Merge(witgo.Context{ + "loc": value, + }) + } + return +} + +func (h *ClessyHandler) Error(session *witgo.Session, msg string) { +}