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 New Photo.
* [ ] Send with New Document.
* [x] Send with New Document.
* [ ] Send with New Audio.
* [ ] Send with New Voice.
* [ ] Send with New Contact.
@ -59,6 +59,7 @@ docker run --rm \
-e PLUGIN_TO=xxxxxxx \
-e PLUGIN_MESSAGE=test \
-e PLUGIN_PHOTO=tests/github.png \
-e PLUGIN_DOCUMENT=tests/gophercolor.png \
-e PLUGIN_DEBUG=true \
-e PLUGIN_FORMAT=markdown \
-e DRONE_REPO_OWNER=appleboy \

View File

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

View File

@ -37,6 +37,7 @@ type (
To []string
Message []string
Photo []string
Document []string
Format string
}
@ -120,32 +121,38 @@ func (p Plugin) Exec() error {
// parse ids
ids := parseID(p.Config.To)
photos := fileExist(trimElement(p.Config.Photo))
documents := fileExist(trimElement(p.Config.Document))
// send message.
for _, user := range ids {
for _, value := range trimElement(message) {
msg := tgbotapi.NewMessage(user, value)
msg.ParseMode = p.Config.Format
_, err := bot.Send(msg)
if err != nil {
log.Println(err.Error())
}
p.Send(bot, msg)
}
for _, value := range photos {
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)
if err != nil {
log.Println(err.Error())
}
}
}
return nil
}
// Message is plugin default message.
func (p Plugin) Message(repo Repo, build Build) []string {

View File

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