Support Venue Message.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-10-14 16:28:40 +08:00
parent b52dcfe9c2
commit 693f15783c
4 changed files with 58 additions and 1 deletions

View File

@ -12,7 +12,7 @@
* [x] Send with New Audio. * [x] Send with New Audio.
* [x] Send with New Voice. * [x] Send with New Voice.
* [x] Send with New Location. * [x] Send with New Location.
* [ ] Send with New Venue. * [x] Send with New Venue.
* [x] Send with New Video. * [x] Send with New Video.
* [x] Send with New Sticker. * [x] Send with New Sticker.
@ -63,6 +63,7 @@ docker run --rm \
-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_LOCATION=24.9163213,121.1424972 \
-e PLUGIN_VENUE=24.9163213,121.1424972,title,address \
-e PLUGIN_VIDEO=tests/video.mp4 \ -e PLUGIN_VIDEO=tests/video.mp4 \
-e PLUGIN_DEBUG=true \ -e PLUGIN_DEBUG=true \
-e PLUGIN_FORMAT=markdown \ -e PLUGIN_FORMAT=markdown \

View File

@ -62,6 +62,11 @@ func main() {
Usage: "send location message", Usage: "send location message",
EnvVar: "PLUGIN_LOCATION", EnvVar: "PLUGIN_LOCATION",
}, },
cli.StringSliceFlag{
Name: "venue",
Usage: "send venue message",
EnvVar: "PLUGIN_VENUE",
},
cli.StringSliceFlag{ cli.StringSliceFlag{
Name: "video", Name: "video",
Usage: "send video message", Usage: "send video message",
@ -163,6 +168,7 @@ func run(c *cli.Context) error {
Voice: c.StringSlice("voice"), Voice: c.StringSlice("voice"),
Location: c.StringSlice("location"), Location: c.StringSlice("location"),
Video: c.StringSlice("video"), Video: c.StringSlice("video"),
Venue: c.StringSlice("venue"),
Format: c.String("format"), Format: c.String("format"),
}, },
} }

View File

@ -43,6 +43,7 @@ type (
Voice []string Voice []string
Location []string Location []string
Video []string Video []string
Venue []string
Format string Format string
} }
@ -55,6 +56,8 @@ type (
// Location format // Location format
Location struct { Location struct {
Title string
Address string
Latitude float64 Latitude float64
Longitude float64 Longitude float64
} }
@ -89,6 +92,7 @@ func fileExist(keys []string) []string {
func convertLocation(value string) (Location, bool) { func convertLocation(value string) (Location, bool) {
var latitude, longitude float64 var latitude, longitude float64
var title, address string
var err error var err error
values := trimElement(strings.Split(value, ",")) values := trimElement(strings.Split(value, ","))
@ -96,6 +100,15 @@ func convertLocation(value string) (Location, bool) {
return Location{}, true 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) latitude, err = strconv.ParseFloat(values[0], 64)
if err != nil { if err != nil {
@ -111,6 +124,8 @@ func convertLocation(value string) (Location, bool) {
} }
return Location{ return Location{
Title: title,
Address: address,
Latitude: latitude, Latitude: latitude,
Longitude: longitude, Longitude: longitude,
}, false }, false
@ -167,6 +182,7 @@ func (p Plugin) Exec() error {
voices := fileExist(trimElement(p.Config.Voice)) voices := fileExist(trimElement(p.Config.Voice))
videos := fileExist(trimElement(p.Config.Video)) videos := fileExist(trimElement(p.Config.Video))
locations := trimElement(p.Config.Location) locations := trimElement(p.Config.Location)
venues := trimElement(p.Config.Venue)
// send message. // send message.
for _, user := range ids { for _, user := range ids {
@ -218,6 +234,17 @@ func (p Plugin) Exec() error {
msg := tgbotapi.NewLocation(user, location.Latitude, location.Longitude) msg := tgbotapi.NewLocation(user, location.Latitude, location.Longitude)
p.Send(bot, msg) 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 return nil

View File

@ -75,6 +75,7 @@ func TestSendMessage(t *testing.T) {
Audio: []string{"tests/audio.mp3", "1234", " "}, Audio: []string{"tests/audio.mp3", "1234", " "},
Voice: []string{"tests/voice.ogg", "1234", " "}, Voice: []string{"tests/voice.ogg", "1234", " "},
Location: []string{"24.9163213,121.1424972", "1", " "}, Location: []string{"24.9163213,121.1424972", "1", " "},
Venue: []string{"35.661777,139.704051,竹北體育館,新竹縣竹北市", "24.9163213,121.1424972", "1", " "},
Video: []string{"tests/video.mp4", "1234", " "}, Video: []string{"tests/video.mp4", "1234", " "},
Debug: false, Debug: false,
}, },
@ -188,4 +189,26 @@ func TestConvertLocation(t *testing.T) {
Latitude: float64(35.661777), Latitude: float64(35.661777),
Longitude: float64(139.704051), Longitude: float64(139.704051),
}, result) }, 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)
} }