Add support for auxiliary message file (#81)
* Add support for external message loaded from the file specified in Config.MessageFile * Enable the message file option in command line/env vars * Update docs * Add a testcase * Fix YAML tag in DOCS.md from 'messageFile' to 'message_file', as is commonly used in Drone * Remove unintended blank line * Move .test.message file to tests/ folder
This commit is contained in:
parent
75ffa6ef7b
commit
f8f311ba1f
13
DOCS.md
13
DOCS.md
@ -149,6 +149,16 @@ Example configuration with a custom message template:
|
|||||||
+ {{/success}}
|
+ {{/success}}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Example configuration with a custom message template loaded from file:
|
||||||
|
|
||||||
|
```diff
|
||||||
|
image: appleboy/drone-telegram
|
||||||
|
settings:
|
||||||
|
token: xxxxxxxxxx
|
||||||
|
to: telegram_user_id
|
||||||
|
+ message_file: message_file.tpl
|
||||||
|
```
|
||||||
|
|
||||||
## Parameter Reference
|
## Parameter Reference
|
||||||
|
|
||||||
token
|
token
|
||||||
@ -160,6 +170,9 @@ to
|
|||||||
message
|
message
|
||||||
: overwrite the default message template
|
: overwrite the default message template
|
||||||
|
|
||||||
|
message_file
|
||||||
|
: overwrite the default message template with the contents of the specified file
|
||||||
|
|
||||||
photo
|
photo
|
||||||
: local file path
|
: local file path
|
||||||
|
|
||||||
|
@ -69,6 +69,7 @@ docker run --rm \
|
|||||||
-e PLUGIN_TOKEN=xxxxxxx \
|
-e PLUGIN_TOKEN=xxxxxxx \
|
||||||
-e PLUGIN_TO=xxxxxxx \
|
-e PLUGIN_TO=xxxxxxx \
|
||||||
-e PLUGIN_MESSAGE=test \
|
-e PLUGIN_MESSAGE=test \
|
||||||
|
-e PLUGIN_MESSAGE_FILE=testmessage.md \
|
||||||
-e PLUGIN_PHOTO=tests/github.png \
|
-e PLUGIN_PHOTO=tests/github.png \
|
||||||
-e PLUGIN_DOCUMENT=tests/gophercolor.png \
|
-e PLUGIN_DOCUMENT=tests/gophercolor.png \
|
||||||
-e PLUGIN_STICKER=tests/github-logo.png \
|
-e PLUGIN_STICKER=tests/github-logo.png \
|
||||||
|
6
main.go
6
main.go
@ -36,6 +36,11 @@ func main() {
|
|||||||
Usage: "send telegram message",
|
Usage: "send telegram message",
|
||||||
EnvVar: "PLUGIN_MESSAGE,TELEGRAM_MESSAGE,INPUT_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.StringSliceFlag{
|
cli.StringSliceFlag{
|
||||||
Name: "photo",
|
Name: "photo",
|
||||||
Usage: "send photo message",
|
Usage: "send photo message",
|
||||||
@ -288,6 +293,7 @@ func run(c *cli.Context) error {
|
|||||||
WebPreview: c.Bool("webpage.preview"),
|
WebPreview: c.Bool("webpage.preview"),
|
||||||
To: c.StringSlice("to"),
|
To: c.StringSlice("to"),
|
||||||
Message: c.StringSlice("message"),
|
Message: c.StringSlice("message"),
|
||||||
|
MessageFile: c.String("message.file"),
|
||||||
Photo: c.StringSlice("photo"),
|
Photo: c.StringSlice("photo"),
|
||||||
Document: c.StringSlice("document"),
|
Document: c.StringSlice("document"),
|
||||||
Sticker: c.StringSlice("sticker"),
|
Sticker: c.StringSlice("sticker"),
|
||||||
|
36
plugin.go
36
plugin.go
@ -1,9 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -62,6 +65,7 @@ type (
|
|||||||
WebPreview bool
|
WebPreview bool
|
||||||
To []string
|
To []string
|
||||||
Message []string
|
Message []string
|
||||||
|
MessageFile string
|
||||||
Photo []string
|
Photo []string
|
||||||
Document []string
|
Document []string
|
||||||
Sticker []string
|
Sticker []string
|
||||||
@ -184,6 +188,20 @@ func convertLocation(value string) (Location, bool) {
|
|||||||
}, false
|
}, 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 {
|
func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
|
||||||
var emails []int64
|
var emails []int64
|
||||||
var ids []int64
|
var ids []int64
|
||||||
@ -222,22 +240,26 @@ func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Exec executes the plugin.
|
// 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 {
|
if len(p.Config.Token) == 0 || len(p.Config.To) == 0 {
|
||||||
return errors.New("missing telegram token or user list")
|
return errors.New("missing telegram token or user list")
|
||||||
}
|
}
|
||||||
|
|
||||||
var message []string
|
var message []string
|
||||||
if len(p.Config.Message) > 0 {
|
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 = p.Config.Message
|
message = p.Config.Message
|
||||||
} else {
|
default:
|
||||||
message = p.Message()
|
message = p.Message()
|
||||||
}
|
}
|
||||||
|
|
||||||
bot, err := tgbotapi.NewBotAPI(p.Config.Token)
|
var bot *tgbotapi.BotAPI
|
||||||
|
if bot, err = tgbotapi.NewBotAPI(p.Config.Token); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -325,3 +325,32 @@ func TestHTMLMessage(t *testing.T) {
|
|||||||
err := plugin.Exec()
|
err := plugin.Exec()
|
||||||
assert.Nil(t, err)
|
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/.test.message",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := plugin.Exec()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
4
tests/.test.message
Normal file
4
tests/.test.message
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
Sample message loaded from file.
|
||||||
|
|
||||||
|
Commit msg: {{commit.message}}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user