mirror of
https://github.com/appleboy/drone-telegram.git
synced 2026-06-13 18:41:32 +08:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6cdfb8ba10 | |||
| dfd91c671b | |||
| 78f99c9c7f | |||
| 8bd5e965e7 | |||
| 97227c749e | |||
| fd83eb04f4 | |||
| ee4c70fa29 | |||
| 09796b1872 | |||
| 2e602c4560 | |||
| 75c04eafaa | |||
| 215d08a0d6 | |||
| e370283454 |
@@ -1,4 +1,4 @@
|
||||
.PHONY:
|
||||
.PHONY: install build test html update docker_build docker_image docker_deploy clean
|
||||
|
||||
VERSION := $(shell git describe --tags || git rev-parse --short HEAD)
|
||||
DEPLOY_ACCOUNT := "appleboy"
|
||||
|
||||
@@ -2,7 +2,20 @@
|
||||
|
||||
[](https://travis-ci.org/appleboy/drone-telegram) [](https://codecov.io/gh/appleboy/drone-telegram) [](https://goreportcard.com/report/github.com/appleboy/drone-telegram)
|
||||
|
||||
Drone plugin for sending telegram notifications.
|
||||
[Drone](https://github.com/drone/drone) plugin for sending telegram notifications.
|
||||
|
||||
## Feature
|
||||
|
||||
* [x] Send with Text Message. (`markdown` or `html` format)
|
||||
* [x] Send with New Photo.
|
||||
* [x] Send with New Document.
|
||||
* [ ] Send with New Audio.
|
||||
* [ ] Send with New Voice.
|
||||
* [ ] Send with New Contact.
|
||||
* [ ] Send with New Location.
|
||||
* [ ] Send with New Venue.
|
||||
* [ ] Send with New Video.
|
||||
* [ ] Send with New Sticker.
|
||||
|
||||
## Build
|
||||
|
||||
@@ -45,7 +58,10 @@ docker run --rm \
|
||||
-e PLUGIN_TOKEN=xxxxxxx \
|
||||
-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 \
|
||||
-e DRONE_REPO_NAME=go-hello \
|
||||
-e DRONE_COMMIT_SHA=e5e82b5eb3737205c25955dcc3dcacc839b7be52 \
|
||||
@@ -54,5 +70,7 @@ docker run --rm \
|
||||
-e DRONE_BUILD_NUMBER=1 \
|
||||
-e DRONE_BUILD_STATUS=success \
|
||||
-e DRONE_BUILD_LINK=http://github.com/appleboy/go-hello \
|
||||
-v $(pwd):$(pwd) \
|
||||
-w $(pwd) \
|
||||
appleboy/drone-telegram
|
||||
```
|
||||
|
||||
@@ -24,7 +24,7 @@ func main() {
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "to",
|
||||
Usage: "send message to user",
|
||||
Usage: "telegram user",
|
||||
EnvVar: "PLUGIN_TO",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
@@ -32,11 +32,27 @@ func main() {
|
||||
Usage: "send telegram message",
|
||||
EnvVar: "PLUGIN_MESSAGE",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "photo",
|
||||
Usage: "send photo message",
|
||||
EnvVar: "PLUGIN_PHOTO",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "document",
|
||||
Usage: "send document message",
|
||||
EnvVar: "PLUGIN_DOCUMENT",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "debug",
|
||||
Usage: "enable debug message",
|
||||
EnvVar: "PLUGIN_DEBUG",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "format",
|
||||
Value: "markdown",
|
||||
Usage: "telegram message format",
|
||||
EnvVar: "PLUGIN_FORMAT",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "repo.owner",
|
||||
Usage: "repository owner",
|
||||
@@ -111,10 +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"),
|
||||
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"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -31,10 +32,13 @@ type (
|
||||
|
||||
// Config for the plugin.
|
||||
Config struct {
|
||||
Token string
|
||||
Debug bool
|
||||
To []string
|
||||
Message []string
|
||||
Token string
|
||||
Debug bool
|
||||
To []string
|
||||
Message []string
|
||||
Photo []string
|
||||
Document []string
|
||||
Format string
|
||||
}
|
||||
|
||||
// Plugin values.
|
||||
@@ -59,6 +63,19 @@ func trimElement(keys []string) []string {
|
||||
return newKeys
|
||||
}
|
||||
|
||||
func fileExist(keys []string) []string {
|
||||
var newKeys []string
|
||||
|
||||
for _, value := range keys {
|
||||
if _, err := os.Stat(value); os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
newKeys = append(newKeys, value)
|
||||
}
|
||||
|
||||
return newKeys
|
||||
}
|
||||
|
||||
func parseID(keys []string) []int64 {
|
||||
var newKeys []int64
|
||||
|
||||
@@ -94,7 +111,7 @@ func (p Plugin) Exec() error {
|
||||
bot, err := tgbotapi.NewBotAPI(p.Config.Token)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
log.Println("Initialize New Bot Error:", err.Error())
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -103,18 +120,40 @@ 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)
|
||||
bot.Send(msg)
|
||||
msg.ParseMode = p.Config.Format
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
// Message is plugin default message.
|
||||
func (p Plugin) Message(repo Repo, build Build) []string {
|
||||
return []string{fmt.Sprintf("[%s] <%s> (%s)『%s』by %s",
|
||||
|
||||
+16
-4
@@ -66,10 +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", " "},
|
||||
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,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -137,3 +139,13 @@ func TestParseID(t *testing.T) {
|
||||
|
||||
assert.Equal(t, result, parseID(input))
|
||||
}
|
||||
|
||||
func TestCheckFileExist(t *testing.T) {
|
||||
var input []string
|
||||
var result []string
|
||||
|
||||
input = []string{"tests/gophercolor.png", "測試", "3"}
|
||||
result = []string{"tests/gophercolor.png"}
|
||||
|
||||
assert.Equal(t, result, fileExist(input))
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 53 KiB |
Reference in New Issue
Block a user