diff --git a/README.md b/README.md index de01458..fe1b6b8 100644 --- a/README.md +++ b/README.md @@ -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 \ diff --git a/main.go b/main.go index f971a5b..a79cb4e 100644 --- a/main.go +++ b/main.go @@ -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", @@ -122,12 +127,13 @@ func run(c *cli.Context) error { Link: c.String("build.link"), }, Config: Config{ - Token: c.String("token"), - Debug: c.Bool("debug"), - To: c.StringSlice("to"), - Message: c.StringSlice("message"), - Photo: c.StringSlice("photo"), - Format: c.String("format"), + Token: c.String("token"), + Debug: c.Bool("debug"), + To: c.StringSlice("to"), + Message: c.StringSlice("message"), + Photo: c.StringSlice("photo"), + Document: c.StringSlice("document"), + Format: c.String("format"), }, } diff --git a/plugin.go b/plugin.go index 9c733bc..a4ad100 100644 --- a/plugin.go +++ b/plugin.go @@ -32,12 +32,13 @@ type ( // Config for the plugin. Config struct { - Token string - Debug bool - To []string - Message []string - Photo []string - Format string + Token string + Debug bool + To []string + Message []string + Photo []string + Document []string + Format string } // Plugin values. @@ -120,33 +121,39 @@ 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) - _, err := bot.Send(msg) + p.Send(bot, msg) + } - if err != nil { - log.Println(err.Error()) - } + 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()) + } +} + // Message is plugin default message. func (p Plugin) Message(repo Repo, build Build) []string { return []string{fmt.Sprintf("[%s] <%s> (%s)『%s』by %s", diff --git a/plugin_test.go b/plugin_test.go index cbc2ddc..efe4a25 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -66,11 +66,12 @@ func TestSendMessage(t *testing.T) { }, Config: Config{ - Token: os.Getenv("TELEGRAM_TOKEN"), - To: []string{os.Getenv("TELEGRAM_TO"), "中文ID", "1234567890"}, - Message: []string{"Test Telegram Chat Bot From Travis or Local", " "}, - Photo: []string{"tests/github.png", "1234", " "}, - Debug: false, + Token: os.Getenv("TELEGRAM_TOKEN"), + 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, }, }