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

View File

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

View File

@ -124,6 +124,30 @@ func TestSendMessage(t *testing.T) {
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) {
plugin := Plugin{
Repo: Repo{