Support Location Message.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-10-14 14:27:34 +08:00
parent 1142360e53
commit 3f25341c8b
4 changed files with 92 additions and 2 deletions

View File

@ -11,8 +11,7 @@
* [x] Send with New Document. * [x] Send with New Document.
* [x] Send with New Audio. * [x] Send with New Audio.
* [x] Send with New Voice. * [x] Send with New Voice.
* [ ] Send with New Contact. * [x] Send with New Location.
* [ ] Send with New Location.
* [ ] Send with New Venue. * [ ] Send with New Venue.
* [ ] Send with New Video. * [ ] Send with New Video.
* [x] Send with New Sticker. * [x] Send with New Sticker.
@ -63,6 +62,7 @@ docker run --rm \
-e PLUGIN_STICKER=tests/github-logo.png \ -e PLUGIN_STICKER=tests/github-logo.png \
-e PLUGIN_AUDIO=tests/audio.mp3 \ -e PLUGIN_AUDIO=tests/audio.mp3 \
-e PLUGIN_VOICE=tests/voice.ogg \ -e PLUGIN_VOICE=tests/voice.ogg \
-e PLUGIN_LOCATION=24.9163213,121.1424972 \
-e PLUGIN_DEBUG=true \ -e PLUGIN_DEBUG=true \
-e PLUGIN_FORMAT=markdown \ -e PLUGIN_FORMAT=markdown \
-e DRONE_REPO_OWNER=appleboy \ -e DRONE_REPO_OWNER=appleboy \

View File

@ -57,6 +57,11 @@ func main() {
Usage: "send voice message", Usage: "send voice message",
EnvVar: "PLUGIN_VOICE", EnvVar: "PLUGIN_VOICE",
}, },
cli.StringSliceFlag{
Name: "location",
Usage: "send location message",
EnvVar: "PLUGIN_LOCATION",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "debug", Name: "debug",
Usage: "enable debug message", Usage: "enable debug message",
@ -151,6 +156,7 @@ func run(c *cli.Context) error {
Sticker: c.StringSlice("sticker"), Sticker: c.StringSlice("sticker"),
Audio: c.StringSlice("audio"), Audio: c.StringSlice("audio"),
Voice: c.StringSlice("voice"), Voice: c.StringSlice("voice"),
Location: c.StringSlice("location"),
Format: c.String("format"), Format: c.String("format"),
}, },
} }

View File

@ -41,6 +41,7 @@ type (
Sticker []string Sticker []string
Audio []string Audio []string
Voice []string Voice []string
Location []string
Format string Format string
} }
@ -50,6 +51,12 @@ type (
Build Build Build Build
Config Config Config Config
} }
// Location format
Location struct {
Latitude float64
Longitude float64
}
) )
func trimElement(keys []string) []string { func trimElement(keys []string) []string {
@ -79,6 +86,35 @@ func fileExist(keys []string) []string {
return newKeys 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 { func parseID(keys []string) []int64 {
var newKeys []int64 var newKeys []int64
@ -128,6 +164,7 @@ func (p Plugin) Exec() error {
stickers := fileExist(trimElement(p.Config.Sticker)) stickers := fileExist(trimElement(p.Config.Sticker))
audios := fileExist(trimElement(p.Config.Audio)) audios := fileExist(trimElement(p.Config.Audio))
voices := fileExist(trimElement(p.Config.Voice)) voices := fileExist(trimElement(p.Config.Voice))
locations := trimElement(p.Config.Location)
// send message. // send message.
for _, user := range ids { for _, user := range ids {
@ -162,6 +199,17 @@ func (p Plugin) Exec() error {
msg := tgbotapi.NewVoiceUpload(user, value) msg := tgbotapi.NewVoiceUpload(user, value)
p.Send(bot, msg) 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 return nil

View File

@ -74,6 +74,7 @@ func TestSendMessage(t *testing.T) {
Sticker: []string{"tests/github-logo.png", "tests/github.png", "1234", " "}, Sticker: []string{"tests/github-logo.png", "tests/github.png", "1234", " "},
Audio: []string{"tests/audio.mp3", "tests/github.png", "1234", " "}, Audio: []string{"tests/audio.mp3", "tests/github.png", "1234", " "},
Voice: []string{"tests/voice.ogg", "tests/github.png", "1234", " "}, Voice: []string{"tests/voice.ogg", "tests/github.png", "1234", " "},
Location: []string{"24.9163213,121.1424972", "1", " "},
Debug: false, Debug: false,
}, },
} }
@ -152,3 +153,38 @@ func TestCheckFileExist(t *testing.T) {
assert.Equal(t, result, fileExist(input)) 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)
}