WIP modules

This commit is contained in:
Hamcha 2017-04-19 18:10:33 +02:00
parent 523523f0b3
commit f5840cc60c
4 changed files with 151 additions and 4 deletions

24
mods/dantes.go Normal file
View File

@ -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 <NOME FAMOSO>
Scusa se faccio <roba>, ma hai detto una stronzata
*/

View File

@ -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())

47
mods/stt.go Normal file
View File

@ -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: \n<b>Durata: </b> %ds\n<b>Dimensione:</b> %d\n<b>Tipo:</b> %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, "<b>ERRORE!</b> @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, "<b>ERRORE!</b> @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, "<b>ERRORE!</b> @hamcha controlla la console!", &update.MessageID)
return
}
broker.SendTextMessage(update.Chat, fmt.Sprintf("Salvata registrazione su disco! (fname %s)", update.Voice.FileID), &update.MessageID)
})
}
}

58
mods/talk.go Normal file
View File

@ -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) {
}