Support Send with New Document.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
78f99c9c7f
commit
dfd91c671b
@ -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 \
|
||||||
|
20
main.go
20
main.go
@ -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",
|
||||||
@ -122,12 +127,13 @@ func run(c *cli.Context) error {
|
|||||||
Link: c.String("build.link"),
|
Link: c.String("build.link"),
|
||||||
},
|
},
|
||||||
Config: Config{
|
Config: Config{
|
||||||
Token: c.String("token"),
|
Token: c.String("token"),
|
||||||
Debug: c.Bool("debug"),
|
Debug: c.Bool("debug"),
|
||||||
To: c.StringSlice("to"),
|
To: c.StringSlice("to"),
|
||||||
Message: c.StringSlice("message"),
|
Message: c.StringSlice("message"),
|
||||||
Photo: c.StringSlice("photo"),
|
Photo: c.StringSlice("photo"),
|
||||||
Format: c.String("format"),
|
Document: c.StringSlice("document"),
|
||||||
|
Format: c.String("format"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
39
plugin.go
39
plugin.go
@ -32,12 +32,13 @@ type (
|
|||||||
|
|
||||||
// Config for the plugin.
|
// Config for the plugin.
|
||||||
Config struct {
|
Config struct {
|
||||||
Token string
|
Token string
|
||||||
Debug bool
|
Debug bool
|
||||||
To []string
|
To []string
|
||||||
Message []string
|
Message []string
|
||||||
Photo []string
|
Photo []string
|
||||||
Format string
|
Document []string
|
||||||
|
Format string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plugin values.
|
// Plugin values.
|
||||||
@ -120,33 +121,39 @@ 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)
|
||||||
_, err := bot.Send(msg)
|
p.Send(bot, msg)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
for _, value := range documents {
|
||||||
log.Println(err.Error())
|
msg := tgbotapi.NewDocumentUpload(user, value)
|
||||||
}
|
p.Send(bot, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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.
|
// Message is plugin default message.
|
||||||
func (p Plugin) Message(repo Repo, build Build) []string {
|
func (p Plugin) Message(repo Repo, build Build) []string {
|
||||||
return []string{fmt.Sprintf("[%s] <%s> (%s)『%s』by %s",
|
return []string{fmt.Sprintf("[%s] <%s> (%s)『%s』by %s",
|
||||||
|
@ -66,11 +66,12 @@ func TestSendMessage(t *testing.T) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
Config: Config{
|
Config: Config{
|
||||||
Token: os.Getenv("TELEGRAM_TOKEN"),
|
Token: os.Getenv("TELEGRAM_TOKEN"),
|
||||||
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", " "},
|
||||||
Debug: false,
|
Document: []string{"tests/gophercolor.png", "1234", " "},
|
||||||
|
Debug: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user