forked from mymmrac/telego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_entity.go
227 lines (197 loc) · 6.36 KB
/
message_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package utils
import (
"fmt"
"strings"
"github.com/dsurl/tgo"
)
// MessageEntityCollection represents text and slice of tgo.MessageEntity associated with it
type MessageEntityCollection struct {
text string
entities []tgo.MessageEntity
}
// Entity creates new MessageEntityCollection with provided text and no entities
func Entity(text string) MessageEntityCollection {
return MessageEntityCollection{
text: text,
}
}
// Entityf creates new MessageEntityCollection with the provided format and args and no entities
func Entityf(format string, args ...interface{}) MessageEntityCollection {
return MessageEntityCollection{
text: fmt.Sprintf(format, args...),
}
}
// Text returns text associated with collection
func (c MessageEntityCollection) Text() string {
return c.text
}
// Entities returns message entities associated with collection
func (c MessageEntityCollection) Entities() []tgo.MessageEntity {
return c.entities
}
// SetOffset sets offset for all entities
func (c MessageEntityCollection) SetOffset(offset int) {
for i := range c.entities {
c.entities[i].Offset = offset
}
}
// Mention assigns mention entity and returns new collection
func (c MessageEntityCollection) Mention() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeMention,
Length: len(c.text),
})
return c
}
// Hashtag assigns hashtag entity and returns new collection
func (c MessageEntityCollection) Hashtag() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeHashtag,
Length: len(c.text),
})
return c
}
// Cashtag assigns cashtag entity and returns new collection
func (c MessageEntityCollection) Cashtag() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeCashtag,
Length: len(c.text),
})
return c
}
// BotCommand assigns bot command entity and returns new collection
func (c MessageEntityCollection) BotCommand() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeBotCommand,
Length: len(c.text),
})
return c
}
// URL assigns url entity and returns new collection
func (c MessageEntityCollection) URL() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeURL,
Length: len(c.text),
})
return c
}
// Email assigns email entity and returns new collection
func (c MessageEntityCollection) Email() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeEmail,
Length: len(c.text),
})
return c
}
// PhoneNumber assigns phone number entity and returns new collection
func (c MessageEntityCollection) PhoneNumber() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypePhoneNumber,
Length: len(c.text),
})
return c
}
// Bold assigns bold entity and returns new collection
func (c MessageEntityCollection) Bold() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeBold,
Length: len(c.text),
})
return c
}
// Italic assigns italic entity and returns new collection
func (c MessageEntityCollection) Italic() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeItalic,
Length: len(c.text),
})
return c
}
// Underline assigns underline entity and returns new collection
func (c MessageEntityCollection) Underline() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeUnderline,
Length: len(c.text),
})
return c
}
// Strikethrough assigns strikethrough entity and returns new collection
func (c MessageEntityCollection) Strikethrough() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeStrikethrough,
Length: len(c.text),
})
return c
}
// Spoiler assigns spoiler entity and returns new collection
func (c MessageEntityCollection) Spoiler() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeSpoiler,
Length: len(c.text),
})
return c
}
// Code assigns code entity and returns new collection
func (c MessageEntityCollection) Code() MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeCode,
Length: len(c.text),
})
return c
}
// Pre assigns pre entity with language and returns new collection
func (c MessageEntityCollection) Pre(language string) MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypePre,
Length: len(c.text),
Language: language,
})
return c
}
// TextLink assigns text link entity with URL and returns new collection
func (c MessageEntityCollection) TextLink(url string) MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeTextLink,
Length: len(c.text),
URL: url,
})
return c
}
// TextMention assigns text mention entity with user and returns new collection
func (c MessageEntityCollection) TextMention(user *tgo.User) MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeTextMention,
Length: len(c.text),
User: user,
})
return c
}
// TextMentionWithID assigns text mention entity with just user ID and returns new collection
func (c MessageEntityCollection) TextMentionWithID(userID int64) MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeTextMention,
Length: len(c.text),
User: &tgo.User{ID: userID},
})
return c
}
// CustomEmoji assigns custom emoji entity and returns new collection
func (c MessageEntityCollection) CustomEmoji(emojiID string) MessageEntityCollection {
c.entities = append(c.entities, tgo.MessageEntity{
Type: tgo.EntityTypeCustomEmoji,
Length: len(c.text),
CustomEmojiID: emojiID,
})
return c
}
// MessageEntities coverts entity collections into text and slice of tgo.MessageEntity associated with that text
func MessageEntities(entityCollections ...MessageEntityCollection) (string, []tgo.MessageEntity) {
text := strings.Builder{}
var entities []tgo.MessageEntity
for _, collection := range entityCollections {
collection.SetOffset(text.Len())
entities = append(entities, collection.Entities()...)
_, _ = text.WriteString(collection.Text())
}
return text.String(), entities
}