From 693f15783cc7bbc44cee8f0df3ca61ed0da3e12e Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 14 Oct 2016 16:28:40 +0800 Subject: [PATCH] Support Venue Message. Signed-off-by: Bo-Yi Wu --- README.md | 3 ++- main.go | 6 ++++++ plugin.go | 27 +++++++++++++++++++++++++++ plugin_test.go | 23 +++++++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3ca7203..bc0ae65 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ * [x] Send with New Audio. * [x] Send with New Voice. * [x] Send with New Location. -* [ ] Send with New Venue. +* [x] Send with New Venue. * [x] Send with New Video. * [x] Send with New Sticker. @@ -63,6 +63,7 @@ docker run --rm \ -e PLUGIN_AUDIO=tests/audio.mp3 \ -e PLUGIN_VOICE=tests/voice.ogg \ -e PLUGIN_LOCATION=24.9163213,121.1424972 \ + -e PLUGIN_VENUE=24.9163213,121.1424972,title,address \ -e PLUGIN_VIDEO=tests/video.mp4 \ -e PLUGIN_DEBUG=true \ -e PLUGIN_FORMAT=markdown \ diff --git a/main.go b/main.go index e40d3d6..6493c56 100644 --- a/main.go +++ b/main.go @@ -62,6 +62,11 @@ func main() { Usage: "send location message", EnvVar: "PLUGIN_LOCATION", }, + cli.StringSliceFlag{ + Name: "venue", + Usage: "send venue message", + EnvVar: "PLUGIN_VENUE", + }, cli.StringSliceFlag{ Name: "video", Usage: "send video message", @@ -163,6 +168,7 @@ func run(c *cli.Context) error { Voice: c.StringSlice("voice"), Location: c.StringSlice("location"), Video: c.StringSlice("video"), + Venue: c.StringSlice("venue"), Format: c.String("format"), }, } diff --git a/plugin.go b/plugin.go index 910c7d0..2319796 100644 --- a/plugin.go +++ b/plugin.go @@ -43,6 +43,7 @@ type ( Voice []string Location []string Video []string + Venue []string Format string } @@ -55,6 +56,8 @@ type ( // Location format Location struct { + Title string + Address string Latitude float64 Longitude float64 } @@ -89,6 +92,7 @@ func fileExist(keys []string) []string { func convertLocation(value string) (Location, bool) { var latitude, longitude float64 + var title, address string var err error values := trimElement(strings.Split(value, ",")) @@ -96,6 +100,15 @@ func convertLocation(value string) (Location, bool) { return Location{}, true } + if len(values) > 2 { + title = values[2] + } + + if len(values) > 3 { + title = values[2] + address = values[3] + } + latitude, err = strconv.ParseFloat(values[0], 64) if err != nil { @@ -111,6 +124,8 @@ func convertLocation(value string) (Location, bool) { } return Location{ + Title: title, + Address: address, Latitude: latitude, Longitude: longitude, }, false @@ -167,6 +182,7 @@ func (p Plugin) Exec() error { voices := fileExist(trimElement(p.Config.Voice)) videos := fileExist(trimElement(p.Config.Video)) locations := trimElement(p.Config.Location) + venues := trimElement(p.Config.Venue) // send message. for _, user := range ids { @@ -218,6 +234,17 @@ func (p Plugin) Exec() error { msg := tgbotapi.NewLocation(user, location.Latitude, location.Longitude) p.Send(bot, msg) } + + for _, value := range venues { + location, empty := convertLocation(value) + + if empty == true { + continue + } + + msg := tgbotapi.NewVenue(user, location.Title, location.Address, location.Latitude, location.Longitude) + p.Send(bot, msg) + } } return nil diff --git a/plugin_test.go b/plugin_test.go index 4f11c03..62d5656 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -75,6 +75,7 @@ func TestSendMessage(t *testing.T) { Audio: []string{"tests/audio.mp3", "1234", " "}, Voice: []string{"tests/voice.ogg", "1234", " "}, Location: []string{"24.9163213,121.1424972", "1", " "}, + Venue: []string{"35.661777,139.704051,竹北體育館,新竹縣竹北市", "24.9163213,121.1424972", "1", " "}, Video: []string{"tests/video.mp4", "1234", " "}, Debug: false, }, @@ -188,4 +189,26 @@ func TestConvertLocation(t *testing.T) { Latitude: float64(35.661777), Longitude: float64(139.704051), }, result) + + input = "35.661777,139.704051,title" + result, empty = convertLocation(input) + + assert.Equal(t, false, empty) + assert.Equal(t, Location{ + Title: "title", + Address: "", + Latitude: float64(35.661777), + Longitude: float64(139.704051), + }, result) + + input = "35.661777,139.704051,title,address" + result, empty = convertLocation(input) + + assert.Equal(t, false, empty) + assert.Equal(t, Location{ + Title: "title", + Address: "address", + Latitude: float64(35.661777), + Longitude: float64(139.704051), + }, result) }