support handlebar template

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-10-31 09:57:34 +08:00
parent 693f15783c
commit e2fc930461
5 changed files with 55 additions and 21 deletions

16
glide.lock generated
View File

@ -1,6 +1,16 @@
hash: b63ca4eb6e194481209bad2e58f3c4eb2c0ef2b4411ead5b3b7a77677ef815ab hash: 5bdcf53533d043ba1cb6ab55ae6a93aca40c812a20264406bd6e0fe342f51d61
updated: 2016-10-04T21:46:25.713693812+08:00 updated: 2016-10-31T09:50:34.654875319+08:00
imports: imports:
- name: github.com/appleboy/drone-facebook
version: abda8124c404d424b150cf2a9935e3e108519c6e
subpackages:
- template
- name: github.com/aymerick/raymond
version: a2232af10b53ef1ae5a767f5178db3a6c1dab655
subpackages:
- ast
- parser
- lexer
- name: github.com/joho/godotenv - name: github.com/joho/godotenv
version: 4ed13390c0acd2ff4e371e64d8b97c8954138243 version: 4ed13390c0acd2ff4e371e64d8b97c8954138243
subpackages: subpackages:
@ -8,7 +18,7 @@ imports:
- name: github.com/technoweenie/multipartstreamer - name: github.com/technoweenie/multipartstreamer
version: a90a01d73ae432e2611d178c18367fbaa13e0154 version: a90a01d73ae432e2611d178c18367fbaa13e0154
- name: github.com/urfave/cli - name: github.com/urfave/cli
version: d53eb991652b1d438abdd34ce4bfa3ef1539108e version: 3eb41f1bb122ac7535cc7b32db6b78b541879726
- name: gopkg.in/telegram-bot-api.v4 - name: gopkg.in/telegram-bot-api.v4
version: a7f48eb2dd301356942677e65bebe0c9aef07013 version: a7f48eb2dd301356942677e65bebe0c9aef07013
testImports: testImports:

View File

@ -5,6 +5,10 @@ import:
- autoload - autoload
- package: github.com/urfave/cli - package: github.com/urfave/cli
- package: gopkg.in/telegram-bot-api.v4 - package: gopkg.in/telegram-bot-api.v4
- package: github.com/appleboy/drone-facebook
version: ^1.3.0
subpackages:
- template
testImport: testImport:
- package: github.com/stretchr/testify - package: github.com/stretchr/testify
subpackages: subpackages:

12
main.go
View File

@ -136,6 +136,16 @@ func main() {
Usage: "build link", Usage: "build link",
EnvVar: "DRONE_BUILD_LINK", EnvVar: "DRONE_BUILD_LINK",
}, },
cli.Float64Flag{
Name: "job.started",
Usage: "job started",
EnvVar: "DRONE_JOB_STARTED",
},
cli.Float64Flag{
Name: "job.finished",
Usage: "job finished",
EnvVar: "DRONE_JOB_FINISHED",
},
} }
app.Run(os.Args) app.Run(os.Args)
} }
@ -155,6 +165,8 @@ func run(c *cli.Context) error {
Author: c.String("commit.author"), Author: c.String("commit.author"),
Message: c.String("commit.message"), Message: c.String("commit.message"),
Link: c.String("build.link"), Link: c.String("build.link"),
Started: c.Float64("job.started"),
Finished: c.Float64("job.finished"),
}, },
Config: Config{ Config: Config{
Token: c.String("token"), Token: c.String("token"),

View File

@ -8,6 +8,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/appleboy/drone-facebook/template"
"gopkg.in/telegram-bot-api.v4" "gopkg.in/telegram-bot-api.v4"
) )
@ -28,6 +29,8 @@ type (
Author string Author string
Status string Status string
Link string Link string
Started float64
Finished float64
} }
// Config for the plugin. // Config for the plugin.
@ -187,7 +190,12 @@ func (p Plugin) Exec() error {
// 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) txt, err := template.RenderTrim(value, p)
if err != nil {
return err
}
msg := tgbotapi.NewMessage(user, txt)
msg.ParseMode = p.Config.Format msg.ParseMode = p.Config.Format
p.Send(bot, msg) p.Send(bot, msg)
} }

View File

@ -68,7 +68,7 @@ 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", "commit message: 『{{ build.message }}』", " "},
Photo: []string{"tests/github.png", "1234", " "}, Photo: []string{"tests/github.png", "1234", " "},
Document: []string{"tests/gophercolor.png", "1234", " "}, Document: []string{"tests/gophercolor.png", "1234", " "},
Sticker: []string{"tests/github-logo.png", "tests/github.png", "1234", " "}, Sticker: []string{"tests/github-logo.png", "tests/github.png", "1234", " "},