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,38 +12,45 @@ import (
)
type Mod struct {
Description string
OnInit func()
OnMessage func(*tg.Broker, tg.APIMessage)
Help func(*tg.Broker, tg.APIMessage)
}
var mods = map[string]Mod{
"metafora": {
Description: "( ͡° ͜ʖ ͡°)",
OnMessage: metafora_message,
},
"dantes": {
OnMessage: dantes_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,
},
@ -54,18 +61,22 @@ var mods = map[string]Mod{
},
*/
"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,
},
@ -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)
// 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)