From 3f25341c8b8d2d33cbe0d02e55f114e7e50705e5 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 14 Oct 2016 14:27:34 +0800 Subject: [PATCH] Support Location Message. Signed-off-by: Bo-Yi Wu --- README.md | 4 ++-- main.go | 6 ++++++ plugin.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ plugin_test.go | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 637ba38..e94454b 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,7 @@ * [x] Send with New Document. * [x] Send with New Audio. * [x] Send with New Voice. -* [ ] Send with New Contact. -* [ ] Send with New Location. +* [x] Send with New Location. * [ ] Send with New Venue. * [ ] Send with New Video. * [x] Send with New Sticker. @@ -63,6 +62,7 @@ docker run --rm \ -e PLUGIN_STICKER=tests/github-logo.png \ -e PLUGIN_AUDIO=tests/audio.mp3 \ -e PLUGIN_VOICE=tests/voice.ogg \ + -e PLUGIN_LOCATION=24.9163213,121.1424972 \ -e PLUGIN_DEBUG=true \ -e PLUGIN_FORMAT=markdown \ -e DRONE_REPO_OWNER=appleboy \ diff --git a/main.go b/main.go index e3cd0fc..f696cbe 100644 --- a/main.go +++ b/main.go @@ -57,6 +57,11 @@ func main() { Usage: "send voice message", EnvVar: "PLUGIN_VOICE", }, + cli.StringSliceFlag{ + Name: "location", + Usage: "send location message", + EnvVar: "PLUGIN_LOCATION", + }, cli.BoolFlag{ Name: "debug", Usage: "enable debug message", @@ -151,6 +156,7 @@ func run(c *cli.Context) error { Sticker: c.StringSlice("sticker"), Audio: c.StringSlice("audio"), Voice: c.StringSlice("voice"), + Location: c.StringSlice("location"), Format: c.String("format"), }, } diff --git a/plugin.go b/plugin.go index 708a1cc..a63e27f 100644 --- a/plugin.go +++ b/plugin.go @@ -41,6 +41,7 @@ type ( Sticker []string Audio []string Voice []string + Location []string Format string } @@ -50,6 +51,12 @@ type ( Build Build Config Config } + + // Location format + Location struct { + Latitude float64 + Longitude float64 + } ) func trimElement(keys []string) []string { @@ -79,6 +86,35 @@ func fileExist(keys []string) []string { return newKeys } +func convertLocation(value string) (Location, bool) { + var latitude, longitude float64 + var err error + values := trimElement(strings.Split(value, ",")) + + if len(values) < 2 { + return Location{}, true + } + + latitude, err = strconv.ParseFloat(values[0], 64) + + if err != nil { + log.Println(err.Error()) + return Location{}, true + } + + longitude, err = strconv.ParseFloat(values[1], 64) + + if err != nil { + log.Println(err.Error()) + return Location{}, true + } + + return Location{ + Latitude: latitude, + Longitude: longitude, + }, false +} + func parseID(keys []string) []int64 { var newKeys []int64 @@ -128,6 +164,7 @@ func (p Plugin) Exec() error { stickers := fileExist(trimElement(p.Config.Sticker)) audios := fileExist(trimElement(p.Config.Audio)) voices := fileExist(trimElement(p.Config.Voice)) + locations := trimElement(p.Config.Location) // send message. for _, user := range ids { @@ -162,6 +199,17 @@ func (p Plugin) Exec() error { msg := tgbotapi.NewVoiceUpload(user, value) p.Send(bot, msg) } + + for _, value := range locations { + location, empty := convertLocation(value) + + if empty == true { + continue + } + + msg := tgbotapi.NewLocation(user, location.Latitude, location.Longitude) + p.Send(bot, msg) + } } return nil diff --git a/plugin_test.go b/plugin_test.go index 6a46a59..5a9f461 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -74,6 +74,7 @@ func TestSendMessage(t *testing.T) { Sticker: []string{"tests/github-logo.png", "tests/github.png", "1234", " "}, Audio: []string{"tests/audio.mp3", "tests/github.png", "1234", " "}, Voice: []string{"tests/voice.ogg", "tests/github.png", "1234", " "}, + Location: []string{"24.9163213,121.1424972", "1", " "}, Debug: false, }, } @@ -152,3 +153,38 @@ func TestCheckFileExist(t *testing.T) { assert.Equal(t, result, fileExist(input)) } + +func TestConvertLocation(t *testing.T) { + var input string + var result Location + var empty bool + + input = "1" + result, empty = convertLocation(input) + + assert.Equal(t, true, empty) + assert.Equal(t, Location{}, result) + + // strconv.ParseInt: parsing "測試": invalid syntax + input = "測試,139.704051" + result, empty = convertLocation(input) + + assert.Equal(t, true, empty) + assert.Equal(t, Location{}, result) + + // strconv.ParseInt: parsing "測試": invalid syntax + input = "35.661777,測試" + result, empty = convertLocation(input) + + assert.Equal(t, true, empty) + assert.Equal(t, Location{}, result) + + input = "35.661777,139.704051" + result, empty = convertLocation(input) + + assert.Equal(t, false, empty) + assert.Equal(t, Location{ + Latitude: float64(35.661777), + Longitude: float64(139.704051), + }, result) +}