Support Send with New Document.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-10-07 21:50:42 +08:00
parent 78f99c9c7f
commit dfd91c671b
4 changed files with 44 additions and 29 deletions

View File

@ -8,7 +8,7 @@
* [x] Send with Text Message. (`markdown` or `html` format) * [x] Send with Text Message. (`markdown` or `html` format)
* [x] Send with New Photo. * [x] Send with New Photo.
* [ ] Send with New Document. * [x] Send with New Document.
* [ ] Send with New Audio. * [ ] Send with New Audio.
* [ ] Send with New Voice. * [ ] Send with New Voice.
* [ ] Send with New Contact. * [ ] Send with New Contact.
@ -59,6 +59,7 @@ docker run --rm \
-e PLUGIN_TO=xxxxxxx \ -e PLUGIN_TO=xxxxxxx \
-e PLUGIN_MESSAGE=test \ -e PLUGIN_MESSAGE=test \
-e PLUGIN_PHOTO=tests/github.png \ -e PLUGIN_PHOTO=tests/github.png \
-e PLUGIN_DOCUMENT=tests/gophercolor.png \
-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

@ -24,7 +24,7 @@ func main() {
}, },
cli.StringSliceFlag{ cli.StringSliceFlag{
Name: "to", Name: "to",
Usage: "send message to user", Usage: "telegram user",
EnvVar: "PLUGIN_TO", EnvVar: "PLUGIN_TO",
}, },
cli.StringSliceFlag{ cli.StringSliceFlag{
@ -37,6 +37,11 @@ func main() {
Usage: "send photo message", Usage: "send photo message",
EnvVar: "PLUGIN_PHOTO", EnvVar: "PLUGIN_PHOTO",
}, },
cli.StringSliceFlag{
Name: "photo",
Usage: "send document message",
EnvVar: "PLUGIN_DOCUMENT",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "debug", Name: "debug",
Usage: "enable debug message", Usage: "enable debug message",
@ -127,6 +132,7 @@ func run(c *cli.Context) error {
To: c.StringSlice("to"), To: c.StringSlice("to"),
Message: c.StringSlice("message"), Message: c.StringSlice("message"),
Photo: c.StringSlice("photo"), Photo: c.StringSlice("photo"),
Document: c.StringSlice("document"),
Format: c.String("format"), Format: c.String("format"),
}, },
} }

View File

@ -37,6 +37,7 @@ type (
To []string To []string
Message []string Message []string
Photo []string Photo []string
Document []string
Format string Format string
} }
@ -120,31 +121,37 @@ func (p Plugin) Exec() error {
// parse ids // parse ids
ids := parseID(p.Config.To) ids := parseID(p.Config.To)
photos := fileExist(trimElement(p.Config.Photo)) photos := fileExist(trimElement(p.Config.Photo))
documents := fileExist(trimElement(p.Config.Document))
// send message. // send message.
for _, user := range ids { for _, user := range ids {
for _, value := range trimElement(message) { for _, value := range trimElement(message) {
msg := tgbotapi.NewMessage(user, value) msg := tgbotapi.NewMessage(user, value)
msg.ParseMode = p.Config.Format msg.ParseMode = p.Config.Format
p.Send(bot, msg)
_, err := bot.Send(msg)
if err != nil {
log.Println(err.Error())
}
} }
for _, value := range photos { for _, value := range photos {
msg := tgbotapi.NewPhotoUpload(user, value) msg := tgbotapi.NewPhotoUpload(user, value)
p.Send(bot, msg)
}
for _, value := range documents {
msg := tgbotapi.NewDocumentUpload(user, value)
p.Send(bot, msg)
}
}
return nil
}
// Send bot message.
func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) {
_, err := bot.Send(msg) _, err := bot.Send(msg)
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
} }
}
}
return nil
} }
// Message is plugin default message. // Message is plugin default message.

View File

@ -70,6 +70,7 @@ func TestSendMessage(t *testing.T) {
To: []string{os.Getenv("TELEGRAM_TO"), "中文ID", "1234567890"}, To: []string{os.Getenv("TELEGRAM_TO"), "中文ID", "1234567890"},
Message: []string{"Test Telegram Chat Bot From Travis or Local", " "}, Message: []string{"Test Telegram Chat Bot From Travis or Local", " "},
Photo: []string{"tests/github.png", "1234", " "}, Photo: []string{"tests/github.png", "1234", " "},
Document: []string{"tests/gophercolor.png", "1234", " "},
Debug: false, Debug: false,
}, },
} }