From ace08f12b9aa6e7103db857121787bb02b55fd41 Mon Sep 17 00:00:00 2001 From: "Daniel M. Lambea" Date: Wed, 30 Oct 2019 11:50:31 +0000 Subject: [PATCH] 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 --- main.go | 2 +- plugin.go | 9 ++++++++- plugin_test.go | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index ca09b9e..72728ba 100644 --- a/main.go +++ b/main.go @@ -103,7 +103,7 @@ func main() { }, cli.StringFlag{ Name: "format", - Value: "markdown", + Value: formatMarkdown, Usage: "telegram message format", EnvVar: "PLUGIN_FORMAT,FORMAT,INPUT_FORMAT", }, diff --git a/plugin.go b/plugin.go index f43977e..6c5bf9c 100644 --- a/plugin.go +++ b/plugin.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "html" "io/ioutil" "log" "os" @@ -16,6 +17,10 @@ import ( tgbotapi "gopkg.in/telegram-bot-api.v4" ) +const ( + formatMarkdown = "markdown" +) + type ( // GitHub information. GitHub struct { @@ -287,7 +292,7 @@ func (p Plugin) Exec() (err error) { message = trimElement(message) - if p.Config.Format == "markdown" { + if p.Config.Format == formatMarkdown { message = escapeMarkdown(message) p.Commit.Message = escapeMarkdownOne(p.Commit.Message) @@ -312,6 +317,8 @@ func (p Plugin) Exec() (err error) { return err } + txt = html.UnescapeString(txt) + msg := tgbotapi.NewMessage(user, txt) msg.ParseMode = p.Config.Format msg.DisableWebPagePreview = !p.Config.WebPreview diff --git a/plugin_test.go b/plugin_test.go index e474c93..943a40d 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -112,7 +112,7 @@ func TestSendMessage(t *testing.T) { err := plugin.Exec() assert.NotNil(t, err) - plugin.Config.Format = "markdown" + plugin.Config.Format = formatMarkdown plugin.Config.Message = []string{"Test escape under_score"} err = plugin.Exec() assert.NotNil(t, err) @@ -376,7 +376,7 @@ func TestTemplateVars(t *testing.T) { Config: Config{ Token: os.Getenv("TELEGRAM_TOKEN"), To: []string{os.Getenv("TELEGRAM_TO")}, - Format: "markdown", + Format: formatMarkdown, MessageFile: "tests/message_template.txt", TemplateVars: `{"env":"testing","version":"1.2.0-SNAPSHOT"}`, },