Repository: appleboy/drone-telegram (go)

Issue:
Message as auxiliary file
Hi @appleboy 

the plugin is awesome, I really like it!

The only thing that bothers me (a little) is that I cannot configure the message in a side file.

It would be incredible if one is able to do the following:

Let's say: in `.drone-telegram-message.md`

```markdown
{{#success build.status}}
... here goes md content
{{else}}
... here goes md content
{{/success}}
```

Pros:
- file is supported by IDE by simply following its extension / file type
- no need to encode special yaml characters such as `#`, `:` etc.
- more readable .drone.yml

And then the config would look as

```yml
    name: telegram
    depends_on: [dockerize]
    image: appleboy/drone-telegram
    when:
      status: [success, failure]
    settings:
      token:
        from_secret: telegram_token
      to:
        from_secret: telegram_to
      format: markdown
      messageFile: .drone-telegram-message.md
```

What do you think?

Pull request:
Add support for auxiliary message file
Fixes #71

This PR adds support for specifying a file name from which the message is loaded. For backwards compatibility, if both a message and a message file are specified, the message from file is preferred.

Patch:
diff --git a/DOCS.md b/DOCS.md
index 6d6c6c0..c0c6afa 100644
--- a/DOCS.md
+++ b/DOCS.md
@@ -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,142 @@ 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
+  - 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
   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}}
++   message_file: message_file.tpl
 ```
 
 ## Parameter Reference
@@ -150,6 +170,9 @@ to
 message
 : overwrite the default message template
 
+message_file
+: overwrite the default message template with the contents of the specified file
+
 photo
 : local file path
 
diff --git a/README.md b/README.md
index 642dfd8..d2b986f 100644
--- a/README.md
+++ b/README.md
@@ -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 \
diff --git a/main.go b/main.go
index b048c44..b234eed 100644
--- a/main.go
+++ b/main.go
@@ -36,6 +36,11 @@ func main() {
 			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.StringSliceFlag{
 			Name:   "photo",
 			Usage:  "send photo message",
@@ -282,22 +287,23 @@ 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.StringSlice("message"),
+			MessageFile: c.String("message.file"),
+			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"),
 		},
 	}
 
diff --git a/plugin.go b/plugin.go
index 60c21db..44b1a64 100644
--- a/plugin.go
+++ b/plugin.go
@@ -1,9 +1,12 @@
 package main
 
 import (
+	"bufio"
 	"errors"
 	"fmt"
+	"io/ioutil"
 	"log"
+	"os"
 	"path/filepath"
 	"strconv"
 	"strings"
@@ -56,22 +59,23 @@ 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
+		Photo       []string
+		Document    []string
+		Sticker     []string
+		Audio       []string
+		Voice       []string
+		Location    []string
+		Video       []string
+		Venue       []string
+		Format      string
+		GitHub      bool
 	}
 
 	// Plugin values.
@@ -184,6 +188,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,22 +240,26 @@ 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 {
+	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
-	} else {
+	default:
 		message = p.Message()
 	}
 
-	bot, err := tgbotapi.NewBotAPI(p.Config.Token)
-
-	if err != nil {
+	var bot *tgbotapi.BotAPI
+	if bot, err = tgbotapi.NewBotAPI(p.Config.Token); err != nil {
 		return err
 	}
 


Test patch:
diff --git a/plugin_test.go b/plugin_test.go
index 56d7684..23aeb3c 100644
--- a/plugin_test.go
+++ b/plugin_test.go
@@ -325,3 +325,32 @@ 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/.test.message",
+		},
+	}
+
+	err := plugin.Exec()
+	assert.Nil(t, err)
+}
diff --git a/tests/.test.message b/tests/.test.message
new file mode 100644
index 0000000..e7827a8
--- /dev/null
+++ b/tests/.test.message
@@ -0,0 +1,4 @@
+Sample message loaded from file.
+
+Commit msg:  {{commit.message}}
+