This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils_entity.go
99 lines (80 loc) 路 2.54 KB
/
utils_entity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package telegram
import (
"fmt"
"net/url"
)
// ParseURL selects URL from entered text of message and parse it as url.URL.
func (entity *MessageEntity) ParseURL(messageText string) *url.URL {
if entity == nil || !entity.IsURL() || messageText == "" {
return nil
}
from := entity.Offset
to := from + entity.Length
text := []rune(messageText)
if len(text) < to {
return nil
}
link, err := url.Parse(string(text[from:to]))
if err == nil && link.Scheme == "" {
link, err = url.Parse(fmt.Sprint("http://", link))
}
if err != nil {
return nil
}
return link
}
// IsBold checks that the current entity is a bold tag.
func (entity *MessageEntity) IsBold() bool {
return entity != nil && entity.Type == EntityBold
}
// IsBotCommand checks that the current entity is a bot command.
func (entity *MessageEntity) IsBotCommand() bool {
return entity != nil && entity.Type == EntityBotCommand
}
// IsCode checks that the current entity is a code tag.
func (entity *MessageEntity) IsCode() bool {
return entity != nil && entity.Type == EntityCode
}
// IsEmail checks that the current entity is a email.
func (entity *MessageEntity) IsEmail() bool {
return entity != nil && entity.Type == EntityEmail
}
// IsHashtag checks that the current entity is a hashtag.
func (entity *MessageEntity) IsHashtag() bool {
return entity != nil && entity.Type == EntityHashtag
}
// IsItalic checks that the current entity is a italic tag.
func (entity *MessageEntity) IsItalic() bool {
return entity != nil && entity.Type == EntityItalic
}
// IsMention checks that the current entity is a username mention.
func (entity *MessageEntity) IsMention() bool {
return entity != nil && entity.Type == EntityMention
}
// IsPre checks that the current entity is a pre tag.
func (entity *MessageEntity) IsPre() bool {
return entity != nil && entity.Type == EntityPre
}
// IsTextLink checks that the current entity is a text link.
func (entity *MessageEntity) IsTextLink() bool {
return entity != nil && entity.Type == EntityTextLink
}
// IsTextMention checks that the current entity is a mention without username.
func (entity *MessageEntity) IsTextMention() bool {
return entity != nil && entity.Type == EntityTextMention
}
// IsURL checks that the current entity is a URL
func (entity *MessageEntity) IsURL() bool {
return entity != nil && entity.Type == EntityURL
}
// TextLink parse current text link entity as url.URL.
func (entity *MessageEntity) TextLink() *url.URL {
if entity == nil {
return nil
}
link, err := url.Parse(entity.URL)
if err != nil {
return nil
}
return link
}