mirror of
https://github.com/appleboy/drone-telegram.git
synced 2026-06-13 18:41:32 +08:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ee0358892d | |||
| 153fe2cb01 | |||
| ce15798a06 | |||
| 3bccad3ca8 | |||
| 2c8ab1428b | |||
| 602b560c4d | |||
| ba1db08ad2 | |||
| e3fc3f805d | |||
| 44ac6f43de | |||
| 5ac85a3412 | |||
| 79e531f648 | |||
| abba2cb37d | |||
| 7f8bd6ba04 | |||
| 0dc176a2ac | |||
| 2af2c10e0e | |||
| fe798d95b2 | |||
| ace08f12b9 | |||
| b7f97df101 | |||
| 866e25b433 | |||
| f8f311ba1f | |||
| 75ffa6ef7b | |||
| f529160760 |
@@ -10,6 +10,7 @@ environment:
|
||||
secure: em/TNLUXxG19O/HvbvfJuQ==
|
||||
docker_password:
|
||||
secure: Yo9FJJqihaNz5q8T4Jz8tQ==
|
||||
GO111MODULE: on
|
||||
|
||||
branches:
|
||||
only:
|
||||
@@ -23,6 +24,7 @@ install:
|
||||
|
||||
build_script:
|
||||
- ps: |
|
||||
go mod download
|
||||
if ( $env:APPVEYOR_REPO_TAG -eq 'false' ) {
|
||||
$version = $env:APPVEYOR_REPO_COMMIT
|
||||
} else {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
---
|
||||
date: 2017-01-08T00:00:00+00:00
|
||||
date: 2019-10-19T00:00:00+00:00
|
||||
title: Telegram
|
||||
author: appleboy
|
||||
tags: [ notifications, chat ]
|
||||
@@ -11,7 +11,7 @@ image: appleboy/drone-telegram
|
||||
The Telegram plugin posts build status messages to your account. The below pipeline configuration demonstrates simple usage:
|
||||
|
||||
```yaml
|
||||
- name: send notification
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
@@ -21,122 +21,181 @@ The Telegram plugin posts build status messages to your account. The below pipel
|
||||
Example configuration with photo message:
|
||||
|
||||
```diff
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ photo:
|
||||
+ - tests/1.png
|
||||
+ - tests/2.png
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ photo:
|
||||
+ - tests/1.png
|
||||
+ - tests/2.png
|
||||
```
|
||||
|
||||
Example configuration with document message:
|
||||
|
||||
```diff
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ document:
|
||||
+ - tests/1.pdf
|
||||
+ - tests/2.pdf
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ document:
|
||||
+ - tests/1.pdf
|
||||
+ - tests/2.pdf
|
||||
```
|
||||
|
||||
Example configuration with sticker message:
|
||||
|
||||
```diff
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ sticker:
|
||||
+ - tests/3.png
|
||||
+ - tests/4.png
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ sticker:
|
||||
+ - tests/3.png
|
||||
+ - tests/4.png
|
||||
```
|
||||
|
||||
Example configuration with audio message:
|
||||
|
||||
```diff
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ audio:
|
||||
+ - tests/audio1.mp3
|
||||
+ - tests/audio2.mp3
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ audio:
|
||||
+ - tests/audio1.mp3
|
||||
+ - tests/audio2.mp3
|
||||
```
|
||||
|
||||
Example configuration with voice message:
|
||||
|
||||
```diff
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ voice:
|
||||
+ - tests/voice1.ogg
|
||||
+ - tests/voice2.ogg
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ voice:
|
||||
+ - tests/voice1.ogg
|
||||
+ - tests/voice2.ogg
|
||||
```
|
||||
|
||||
Example configuration with location message:
|
||||
|
||||
```diff
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ location:
|
||||
+ - 24.9163213,121.1424972
|
||||
+ - 24.9263213,121.1224972
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ location:
|
||||
+ - 24.9163213,121.1424972
|
||||
+ - 24.9263213,121.1224972
|
||||
```
|
||||
|
||||
Example configuration with venue message:
|
||||
|
||||
```diff
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ venue:
|
||||
+ - 24.9163213,121.1424972,title,address
|
||||
+ - 24.3163213,121.1824972,title,address
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ venue:
|
||||
+ - 24.9163213,121.1424972,title,address
|
||||
+ - 24.3163213,121.1824972,title,address
|
||||
```
|
||||
|
||||
Example configuration with video message:
|
||||
|
||||
```diff
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ video:
|
||||
+ - tests/video1.mp4
|
||||
+ - tests/video2.mp4
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ video:
|
||||
+ - tests/video1.mp4
|
||||
+ - tests/video2.mp4
|
||||
```
|
||||
|
||||
Example configuration with message format:
|
||||
|
||||
```diff
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ format: markdown
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ format: markdown
|
||||
```
|
||||
|
||||
Example configuration with a custom message template:
|
||||
|
||||
```diff
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ message: >
|
||||
+ {{#success build.status}}
|
||||
+ build {{build.number}} succeeded. Good job.
|
||||
+ {{else}}
|
||||
+ build {{build.number}} failed. Fix me please.
|
||||
+ {{/success}}
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ message: >
|
||||
+ {{#success build.status}}
|
||||
+ build {{build.number}} succeeded. Good job.
|
||||
+ {{else}}
|
||||
+ build {{build.number}} failed. Fix me please.
|
||||
+ {{/success}}
|
||||
```
|
||||
|
||||
Example configuration with a custom message template loaded from file:
|
||||
|
||||
```diff
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ message_file: message_file.tpl
|
||||
```
|
||||
|
||||
Example configuration with a generic message template loaded from file, with additional extra vars:
|
||||
|
||||
```diff
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
+ message_file: message_file.tpl
|
||||
+ template_vars:
|
||||
+ env: testing
|
||||
+ app: MyApp
|
||||
```
|
||||
|
||||
Where `message_file.tpl` is:
|
||||
|
||||
```bash
|
||||
Build finished for *{{tpl.app}}* - *{{tpl.env}}*
|
||||
|
||||
{{#success build.status}}
|
||||
build {{build.number}} succeeded. Good job.
|
||||
{{else}}
|
||||
build {{build.number}} failed. Fix me please.
|
||||
{{/success}}
|
||||
```
|
||||
|
||||
Example configuration with a custom socks5 URL:
|
||||
|
||||
```diff
|
||||
- name: send telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token: xxxxxxxxxx
|
||||
to: telegram_user_id
|
||||
message: send message using custom socks5 URL
|
||||
+ socks5: socks5://67.204.21.1:64312
|
||||
```
|
||||
|
||||
## Parameter Reference
|
||||
@@ -150,6 +209,12 @@ to
|
||||
message
|
||||
: overwrite the default message template
|
||||
|
||||
message_file
|
||||
: overwrite the default message template with the contents of the specified file
|
||||
|
||||
template_vars
|
||||
: define additional template vars. Example: `var1: hello` can be used within the template as `tpl.var1`
|
||||
|
||||
photo
|
||||
: local file path
|
||||
|
||||
|
||||
@@ -8,11 +8,9 @@ DEPLOY_ACCOUNT := appleboy
|
||||
DEPLOY_IMAGE := $(EXECUTABLE)
|
||||
|
||||
TARGETS ?= linux darwin windows
|
||||
PACKAGES ?= $(shell $(GO) list ./...)
|
||||
SOURCES ?= $(shell find . -name "*.go" -type f)
|
||||
TAGS ?=
|
||||
LDFLAGS ?= -X 'main.Version=$(VERSION)'
|
||||
TMPDIR := $(shell mktemp -d 2>/dev/null || mktemp -d -t 'tempdir')
|
||||
|
||||
ifneq ($(shell uname), Darwin)
|
||||
EXTLDFLAGS = -extldflags "-static" $(null)
|
||||
@@ -32,7 +30,7 @@ fmt:
|
||||
$(GOFMT) -w $(SOURCES)
|
||||
|
||||
vet:
|
||||
$(GO) vet $(PACKAGES)
|
||||
$(GO) vet ./...
|
||||
|
||||
lint:
|
||||
@hash revive > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||
@@ -64,7 +62,7 @@ fmt-check:
|
||||
fi;
|
||||
|
||||
test: fmt-check
|
||||
@$(GO) test -v -cover -coverprofile coverage.txt $(PACKAGES) && echo "\n==>\033[32m Ok\033[m\n" || exit 1
|
||||
@$(GO) test -v -cover -coverprofile coverage.txt ./... && echo "\n==>\033[32m Ok\033[m\n" || exit 1
|
||||
|
||||
install: $(SOURCES)
|
||||
$(GO) install -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)'
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
[](https://godoc.org/github.com/appleboy/drone-telegram)
|
||||
[](https://cloud.drone.io/appleboy/drone-telegram)
|
||||
[](https://ci.appveyor.com/project/appleboy/drone-telegram)
|
||||
[](https://ci.appveyor.com/project/appleboy/drone-telegram-cd47y)
|
||||
[](https://codecov.io/gh/appleboy/drone-telegram)
|
||||
[](https://goreportcard.com/report/github.com/appleboy/drone-telegram)
|
||||
[](https://hub.docker.com/r/appleboy/drone-telegram/)
|
||||
@@ -69,6 +69,7 @@ docker run --rm \
|
||||
-e PLUGIN_TOKEN=xxxxxxx \
|
||||
-e PLUGIN_TO=xxxxxxx \
|
||||
-e PLUGIN_MESSAGE=test \
|
||||
-e PLUGIN_MESSAGE_FILE=testmessage.md \
|
||||
-e PLUGIN_PHOTO=tests/github.png \
|
||||
-e PLUGIN_DOCUMENT=tests/gophercolor.png \
|
||||
-e PLUGIN_STICKER=tests/github-logo.png \
|
||||
@@ -102,7 +103,7 @@ Load all environments from file.
|
||||
|
||||
```bash
|
||||
docker run --rm \
|
||||
-e ENV_FILE=your_env_file_path \
|
||||
-e PLUGIN_ENV_FILE=your_env_file_path \
|
||||
-v $(pwd):$(pwd) \
|
||||
-w $(pwd) \
|
||||
appleboy/drone-telegram
|
||||
|
||||
@@ -5,6 +5,6 @@ LABEL maintainer="Bo-Yi Wu <appleboy.tw@gmail.com>" \
|
||||
org.label-schema.vendor="Bo-Yi Wu" \
|
||||
org.label-schema.schema-version="1.0"
|
||||
|
||||
ADD release/linux/amd64/drone-telegram /bin/
|
||||
COPY release/linux/amd64/drone-telegram /bin/
|
||||
|
||||
ENTRYPOINT ["/bin/drone-telegram"]
|
||||
|
||||
@@ -5,6 +5,6 @@ LABEL maintainer="Bo-Yi Wu <appleboy.tw@gmail.com>" \
|
||||
org.label-schema.vendor="Bo-Yi Wu" \
|
||||
org.label-schema.schema-version="1.0"
|
||||
|
||||
ADD release/linux/arm/drone-telegram /bin/
|
||||
COPY release/linux/arm/drone-telegram /bin/
|
||||
|
||||
ENTRYPOINT ["/bin/drone-telegram"]
|
||||
|
||||
@@ -5,6 +5,6 @@ LABEL maintainer="Bo-Yi Wu <appleboy.tw@gmail.com>" \
|
||||
org.label-schema.vendor="Bo-Yi Wu" \
|
||||
org.label-schema.schema-version="1.0"
|
||||
|
||||
ADD release/linux/arm64/drone-telegram /bin/
|
||||
COPY release/linux/arm64/drone-telegram /bin/
|
||||
|
||||
ENTRYPOINT ["/bin/drone-telegram"]
|
||||
|
||||
@@ -5,5 +5,6 @@ LABEL maintainer="Bo-Yi Wu <appleboy.tw@gmail.com>" \
|
||||
org.label-schema.vendor="Bo-Yi Wu" \
|
||||
org.label-schema.schema-version="1.0"
|
||||
|
||||
ADD release/drone-telegram.exe /drone-telegram.exe
|
||||
COPY release/drone-telegram.exe /drone-telegram.exe
|
||||
|
||||
ENTRYPOINT [ "\\drone-telegram.exe" ]
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -15,6 +14,11 @@ var (
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Load env-file if it exists first
|
||||
if filename, found := os.LookupEnv("PLUGIN_ENV_FILE"); found {
|
||||
godotenv.Load(filename)
|
||||
}
|
||||
|
||||
app := cli.NewApp()
|
||||
app.Name = "telegram plugin"
|
||||
app.Usage = "telegram plugin"
|
||||
@@ -29,13 +33,23 @@ func main() {
|
||||
cli.StringSliceFlag{
|
||||
Name: "to",
|
||||
Usage: "telegram user",
|
||||
EnvVar: "PLUGIN_TO,TELEGRAM_TO,INPUT_TOKEN",
|
||||
EnvVar: "PLUGIN_TO,TELEGRAM_TO,INPUT_TO",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
cli.StringFlag{
|
||||
Name: "message",
|
||||
Usage: "send telegram message",
|
||||
EnvVar: "PLUGIN_MESSAGE,TELEGRAM_MESSAGE,INPUT_MESSAGE",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "message.file",
|
||||
Usage: "send telegram message from file",
|
||||
EnvVar: "PLUGIN_MESSAGE_FILE,TELEGRAM_MESSAGE_FILE,INPUT_MESSAGE_FILE",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "template.vars",
|
||||
Usage: "additional template vars to be used in message, as JSON string",
|
||||
EnvVar: "PLUGIN_TEMPLATE_VARS,TELEGRAM_TEMPLATE_VARS,INPUT_TEMPLATE_VARS",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "photo",
|
||||
Usage: "send photo message",
|
||||
@@ -93,7 +107,7 @@ func main() {
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "format",
|
||||
Value: "markdown",
|
||||
Value: formatMarkdown,
|
||||
Usage: "telegram message format",
|
||||
EnvVar: "PLUGIN_FORMAT,FORMAT,INPUT_FORMAT",
|
||||
},
|
||||
@@ -188,17 +202,12 @@ func main() {
|
||||
cli.Float64Flag{
|
||||
Name: "job.started",
|
||||
Usage: "job started",
|
||||
EnvVar: "DRONE_JOB_STARTED",
|
||||
EnvVar: "DRONE_BUILD_STARTED",
|
||||
},
|
||||
cli.Float64Flag{
|
||||
Name: "job.finished",
|
||||
Usage: "job finished",
|
||||
EnvVar: "DRONE_JOB_FINISHED",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "env-file",
|
||||
Usage: "source env file",
|
||||
EnvVar: "ENV_FILE",
|
||||
EnvVar: "DRONE_BUILD_FINISHED",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "github",
|
||||
@@ -235,6 +244,15 @@ func main() {
|
||||
Usage: "Provides the target deployment environment for the running build. This value is only available to promotion and rollback pipelines.",
|
||||
EnvVar: "DRONE_DEPLOY_TO",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "socks5",
|
||||
Usage: "Socks5 proxy URL",
|
||||
EnvVar: "PLUGIN_SOCKS5,SOCKS5,INPUT_SOCKS5",
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := os.Stat("/run/drone/env"); err == nil {
|
||||
godotenv.Overload("/run/drone/env")
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
@@ -243,10 +261,6 @@ func main() {
|
||||
}
|
||||
|
||||
func run(c *cli.Context) error {
|
||||
if c.String("env-file") != "" {
|
||||
_ = godotenv.Load(c.String("env-file"))
|
||||
}
|
||||
|
||||
plugin := Plugin{
|
||||
GitHub: GitHub{
|
||||
Workflow: c.String("github.workflow"),
|
||||
@@ -282,22 +296,25 @@ func run(c *cli.Context) error {
|
||||
DeployTo: c.String("deploy.to"),
|
||||
},
|
||||
Config: Config{
|
||||
Token: c.String("token"),
|
||||
Debug: c.Bool("debug"),
|
||||
MatchEmail: c.Bool("match.email"),
|
||||
WebPreview: c.Bool("webpage.preview"),
|
||||
To: c.StringSlice("to"),
|
||||
Message: c.StringSlice("message"),
|
||||
Photo: c.StringSlice("photo"),
|
||||
Document: c.StringSlice("document"),
|
||||
Sticker: c.StringSlice("sticker"),
|
||||
Audio: c.StringSlice("audio"),
|
||||
Voice: c.StringSlice("voice"),
|
||||
Location: c.StringSlice("location"),
|
||||
Video: c.StringSlice("video"),
|
||||
Venue: c.StringSlice("venue"),
|
||||
Format: c.String("format"),
|
||||
GitHub: c.Bool("github"),
|
||||
Token: c.String("token"),
|
||||
Debug: c.Bool("debug"),
|
||||
MatchEmail: c.Bool("match.email"),
|
||||
WebPreview: c.Bool("webpage.preview"),
|
||||
To: c.StringSlice("to"),
|
||||
Message: c.String("message"),
|
||||
MessageFile: c.String("message.file"),
|
||||
TemplateVars: c.String("template.vars"),
|
||||
Photo: c.StringSlice("photo"),
|
||||
Document: c.StringSlice("document"),
|
||||
Sticker: c.StringSlice("sticker"),
|
||||
Audio: c.StringSlice("audio"),
|
||||
Voice: c.StringSlice("voice"),
|
||||
Location: c.StringSlice("location"),
|
||||
Video: c.StringSlice("video"),
|
||||
Venue: c.StringSlice("venue"),
|
||||
Format: c.String("format"),
|
||||
GitHub: c.Bool("github"),
|
||||
Socks5: c.String("socks5"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -12,6 +19,10 @@ import (
|
||||
tgbotapi "gopkg.in/telegram-bot-api.v4"
|
||||
)
|
||||
|
||||
const (
|
||||
formatMarkdown = "markdown"
|
||||
)
|
||||
|
||||
type (
|
||||
// GitHub information.
|
||||
GitHub struct {
|
||||
@@ -56,22 +67,25 @@ type (
|
||||
|
||||
// Config for the plugin.
|
||||
Config struct {
|
||||
Token string
|
||||
Debug bool
|
||||
MatchEmail bool
|
||||
WebPreview bool
|
||||
To []string
|
||||
Message []string
|
||||
Photo []string
|
||||
Document []string
|
||||
Sticker []string
|
||||
Audio []string
|
||||
Voice []string
|
||||
Location []string
|
||||
Video []string
|
||||
Venue []string
|
||||
Format string
|
||||
GitHub bool
|
||||
Token string
|
||||
Debug bool
|
||||
MatchEmail bool
|
||||
WebPreview bool
|
||||
To []string
|
||||
Message string
|
||||
MessageFile string
|
||||
TemplateVars string
|
||||
Photo []string
|
||||
Document []string
|
||||
Sticker []string
|
||||
Audio []string
|
||||
Voice []string
|
||||
Location []string
|
||||
Video []string
|
||||
Venue []string
|
||||
Format string
|
||||
GitHub bool
|
||||
Socks5 string
|
||||
}
|
||||
|
||||
// Plugin values.
|
||||
@@ -81,6 +95,7 @@ type (
|
||||
Commit Commit
|
||||
Build Build
|
||||
Config Config
|
||||
Tpl map[string]string
|
||||
}
|
||||
|
||||
// Location format
|
||||
@@ -92,6 +107,12 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
var icons = map[string]string{
|
||||
"failure": "❌",
|
||||
"cancelled": "❕",
|
||||
"success": "✅",
|
||||
}
|
||||
|
||||
func trimElement(keys []string) []string {
|
||||
var newKeys []string
|
||||
|
||||
@@ -184,6 +205,20 @@ func convertLocation(value string) (Location, bool) {
|
||||
}, false
|
||||
}
|
||||
|
||||
func loadTextFromFile(filename string) ([]string, error) {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
r := bufio.NewReader(f)
|
||||
content, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []string{string(content)}, nil
|
||||
}
|
||||
|
||||
func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
|
||||
var emails []int64
|
||||
var ids []int64
|
||||
@@ -222,20 +257,44 @@ func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
|
||||
}
|
||||
|
||||
// Exec executes the plugin.
|
||||
func (p Plugin) Exec() error {
|
||||
|
||||
func (p Plugin) Exec() (err error) {
|
||||
if len(p.Config.Token) == 0 || len(p.Config.To) == 0 {
|
||||
return errors.New("missing telegram token or user list")
|
||||
}
|
||||
|
||||
var message []string
|
||||
if len(p.Config.Message) > 0 {
|
||||
message = p.Config.Message
|
||||
} else {
|
||||
switch {
|
||||
case len(p.Config.MessageFile) > 0:
|
||||
message, err = loadTextFromFile(p.Config.MessageFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error loading message file '%s': %v", p.Config.MessageFile, err)
|
||||
}
|
||||
case len(p.Config.Message) > 0:
|
||||
message = []string{p.Config.Message}
|
||||
default:
|
||||
p.Config.Format = formatMarkdown
|
||||
message = p.Message()
|
||||
}
|
||||
|
||||
bot, err := tgbotapi.NewBotAPI(p.Config.Token)
|
||||
if p.Config.TemplateVars != "" {
|
||||
p.Tpl = make(map[string]string)
|
||||
if err = json.Unmarshal([]byte(p.Config.TemplateVars), &p.Tpl); err != nil {
|
||||
return fmt.Errorf("unable to unmarshall template vars from JSON string '%s': %v", p.Config.TemplateVars, err)
|
||||
}
|
||||
}
|
||||
|
||||
var proxyURL *url.URL
|
||||
if proxyURL, err = url.Parse(p.Config.Socks5); err != nil {
|
||||
return fmt.Errorf("unable to unmarshall socks5 proxy url from string '%s': %v", p.Config.Socks5, err)
|
||||
}
|
||||
|
||||
var bot *tgbotapi.BotAPI
|
||||
if len(p.Config.Socks5) > 0 {
|
||||
proxyClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
|
||||
bot, err = tgbotapi.NewBotAPIWithClient(p.Config.Token, proxyClient)
|
||||
} else {
|
||||
bot, err = tgbotapi.NewBotAPI(p.Config.Token)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -255,7 +314,7 @@ func (p Plugin) Exec() error {
|
||||
|
||||
message = trimElement(message)
|
||||
|
||||
if p.Config.Format == "markdown" {
|
||||
if p.Config.Format == formatMarkdown {
|
||||
message = escapeMarkdown(message)
|
||||
|
||||
p.Commit.Message = escapeMarkdownOne(p.Commit.Message)
|
||||
@@ -280,6 +339,8 @@ func (p Plugin) Exec() error {
|
||||
return err
|
||||
}
|
||||
|
||||
txt = html.UnescapeString(txt)
|
||||
|
||||
msg := tgbotapi.NewMessage(user, txt)
|
||||
msg.ParseMode = p.Config.Format
|
||||
msg.DisableWebPagePreview = !p.Config.WebPreview
|
||||
@@ -381,6 +442,8 @@ func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) error {
|
||||
|
||||
// Message is plugin default message.
|
||||
func (p Plugin) Message() []string {
|
||||
icon := icons[strings.ToLower(p.Build.Status)]
|
||||
|
||||
if p.Config.GitHub {
|
||||
return []string{fmt.Sprintf("%s/%s triggered by %s (%s)",
|
||||
p.Repo.FullName,
|
||||
@@ -390,11 +453,20 @@ func (p Plugin) Message() []string {
|
||||
)}
|
||||
}
|
||||
|
||||
return []string{fmt.Sprintf("[%s] <%s> (%s)『%s』by %s",
|
||||
// ✅ Build #106 of drone-telegram succeeded.
|
||||
//
|
||||
// 📝 Commit by appleboy on master:
|
||||
// chore: update default template
|
||||
//
|
||||
// 🌐 https://cloud.drone.io/appleboy/drone-telegram/106
|
||||
return []string{fmt.Sprintf("%s Build #%d of `%s` %s.\n\n📝 Commit by %s on `%s`:\n``` %s ```\n\n🌐 %s",
|
||||
icon,
|
||||
p.Build.Number,
|
||||
p.Repo.FullName,
|
||||
p.Build.Status,
|
||||
p.Build.Link,
|
||||
p.Commit.Author,
|
||||
p.Commit.Branch,
|
||||
p.Commit.Message,
|
||||
p.Commit.Author,
|
||||
p.Build.Link,
|
||||
)}
|
||||
}
|
||||
|
||||
+100
-6
@@ -30,6 +30,7 @@ func TestMissingUserConfig(t *testing.T) {
|
||||
func TestDefaultMessageFormat(t *testing.T) {
|
||||
plugin := Plugin{
|
||||
Repo: Repo{
|
||||
FullName: "appleboy/go-hello",
|
||||
Name: "go-hello",
|
||||
Namespace: "appleboy",
|
||||
},
|
||||
@@ -48,7 +49,7 @@ func TestDefaultMessageFormat(t *testing.T) {
|
||||
|
||||
message := plugin.Message()
|
||||
|
||||
assert.Equal(t, []string{"[success] <https://github.com/appleboy/go-hello> (master)『update travis』by Bo-Yi Wu"}, message)
|
||||
assert.Equal(t, []string{"✅ Build #101 of `appleboy/go-hello` success.\n\n📝 Commit by Bo-Yi Wu on `master`:\n``` update travis ```\n\n🌐 https://github.com/appleboy/go-hello"}, message)
|
||||
}
|
||||
|
||||
func TestDefaultMessageFormatFromGitHub(t *testing.T) {
|
||||
@@ -96,7 +97,7 @@ func TestSendMessage(t *testing.T) {
|
||||
Config: Config{
|
||||
Token: os.Getenv("TELEGRAM_TOKEN"),
|
||||
To: []string{os.Getenv("TELEGRAM_TO"), os.Getenv("TELEGRAM_TO") + ":appleboy@gmail.com", "中文ID", "1234567890"},
|
||||
Message: []string{"Test Telegram Chat Bot From Travis or Local", "commit message: 『{{ build.message }}』", " "},
|
||||
Message: "Test Telegram Chat Bot From Travis or Local, commit message: 『{{ build.message }}』",
|
||||
Photo: []string{"tests/github.png", "1234", " "},
|
||||
Document: []string{"tests/gophercolor.png", "1234", " "},
|
||||
Sticker: []string{"tests/github-logo.png", "tests/github.png", "1234", " "},
|
||||
@@ -112,13 +113,13 @@ func TestSendMessage(t *testing.T) {
|
||||
err := plugin.Exec()
|
||||
assert.NotNil(t, err)
|
||||
|
||||
plugin.Config.Format = "markdown"
|
||||
plugin.Config.Message = []string{"Test escape under_score"}
|
||||
plugin.Config.Format = formatMarkdown
|
||||
plugin.Config.Message = "Test escape under_score"
|
||||
err = plugin.Exec()
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// disable message
|
||||
plugin.Config.Message = []string{}
|
||||
plugin.Config.Message = ""
|
||||
err = plugin.Exec()
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
@@ -144,7 +145,7 @@ func TestBotError(t *testing.T) {
|
||||
Config: Config{
|
||||
Token: "appleboy",
|
||||
To: []string{os.Getenv("TELEGRAM_TO"), "中文ID", "1234567890"},
|
||||
Message: []string{"Test Telegram Chat Bot From Travis or Local", " "},
|
||||
Message: "Test Telegram Chat Bot From Travis or Local",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -325,3 +326,96 @@ func TestHTMLMessage(t *testing.T) {
|
||||
err := plugin.Exec()
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestMessageFile(t *testing.T) {
|
||||
plugin := Plugin{
|
||||
Repo: Repo{
|
||||
Name: "go-hello",
|
||||
Namespace: "appleboy",
|
||||
},
|
||||
Commit: Commit{
|
||||
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
|
||||
Author: "Bo-Yi Wu",
|
||||
Branch: "master",
|
||||
Message: "Freakin' macOS isn't fully case-sensitive..",
|
||||
},
|
||||
Build: Build{
|
||||
Number: 101,
|
||||
Status: "success",
|
||||
Link: "https://github.com/appleboy/go-hello",
|
||||
},
|
||||
|
||||
Config: Config{
|
||||
Token: os.Getenv("TELEGRAM_TOKEN"),
|
||||
To: []string{os.Getenv("TELEGRAM_TO")},
|
||||
MessageFile: "tests/message.txt",
|
||||
},
|
||||
}
|
||||
|
||||
err := plugin.Exec()
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestTemplateVars(t *testing.T) {
|
||||
plugin := Plugin{
|
||||
Repo: Repo{
|
||||
Name: "go-hello",
|
||||
Namespace: "appleboy",
|
||||
},
|
||||
Commit: Commit{
|
||||
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
|
||||
Author: "Bo-Yi Wu",
|
||||
Branch: "master",
|
||||
Message: "This is a test commit msg",
|
||||
},
|
||||
Build: Build{
|
||||
Number: 101,
|
||||
Status: "success",
|
||||
Link: "https://github.com/appleboy/go-hello",
|
||||
},
|
||||
|
||||
Config: Config{
|
||||
Token: os.Getenv("TELEGRAM_TOKEN"),
|
||||
To: []string{os.Getenv("TELEGRAM_TO")},
|
||||
Format: formatMarkdown,
|
||||
MessageFile: "tests/message_template.txt",
|
||||
TemplateVars: `{"env":"testing","version":"1.2.0-SNAPSHOT"}`,
|
||||
},
|
||||
}
|
||||
|
||||
err := plugin.Exec()
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestProxySendMessage(t *testing.T) {
|
||||
plugin := Plugin{
|
||||
Repo: Repo{
|
||||
Name: "go-hello",
|
||||
Namespace: "appleboy",
|
||||
},
|
||||
Commit: Commit{
|
||||
Sha: "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
|
||||
Author: "Bo-Yi Wu",
|
||||
Branch: "master",
|
||||
Message: "start use proxy",
|
||||
Email: "test@gmail.com",
|
||||
},
|
||||
Build: Build{
|
||||
Tag: "1.0.0",
|
||||
Number: 101,
|
||||
Status: "success",
|
||||
Link: "https://github.com/appleboy/go-hello",
|
||||
},
|
||||
|
||||
Config: Config{
|
||||
Token: os.Getenv("TELEGRAM_TOKEN"),
|
||||
To: []string{os.Getenv("TELEGRAM_TO")},
|
||||
Message: "Send message from socks5 proxy URL.",
|
||||
Debug: false,
|
||||
Socks5: os.Getenv("SOCKS5"),
|
||||
},
|
||||
}
|
||||
|
||||
err := plugin.Exec()
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
Sample message loaded from file.
|
||||
|
||||
Commit msg: {{commit.message}}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
Sample message template loaded from file.
|
||||
|
||||
*Environ:* {{tpl.env}}
|
||||
*Version:* {{tpl.version}}
|
||||
|
||||
Commit msg: {{commit.message}}
|
||||
|
||||
Reference in New Issue
Block a user