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 \

20
main.go
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",
@ -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"),
},
}

View File

@ -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",

View File

@ -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,
},
}