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
go get github.com/llgcode/draw2d/draw2dimg go get github.com/llgcode/draw2d/draw2dimg
go get github.com/disintegration/imaging go get github.com/disintegration/imaging
go get github.com/simon-weber/gomarkov
install-tg: install-tg:
go get -u github.com/hamcha/tg go get -u github.com/hamcha/tg

View File

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

View File

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

View File

@ -57,12 +57,22 @@ func unsplash_message(broker *tg.Broker, update tg.APIMessage) {
user := update.User user := update.User
if update.ReplyTo != nil { 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) broker.SendTextMessage(update.Chat, "Non c'e' niente di 'ispiratore' in questo..", &update.MessageID)
return 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 { } else {
if strings.Index(*(update.Text), " ") > 0 { if strings.Index(*(update.Text), " ") > 0 {
text = strings.TrimSpace(strings.SplitN(*(update.Text), " ", 2)[1]) 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) { func viaggi_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "viaggi") { 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) parts := strings.SplitN(*(update.Text), " ", 2)
if len(parts) < 2 { if len(parts) < 2 {
usage() viaggi_help(broker, update)
return return
} }
text := parts[1] text := parts[1]
msgs := reg.FindStringSubmatch(text) msgs := reg.FindStringSubmatch(text)
if len(msgs) <= 2 { if len(msgs) <= 2 {
usage() viaggi_help(broker, update)
return 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 { func parseData(route Romeroute) string {
// Get time // Get time
minutes := int(route.Duration) minutes := int(route.Duration)