1
0
telegram/utils_entity.go

103 lines
2.6 KiB
Go
Raw Normal View History

2017-10-17 11:08:09 +00:00
package telegram
import (
"fmt"
"net/url"
2018-08-15 13:26:07 +00:00
"strings"
)
2017-10-17 11:08:09 +00:00
2018-04-19 13:02:15 +00:00
// ParseURL selects URL from entered text of message and parse it as url.URL.
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) ParseURL(messageText string) *url.URL {
2018-08-15 13:26:07 +00:00
if e == nil ||
!e.IsURL() ||
strings.EqualFold(messageText, "") {
return nil
}
2018-08-15 11:05:58 +00:00
from := e.Offset
to := from + e.Length
text := []rune(messageText)
if len(text) < to {
return nil
}
link, err := url.Parse(string(text[from:to]))
2018-08-15 13:26:07 +00:00
if err == nil && strings.EqualFold(link.Scheme, "") {
link, err = url.Parse(fmt.Sprint("http://", link))
}
if err != nil {
return nil
2017-10-17 11:08:09 +00:00
}
return link
2017-10-17 11:08:09 +00:00
}
2018-04-19 13:02:15 +00:00
// IsBold checks that the current entity is a bold tag.
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) IsBold() bool {
2018-08-15 13:26:07 +00:00
return e != nil && strings.EqualFold(e.Type, EntityBold)
}
2018-04-19 13:02:15 +00:00
// IsBotCommand checks that the current entity is a bot command.
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) IsBotCommand() bool {
2018-08-15 13:26:07 +00:00
return e != nil && strings.EqualFold(e.Type, EntityBotCommand)
}
2018-04-19 13:02:15 +00:00
// IsCode checks that the current entity is a code tag.
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) IsCode() bool {
2018-08-15 13:26:07 +00:00
return e != nil && strings.EqualFold(e.Type, EntityCode)
}
2018-04-19 13:02:15 +00:00
// IsEmail checks that the current entity is a email.
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) IsEmail() bool {
2018-08-15 13:26:07 +00:00
return e != nil && strings.EqualFold(e.Type, EntityEmail)
}
2018-04-19 13:02:15 +00:00
// IsHashtag checks that the current entity is a hashtag.
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) IsHashtag() bool {
2018-08-15 13:26:07 +00:00
return e != nil && strings.EqualFold(e.Type, EntityHashtag)
}
2018-04-19 13:02:15 +00:00
// IsItalic checks that the current entity is a italic tag.
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) IsItalic() bool {
2018-08-15 13:26:07 +00:00
return e != nil && strings.EqualFold(e.Type, EntityItalic)
}
2018-04-19 13:02:15 +00:00
// IsMention checks that the current entity is a username mention.
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) IsMention() bool {
2018-08-15 13:26:07 +00:00
return e != nil && strings.EqualFold(e.Type, EntityMention)
}
2018-04-19 13:02:15 +00:00
// IsPre checks that the current entity is a pre tag.
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) IsPre() bool {
2018-08-15 13:26:07 +00:00
return e != nil && strings.EqualFold(e.Type, EntityPre)
}
2018-04-19 13:02:15 +00:00
// IsTextLink checks that the current entity is a text link.
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) IsTextLink() bool {
2018-08-15 13:26:07 +00:00
return e != nil && strings.EqualFold(e.Type, EntityTextLink)
}
2018-04-19 13:02:15 +00:00
// IsTextMention checks that the current entity is a mention without username.
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) IsTextMention() bool {
2018-08-15 13:26:07 +00:00
return e != nil && strings.EqualFold(e.Type, EntityTextMention)
}
2018-04-19 13:02:15 +00:00
// IsURL checks that the current entity is a URL
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) IsURL() bool {
2018-08-15 13:26:07 +00:00
return e != nil && strings.EqualFold(e.Type, EntityURL)
}
2018-04-19 13:02:15 +00:00
// TextLink parse current text link entity as url.URL.
2018-08-15 11:05:58 +00:00
func (e *MessageEntity) TextLink() *url.URL {
if e == nil {
return nil
}
2018-08-15 11:05:58 +00:00
link, err := url.Parse(e.URL)
if err != nil {
return nil
}
return link
}