Merge pull request #219 from lzakharov/message-entity-helpers

Add type helpers to message entities.
bot-api-6.1
Syfaro 2019-04-24 02:31:13 -05:00 committed by GitHub
commit 2f55ae3a3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 134 additions and 4 deletions

View File

@ -184,7 +184,7 @@ func (m *Message) IsCommand() bool {
} }
entity := (*m.Entities)[0] 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 // Command checks if the message was a command and if it was, returns the
@ -250,12 +250,62 @@ type MessageEntity struct {
} }
// ParseURL attempts to parse a URL contained within a MessageEntity. // ParseURL attempts to parse a URL contained within a MessageEntity.
func (entity MessageEntity) ParseURL() (*url.URL, error) { func (e MessageEntity) ParseURL() (*url.URL, error) {
if entity.URL == "" { if e.URL == "" {
return nil, errors.New(ErrBadURL) 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. // PhotoSize contains information about photos.

View File

@ -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) { func TestFileLink(t *testing.T) {
file := tgbotapi.File{FilePath: "test/test.txt"} file := tgbotapi.File{FilePath: "test/test.txt"}