Mergione dopo il refactorone

This commit is contained in:
Hamcha 2018-06-18 19:26:22 +02:00
parent 8551fbbe2c
commit fee485fe6d
Signed by: hamcha
GPG Key ID: A40413D21021EAEE
5 changed files with 66 additions and 43 deletions

View File

@ -6,6 +6,7 @@ deps:
go get github.com/llgcode/draw2d
go get github.com/llgcode/draw2d/draw2dimg
go get github.com/disintegration/imaging
go get github.com/simon-weber/gomarkov
install-tg:
go get -u github.com/hamcha/tg

View File

@ -12,40 +12,47 @@ import (
)
type Mod struct {
OnInit func()
OnMessage func(*tg.Broker, tg.APIMessage)
Description string
OnInit func()
OnMessage func(*tg.Broker, tg.APIMessage)
Help func(*tg.Broker, tg.APIMessage)
}
var mods = map[string]Mod{
"metafora": {
OnMessage: metafora_message,
},
"dantes": {
OnMessage: dantes_message,
Description: "( ͡° ͜ʖ ͡°)",
OnMessage: metafora_message,
},
"viaggi": {
OnInit: viaggi_init,
OnMessage: viaggi_message,
Description: "Calcola itinerari",
OnInit: viaggi_init,
OnMessage: viaggi_message,
Help: viaggi_help,
},
"meme": {
OnInit: meme_init,
OnMessage: meme_message,
Description: "Crea macro da immagini",
OnInit: meme_init,
OnMessage: meme_message,
},
"unsplash": {
OnInit: unsplash_init,
OnMessage: unsplash_message,
Description: "Presenta messaggi in modo hipster",
OnInit: unsplash_init,
OnMessage: unsplash_message,
},
"macro": {
OnInit: macro_init,
OnMessage: macro_message,
Description: "Salva/leggi messaggi",
OnInit: macro_init,
OnMessage: macro_message,
},
"snapchat": {
OnInit: snapchat_init,
OnMessage: snapchat_message,
Description: "Crea immagini stile snapchat",
OnInit: snapchat_init,
OnMessage: snapchat_message,
},
"proverbio": {
OnInit: proverbio_init,
OnMessage: proverbio_message,
Description: "Generatore di proverbi",
OnInit: proverbio_init,
OnMessage: proverbio_message,
},
/*
"talk": {
@ -54,20 +61,24 @@ var mods = map[string]Mod{
},
*/
"stt": {
OnInit: stt_init,
OnMessage: stt_message,
Description: "Trascrivi messaggi vocali",
OnInit: stt_init,
OnMessage: stt_message,
},
"remind": {
OnInit: remind_init,
OnMessage: remind_message,
Description: "Reminder",
OnInit: remind_init,
OnMessage: remind_message,
},
"search": {
OnInit: search_init,
OnMessage: search_message,
Description: "Cerca sul Knowledge graph",
OnInit: search_init,
OnMessage: search_message,
},
"oroscopo": {
OnInit: oroscopo_init,
OnMessage: oroscopo_message,
Description: "Generatore di oroscopi",
OnInit: oroscopo_init,
OnMessage: oroscopo_message,
},
}
@ -99,17 +110,18 @@ func isCommand(update tg.APIMessage, cmdname string) bool {
return false
}
text := *(update.Text)
text := strings.TrimSpace(*(update.Text))
// Check without bot suffix
prefix := "/" + cmdname
if text == prefix || strings.HasPrefix(text, prefix+" ") {
shortcmd := "/" + cmdname
fullcmd := shortcmd + "@" + *botname
// Check short form
if text == shortcmd || strings.HasPrefix(text, shortcmd+" ") {
return true
}
// Check with bot suffix
prefix = "/" + cmdname + "@" + *botname
if text == prefix || strings.HasPrefix(text, prefix+" ") {
// Check long form
if text == fullcmd || strings.HasPrefix(text, fullcmd+" ") {
return true
}

View File

@ -9,7 +9,7 @@ import (
"io/ioutil"
"path/filepath"
"github.com/hamcha/clessy/tg"
"github.com/hamcha/tg"
gomarkov "github.com/simon-weber/gomarkov"
)

View File

@ -57,12 +57,22 @@ func unsplash_message(broker *tg.Broker, update tg.APIMessage) {
user := update.User
if update.ReplyTo != nil {
if update.ReplyTo.Text == nil {
switch {
case update.ReplyTo.Text != nil:
text = *(update.ReplyTo.Text)
case update.ReplyTo.Caption != nil:
text = *(update.ReplyTo.Caption)
default:
broker.SendTextMessage(update.Chat, "Non c'e' niente di 'ispiratore' in questo..", &update.MessageID)
return
}
text = *(update.ReplyTo.Text)
user = update.ReplyTo.User
// For forwarded message take the original user
if update.FwdUser != nil {
user = update.FwdUser.Message.User
} else {
user = update.ReplyTo.User
}
} else {
if strings.Index(*(update.Text), " ") > 0 {
text = strings.TrimSpace(strings.SplitN(*(update.Text), " ", 2)[1])

View File

@ -23,19 +23,15 @@ func viaggi_init() {
func viaggi_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "viaggi") {
usage := func() {
broker.SendTextMessage(update.Chat, "Formato: /viaggi <i>&lt;PARTENZA&gt;</i> -> <i>&lt;DESTINAZIONE&gt;</i>", &update.MessageID)
}
parts := strings.SplitN(*(update.Text), " ", 2)
if len(parts) < 2 {
usage()
viaggi_help(broker, update)
return
}
text := parts[1]
msgs := reg.FindStringSubmatch(text)
if len(msgs) <= 2 {
usage()
viaggi_help(broker, update)
return
}
@ -55,6 +51,10 @@ func viaggi_message(broker *tg.Broker, update tg.APIMessage) {
}
}
func viaggi_help(broker *tg.Broker, update tg.APIMessage) {
broker.SendTextMessage(update.Chat, "Formato: /viaggi <i>&lt;PARTENZA&gt;</i> -> <i>&lt;DESTINAZIONE&gt;</i>", &update.MessageID)
}
func parseData(route Romeroute) string {
// Get time
minutes := int(route.Duration)