Unescape html entities (#85)

* Create a constant for 'markdown' formatting style, to reduce duplicated strings and make the code less error-prone

* Unescape HTML entities from text messages
This commit is contained in:
Daniel M. Lambea 2019-10-30 11:50:31 +00:00 committed by Bo-Yi Wu
parent b7f97df101
commit ace08f12b9
3 changed files with 11 additions and 4 deletions

View File

@ -103,7 +103,7 @@ func main() {
}, },
cli.StringFlag{ cli.StringFlag{
Name: "format", Name: "format",
Value: "markdown", Value: formatMarkdown,
Usage: "telegram message format", Usage: "telegram message format",
EnvVar: "PLUGIN_FORMAT,FORMAT,INPUT_FORMAT", EnvVar: "PLUGIN_FORMAT,FORMAT,INPUT_FORMAT",
}, },

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"html"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
@ -16,6 +17,10 @@ import (
tgbotapi "gopkg.in/telegram-bot-api.v4" tgbotapi "gopkg.in/telegram-bot-api.v4"
) )
const (
formatMarkdown = "markdown"
)
type ( type (
// GitHub information. // GitHub information.
GitHub struct { GitHub struct {
@ -287,7 +292,7 @@ func (p Plugin) Exec() (err error) {
message = trimElement(message) message = trimElement(message)
if p.Config.Format == "markdown" { if p.Config.Format == formatMarkdown {
message = escapeMarkdown(message) message = escapeMarkdown(message)
p.Commit.Message = escapeMarkdownOne(p.Commit.Message) p.Commit.Message = escapeMarkdownOne(p.Commit.Message)
@ -312,6 +317,8 @@ func (p Plugin) Exec() (err error) {
return err return err
} }
txt = html.UnescapeString(txt)
msg := tgbotapi.NewMessage(user, txt) msg := tgbotapi.NewMessage(user, txt)
msg.ParseMode = p.Config.Format msg.ParseMode = p.Config.Format
msg.DisableWebPagePreview = !p.Config.WebPreview msg.DisableWebPagePreview = !p.Config.WebPreview

View File

@ -112,7 +112,7 @@ func TestSendMessage(t *testing.T) {
err := plugin.Exec() err := plugin.Exec()
assert.NotNil(t, err) assert.NotNil(t, err)
plugin.Config.Format = "markdown" plugin.Config.Format = formatMarkdown
plugin.Config.Message = []string{"Test escape under_score"} plugin.Config.Message = []string{"Test escape under_score"}
err = plugin.Exec() err = plugin.Exec()
assert.NotNil(t, err) assert.NotNil(t, err)
@ -376,7 +376,7 @@ func TestTemplateVars(t *testing.T) {
Config: Config{ Config: Config{
Token: os.Getenv("TELEGRAM_TOKEN"), Token: os.Getenv("TELEGRAM_TOKEN"),
To: []string{os.Getenv("TELEGRAM_TO")}, To: []string{os.Getenv("TELEGRAM_TO")},
Format: "markdown", Format: formatMarkdown,
MessageFile: "tests/message_template.txt", MessageFile: "tests/message_template.txt",
TemplateVars: `{"env":"testing","version":"1.2.0-SNAPSHOT"}`, TemplateVars: `{"env":"testing","version":"1.2.0-SNAPSHOT"}`,
}, },