refactor: DisableWebPagePreview

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2020-10-03 09:20:17 +08:00
parent 737043aa8d
commit dc17cf7288
3 changed files with 33 additions and 7 deletions

11
main.go
View File

@ -109,10 +109,10 @@ func main() {
Usage: "send message when only match email", Usage: "send message when only match email",
EnvVar: "PLUGIN_ONLY_MATCH_EMAIL,INPUT_ONLY_MATCH_EMAIL", EnvVar: "PLUGIN_ONLY_MATCH_EMAIL,INPUT_ONLY_MATCH_EMAIL",
}, },
cli.BoolTFlag{ cli.BoolFlag{
Name: "webpage.preview", Name: "disable.webpage.preview",
Usage: "toggle web-page preview", Usage: "disables link previews for links in this message",
EnvVar: "PLUGIN_WEBPAGE_PREVIEW,INPUT_WEBPAGE_PREVIEW", EnvVar: "PLUGIN_WEBPAGE_PREVIEW,INPUT_DISABLE_WEB_PAGE_PREVIEW",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "format", Name: "format",
@ -304,7 +304,6 @@ func run(c *cli.Context) error {
Token: c.String("token"), Token: c.String("token"),
Debug: c.Bool("debug"), Debug: c.Bool("debug"),
MatchEmail: c.Bool("match.email"), MatchEmail: c.Bool("match.email"),
WebPreview: c.Bool("webpage.preview"),
To: c.StringSlice("to"), To: c.StringSlice("to"),
Message: c.String("message"), Message: c.String("message"),
MessageFile: c.String("message.file"), MessageFile: c.String("message.file"),
@ -321,6 +320,8 @@ func run(c *cli.Context) error {
Format: c.String("format"), Format: c.String("format"),
GitHub: c.Bool("github"), GitHub: c.Bool("github"),
Socks5: c.String("socks5"), Socks5: c.String("socks5"),
DisableWebPagePreview: c.Bool("disable.webpage.preview"),
}, },
} }

View File

@ -70,7 +70,6 @@ type (
Token string Token string
Debug bool Debug bool
MatchEmail bool MatchEmail bool
WebPreview bool
To []string To []string
Message string Message string
MessageFile string MessageFile string
@ -87,6 +86,8 @@ type (
Format string Format string
GitHub bool GitHub bool
Socks5 string Socks5 string
DisableWebPagePreview bool
} }
// Plugin values. // Plugin values.
@ -363,7 +364,7 @@ func (p Plugin) Exec() (err error) {
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.DisableWebPagePreview
if err := p.Send(bot, msg); err != nil { if err := p.Send(bot, msg); err != nil {
return err return err
} }

View File

@ -124,6 +124,30 @@ func TestSendMessage(t *testing.T) {
assert.NotNil(t, err) assert.NotNil(t, err)
} }
func TestDisableWebPagePreviewMessage(t *testing.T) {
plugin := Plugin{
Config: Config{
Token: os.Getenv("TELEGRAM_TOKEN"),
To: []string{os.Getenv("TELEGRAM_TO")},
DisableWebPagePreview: true,
Debug: false,
},
}
err := plugin.Exec()
assert.NotNil(t, err)
plugin.Config.Format = formatMarkdown
plugin.Config.Message = "DisableWebPagePreview https://www.google.com.tw"
err = plugin.Exec()
assert.NotNil(t, err)
// disable message
plugin.Config.DisableWebPagePreview = false
err = plugin.Exec()
assert.NotNil(t, err)
}
func TestBotError(t *testing.T) { func TestBotError(t *testing.T) {
plugin := Plugin{ plugin := Plugin{
Repo: Repo{ Repo: Repo{