forked from telegraf/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 6
/
markup.js
286 lines (245 loc) · 6.8 KB
/
markup.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
class Markup {
forceReply (value = true) {
this.force_reply = value
return this
}
removeKeyboard (value = true) {
this.remove_keyboard = value
return this
}
selective (value = true) {
this.selective = value
return this
}
extra (options) {
return {
reply_markup: { ...this },
...options
}
}
keyboard (buttons, options) {
const keyboard = buildKeyboard(buttons, { columns: 1, ...options })
if (keyboard && keyboard.length > 0) {
this.keyboard = keyboard
}
return this
}
resize (value = true) {
this.resize_keyboard = value
return this
}
oneTime (value = true) {
this.one_time_keyboard = value
return this
}
inlineKeyboard (buttons, options) {
const keyboard = buildKeyboard(buttons, { columns: buttons.length, ...options })
if (keyboard && keyboard.length > 0) {
this.inline_keyboard = keyboard
}
return this
}
button (text, hide) {
return Markup.button(text, hide)
}
contactRequestButton (text, hide) {
return Markup.contactRequestButton(text, hide)
}
locationRequestButton (text, hide) {
return Markup.locationRequestButton(text, hide)
}
pollRequestButton (text, type, hide) {
return Markup.pollRequestButton(text, type, hide)
}
urlButton (text, url, hide) {
return Markup.urlButton(text, url, hide)
}
callbackButton (text, data, hide) {
return Markup.callbackButton(text, data, hide)
}
switchToChatButton (text, value, hide) {
return Markup.switchToChatButton(text, value, hide)
}
switchToCurrentChatButton (text, value, hide) {
return Markup.switchToCurrentChatButton(text, value, hide)
}
gameButton (text, hide) {
return Markup.gameButton(text, hide)
}
payButton (text, hide) {
return Markup.payButton(text, hide)
}
loginButton (text, url, opts, hide) {
return Markup.loginButton(text, url, opts, hide)
}
static removeKeyboard (value) {
return new Markup().removeKeyboard(value)
}
static forceReply (value) {
return new Markup().forceReply(value)
}
static keyboard (buttons, options) {
return new Markup().keyboard(buttons, options)
}
static inlineKeyboard (buttons, options) {
return new Markup().inlineKeyboard(buttons, options)
}
static resize (value = true) {
return new Markup().resize(value)
}
static selective (value = true) {
return new Markup().selective(value)
}
static oneTime (value = true) {
return new Markup().oneTime(value)
}
static button (text, hide = false) {
return { text: text, hide: hide }
}
static contactRequestButton (text, hide = false) {
return { text: text, request_contact: true, hide: hide }
}
static locationRequestButton (text, hide = false) {
return { text: text, request_location: true, hide: hide }
}
static pollRequestButton (text, type, hide = false) {
return { text: text, request_poll: { type }, hide: hide }
}
static urlButton (text, url, hide = false) {
return { text: text, url: url, hide: hide }
}
static callbackButton (text, data, hide = false) {
return { text: text, callback_data: data, hide: hide }
}
static switchToChatButton (text, value, hide = false) {
return { text: text, switch_inline_query: value, hide: hide }
}
static switchToCurrentChatButton (text, value, hide = false) {
return { text: text, switch_inline_query_current_chat: value, hide: hide }
}
static gameButton (text, hide = false) {
return { text: text, callback_game: {}, hide: hide }
}
static payButton (text, hide = false) {
return { text: text, pay: true, hide: hide }
}
static loginButton (text, url, opts = {}, hide = false) {
return {
text: text,
login_url: { ...opts, url: url },
hide: hide
}
}
static formatHTML (text = '', entities = []) {
const chars = [...text]
const available = [...entities]
const opened = []
const result = []
for (let offset = 0; offset < chars.length; offset++) {
while (true) {
const index = available.findIndex((entity) => entity.offset === offset)
if (index === -1) {
break
}
const entity = available[index]
switch (entity.type) {
case 'bold':
result.push('<b>')
break
case 'italic':
result.push('<i>')
break
case 'code':
result.push('<code>')
break
case 'pre':
if (entity.language) {
result.push(`<pre><code class="language-${entity.language}">`)
} else {
result.push('<pre>')
}
break
case 'strikethrough':
result.push('<s>')
break
case 'underline':
result.push('<u>')
break
case 'text_mention':
result.push(`<a href="tg://user?id=${entity.user.id}">`)
break
case 'text_link':
result.push(`<a href="${entity.url}">`)
break
}
opened.unshift(entity)
available.splice(index, 1)
}
result.push(chars[offset])
while (true) {
const index = opened.findIndex((entity) => entity.offset + entity.length - 1 === offset)
if (index === -1) {
break
}
const entity = opened[index]
switch (entity.type) {
case 'bold':
result.push('</b>')
break
case 'italic':
result.push('</i>')
break
case 'code':
result.push('</code>')
break
case 'pre':
if (entity.language) {
result.push('</code></pre>')
} else {
result.push('</pre>')
}
break
case 'strikethrough':
result.push('</s>')
break
case 'underline':
result.push('</u>')
break
case 'text_mention':
case 'text_link':
result.push('</a>')
break
}
opened.splice(index, 1)
}
}
return result.join('')
}
}
function buildKeyboard (buttons, options) {
const result = []
if (!Array.isArray(buttons)) {
return result
}
if (buttons.find(Array.isArray)) {
return buttons.map(row => row.filter((button) => !button.hide))
}
const wrapFn = options.wrap
? options.wrap
: (btn, index, currentRow) => currentRow.length >= options.columns
let currentRow = []
let index = 0
for (const btn of buttons.filter((button) => !button.hide)) {
if (wrapFn(btn, index, currentRow) && currentRow.length > 0) {
result.push(currentRow)
currentRow = []
}
currentRow.push(btn)
index++
}
if (currentRow.length > 0) {
result.push(currentRow)
}
return result
}
module.exports = Markup