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:
parent
b7f97df101
commit
ace08f12b9
2
main.go
2
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",
|
||||
},
|
||||
|
@ -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
|
||||
|
@ -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"}`,
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user