From fdf31c7cf4973c36780adf1ad740330657d5225b Mon Sep 17 00:00:00 2001 From: Lev Zakharov Date: Thu, 10 Jan 2019 14:40:12 +0300 Subject: [PATCH] Add type helpers to message entities. --- types.go | 58 ++++++++++++++++++++++++++++++++++--- types_test.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 4 deletions(-) diff --git a/types.go b/types.go index c6cd642..7b4308a 100644 --- a/types.go +++ b/types.go @@ -183,7 +183,7 @@ func (m *Message) IsCommand() bool { } entity := (*m.Entities)[0] - return entity.Offset == 0 && entity.Type == "bot_command" + return entity.Offset == 0 && entity.IsCommand() } // Command checks if the message was a command and if it was, returns the @@ -249,12 +249,62 @@ type MessageEntity struct { } // ParseURL attempts to parse a URL contained within a MessageEntity. -func (entity MessageEntity) ParseURL() (*url.URL, error) { - if entity.URL == "" { +func (e MessageEntity) ParseURL() (*url.URL, error) { + if e.URL == "" { return nil, errors.New(ErrBadURL) } - return url.Parse(entity.URL) + return url.Parse(e.URL) +} + +// IsMention returns true if the type of the message entity is "mention" (@username). +func (e MessageEntity) IsMention() bool { + return e.Type == "mention" +} + +// IsHashtag returns true if the type of the message entity is "hashtag". +func (e MessageEntity) IsHashtag() bool { + return e.Type == "hashtag" +} + +// IsCommand returns true if the type of the message entity is "bot_command". +func (e MessageEntity) IsCommand() bool { + return e.Type == "bot_command" +} + +// IsUrl returns true if the type of the message entity is "url". +func (e MessageEntity) IsUrl() bool { + return e.Type == "url" +} + +// IsEmail returns true if the type of the message entity is "email". +func (e MessageEntity) IsEmail() bool { + return e.Type == "email" +} + +// IsBold returns true if the type of the message entity is "bold" (bold text). +func (e MessageEntity) IsBold() bool { + return e.Type == "bold" +} + +// IsItalic returns true if the type of the message entity is "italic" (italic text). +func (e MessageEntity) IsItalic() bool { + return e.Type == "italic" +} + +// IsCode returns true if the type of the message entity is "code" (monowidth string). +func (e MessageEntity) IsCode() bool { + return e.Type == "code" +} + +// IsPre returns true if the type of the message entity is "pre" (monowidth block). +func (e MessageEntity) IsPre() bool { + return e.Type == "pre" +} + +// IsTextLink returns true if the type of the message entity is "text_link" (clickable text URL). +func (e MessageEntity) IsTextLink() bool { + return e.Type == "text_link" } // PhotoSize contains information about photos. diff --git a/types_test.go b/types_test.go index bb7bb64..a5db1d4 100644 --- a/types_test.go +++ b/types_test.go @@ -191,6 +191,86 @@ func TestChatIsSuperGroup(t *testing.T) { } } +func TestMessageEntityIsMention(t *testing.T) { + entity := tgbotapi.MessageEntity{Type: "mention"} + + if !entity.IsMention() { + t.Fail() + } +} + +func TestMessageEntityIsHashtag(t *testing.T) { + entity := tgbotapi.MessageEntity{Type: "hashtag"} + + if !entity.IsHashtag() { + t.Fail() + } +} + +func TestMessageEntityIsBotCommand(t *testing.T) { + entity := tgbotapi.MessageEntity{Type: "bot_command"} + + if !entity.IsCommand() { + t.Fail() + } +} + +func TestMessageEntityIsUrl(t *testing.T) { + entity := tgbotapi.MessageEntity{Type: "url"} + + if !entity.IsUrl() { + t.Fail() + } +} + +func TestMessageEntityIsEmail(t *testing.T) { + entity := tgbotapi.MessageEntity{Type: "email"} + + if !entity.IsEmail() { + t.Fail() + } +} + +func TestMessageEntityIsBold(t *testing.T) { + entity := tgbotapi.MessageEntity{Type: "bold"} + + if !entity.IsBold() { + t.Fail() + } +} + +func TestMessageEntityIsItalic(t *testing.T) { + entity := tgbotapi.MessageEntity{Type: "italic"} + + if !entity.IsItalic() { + t.Fail() + } +} + +func TestMessageEntityIsCode(t *testing.T) { + entity := tgbotapi.MessageEntity{Type: "code"} + + if !entity.IsCode() { + t.Fail() + } +} + +func TestMessageEntityIsPre(t *testing.T) { + entity := tgbotapi.MessageEntity{Type: "pre"} + + if !entity.IsPre() { + t.Fail() + } +} + +func TestMessageEntityIsTextLink(t *testing.T) { + entity := tgbotapi.MessageEntity{Type: "text_link"} + + if !entity.IsTextLink() { + t.Fail() + } +} + func TestFileLink(t *testing.T) { file := tgbotapi.File{FilePath: "test/test.txt"}