From fee485fe6de5abb40b9bfd18283c8fb629cbb902 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Mon, 18 Jun 2018 19:26:22 +0200 Subject: [PATCH] Mergione dopo il refactorone --- Makefile | 1 + mods/main.go | 78 ++++++++++++++++++++++++++++-------------------- mods/oroscopo.go | 2 +- mods/unsplash.go | 16 ++++++++-- mods/viaggi.go | 12 ++++---- 5 files changed, 66 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index d04d811..5fd86db 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/mods/main.go b/mods/main.go index 64b0b4e..9cce2c3 100644 --- a/mods/main.go +++ b/mods/main.go @@ -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 } diff --git a/mods/oroscopo.go b/mods/oroscopo.go index 2918585..b7e08d1 100644 --- a/mods/oroscopo.go +++ b/mods/oroscopo.go @@ -9,7 +9,7 @@ import ( "io/ioutil" "path/filepath" - "github.com/hamcha/clessy/tg" + "github.com/hamcha/tg" gomarkov "github.com/simon-weber/gomarkov" ) diff --git a/mods/unsplash.go b/mods/unsplash.go index 1ff84f2..04f3684 100644 --- a/mods/unsplash.go +++ b/mods/unsplash.go @@ -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]) diff --git a/mods/viaggi.go b/mods/viaggi.go index ff358c0..d6bc4c6 100644 --- a/mods/viaggi.go +++ b/mods/viaggi.go @@ -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 <PARTENZA> -> <DESTINAZIONE>", &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 <PARTENZA> -> <DESTINAZIONE>", &update.MessageID) +} + func parseData(route Romeroute) string { // Get time minutes := int(route.Duration)