Compare commits

..

12 Commits

Author SHA1 Message Date
Bo-Yi Wu 6cdfb8ba10 fix wrong name.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-07 09:09:37 -05:00
Bo-Yi Wu dfd91c671b Support Send with New Document.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-07 09:09:37 -05:00
Bo-Yi Wu 78f99c9c7f [ci skip] update readme.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-07 17:25:24 +08:00
Bo-Yi Wu 8bd5e965e7 add tests image.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-07 03:02:29 -05:00
Bo-Yi Wu 97227c749e [ci skip] update readme.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-07 03:02:29 -05:00
Bo-Yi Wu fd83eb04f4 support send photo message.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-07 03:02:29 -05:00
Bo-Yi Wu ee4c70fa29 [ci skip] update readme.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-07 08:18:50 +08:00
Bo-Yi Wu 09796b1872 update readme.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-06 12:00:05 -05:00
Bo-Yi Wu 2e602c4560 support markdown and html format.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-06 12:00:05 -05:00
Bo-Yi Wu 75c04eafaa support markdown format.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-07 00:28:13 +08:00
Bo-Yi Wu 215d08a0d6 [ci skip] update readme.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-05 13:32:07 +08:00
Bo-Yi Wu e370283454 add target on PHONY
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2016-10-04 23:06:39 +08:00
7 changed files with 105 additions and 17 deletions
+1 -1
View File
@@ -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"
+19 -1
View File
@@ -2,7 +2,20 @@
[![Build Status](https://travis-ci.org/appleboy/drone-telegram.svg?branch=master)](https://travis-ci.org/appleboy/drone-telegram) [![codecov](https://codecov.io/gh/appleboy/drone-telegram/branch/master/graph/badge.svg)](https://codecov.io/gh/appleboy/drone-telegram) [![Go Report Card](https://goreportcard.com/badge/github.com/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 -5
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{
@@ -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"),
},
}
+45 -6
View File
@@ -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
View File
@@ -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))
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB