1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpBotFramework. |
5
|
|
|
* |
6
|
|
|
* PhpBotFramework is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, version 3. |
9
|
|
|
* |
10
|
|
|
* PhpBotFramework is distributed in the hope that it will be useful, but |
11
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13
|
|
|
* Lesser General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace PhpBotFramework\Entities; |
20
|
|
|
|
21
|
|
|
use PhpBotFramework\Exceptions\BotException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* \addtogroup Entities Entities |
25
|
|
|
* @{ |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
/** \class InlineKeyboard |
29
|
|
|
* \brief Inline Keyboard handler that create and handle inline keyboard buttons. |
30
|
|
|
* \details It stores the inline keyboard buttons added until get() is called. |
31
|
|
|
* It also provides some basic button to get, like Menu and Back buttons plus the dynamic-keyboard for menu browsing. |
32
|
|
|
*/ |
33
|
|
|
class InlineKeyboard |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* \addtogroup InlineKeyboard InlineKeyboard |
37
|
|
|
* \brief Handle an inline keyboard to send along with messages. |
38
|
|
|
* @{ |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
/** \brief Stores the array of InlineKeyboardButton */ |
42
|
|
|
protected $inline_keyboard; |
43
|
|
|
|
44
|
|
|
/** \brief Stores the current row. */ |
45
|
|
|
private $row; |
46
|
|
|
|
47
|
|
|
/** \brief Stores the current column. */ |
48
|
|
|
private $column; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* \brief Create an inline keyboard object. |
52
|
|
|
* @param array $buttons Buttons to add to the inline keyboard. |
53
|
|
|
*/ |
54
|
2 |
|
public function __construct(array $buttons = array()) |
55
|
|
|
{ |
56
|
2 |
|
$this->inline_keyboard = $buttons; |
57
|
|
|
|
58
|
|
|
// Reset indexes |
59
|
2 |
|
$this->row = 0; |
60
|
2 |
|
$this->column = 0; |
61
|
2 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* \brief Get a JSON object containing the inline keyboard. |
65
|
|
|
* @param bool $clear_keyboard Remove all the buttons from this object. |
66
|
|
|
* @return string JSON object. |
67
|
|
|
*/ |
68
|
1 |
View Code Duplication |
public function get(bool $clear_keyboard = true) : string |
|
|
|
|
69
|
|
|
{ |
70
|
1 |
|
if (empty($this->inline_keyboard)) { |
71
|
|
|
throw new BotException("Inline keyboard is empty"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Create a new array to put our buttons |
75
|
1 |
|
$reply_markup = ['inline_keyboard' => $this->inline_keyboard]; |
76
|
1 |
|
$reply_markup = json_encode($reply_markup); |
77
|
|
|
|
78
|
1 |
|
if ($clear_keyboard) { |
79
|
|
|
$this->clearKeyboard(); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
return $reply_markup; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* \brief Get the array containing the buttons. |
87
|
|
|
* \details Use this method when adding keyboard to inline query results. |
88
|
|
|
* @param bool $clean_keyboard If it's true, it'll clean the inline keyboard. |
89
|
|
|
* @return array An array containing the buttons. |
90
|
|
|
*/ |
91
|
1 |
View Code Duplication |
public function getArray(bool $clean_keyboard = true) : array |
|
|
|
|
92
|
|
|
{ |
93
|
1 |
|
if (empty($this->inline_keyboard)) { |
94
|
|
|
throw new BotException("Inline keyboard is empty"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Create a new array to put the buttons |
98
|
1 |
|
$reply_markup = ['inline_keyboard' => $this->inline_keyboard]; |
99
|
|
|
|
100
|
1 |
|
if ($clean_keyboard) { |
101
|
1 |
|
$this->clearKeyboard(); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
return $reply_markup; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* \brief Add buttons for the current row. |
109
|
|
|
* \details Each array sent as parameter requires a text key |
110
|
|
|
* and one another key |
111
|
|
|
* (see <a href="https://core.telegram.org/bots/api/#inlinekeyboardbutton" target="blank">here</a>) |
112
|
|
|
* like: |
113
|
|
|
* - url |
114
|
|
|
* - callback_data |
115
|
|
|
* - switch_inline_query |
116
|
|
|
* - switch_inline_query_current_chat |
117
|
|
|
* - callback_game |
118
|
|
|
* |
119
|
|
|
* Each call to this function add one or more button to a row. |
120
|
|
|
* The next call add buttons on the next row. |
121
|
|
|
* |
122
|
|
|
* There are twelve columns and eight buttons per row. |
123
|
|
|
* |
124
|
|
|
* Let's see an example: |
125
|
|
|
* |
126
|
|
|
* addLevelButtons(['text' => 'Click me!', 'url' => 'https://telegram.me']); |
127
|
|
|
* |
128
|
|
|
* If you want to add more than a button: |
129
|
|
|
* |
130
|
|
|
* addLevelButtons(['text' => 'Button 1', 'url' => 'https://telegram.me/gamedev_ita'], ['text' => 'Button 2', 'url' => 'https://telegram.me/animewallpaper']); |
131
|
|
|
* |
132
|
|
|
* @param array ...$buttons One or more arrays which represents buttons. |
133
|
|
|
*/ |
134
|
1 |
|
public function addLevelButtons(array ...$buttons) |
135
|
|
|
{ |
136
|
|
|
// If the user has already added a button in this row |
137
|
1 |
|
if ($this->column != 0) { |
138
|
|
|
$this->changeRow(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Add buttons to the next row |
142
|
1 |
|
$this->inline_keyboard[] = $buttons; |
143
|
1 |
|
$this->changeRow(); |
144
|
1 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* \brief Add a button. |
148
|
|
|
* \details The button will be added next to the last one or |
149
|
|
|
* in the next row if the bot has reached the limit of <b>buttons per row<b>. |
150
|
|
|
* |
151
|
|
|
* Each row allows eight buttons per row and a maximum of twelve columns. |
152
|
|
|
* |
153
|
|
|
* addButton('Click me!', 'url', 'https://telegram.me'); |
154
|
|
|
* |
155
|
|
|
* @param string $text Text showed on the button. |
156
|
|
|
* @param string $data_type The type of the button data. |
157
|
|
|
* Select one from these types. |
158
|
|
|
* - url |
159
|
|
|
* - callback_data |
160
|
|
|
* - switch_inline_query |
161
|
|
|
* - switch_inline_query_current_chat |
162
|
|
|
* - callback_game |
163
|
|
|
* @param string $data Data for the type selected. |
164
|
|
|
*/ |
165
|
1 |
|
public function addButton(string $text, string $data_type, string $data) |
166
|
|
|
{ |
167
|
|
|
// If we get the end of the row |
168
|
1 |
|
if ($this->column == 8) { |
169
|
|
|
$this->changeRow(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Add the button |
173
|
1 |
|
$this->inline_keyboard[$this->row][$this->column] = ['text' => $text, $data_type => $data]; |
174
|
1 |
|
$this->column++; |
175
|
1 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* \brief Change row for the current keyboard. |
179
|
|
|
* \details Change row where the buttons will be saved. |
180
|
|
|
*/ |
181
|
1 |
|
public function changeRow() |
182
|
|
|
{ |
183
|
1 |
|
$this->row++; |
184
|
1 |
|
$this->column = 0; |
185
|
1 |
|
} |
186
|
|
|
|
187
|
|
|
/** \brief Clear inline keyboard. */ |
188
|
1 |
|
public function clearKeyboard() |
189
|
|
|
{ |
190
|
1 |
|
$this->inline_keyboard = []; |
191
|
|
|
|
192
|
1 |
|
$this->row = 0; |
193
|
1 |
|
$this->column = 0; |
194
|
1 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* \brief Add a list keyboard. |
198
|
|
|
* \details A list keyboard can be useful if you want separate |
199
|
|
|
* data in multiple "pages" and allows users to navigate it easily. |
200
|
|
|
* The keyboard is generated at runtime. |
201
|
|
|
* @param int $index The current index of the list. |
202
|
|
|
* @param int $list The length of the list. |
203
|
|
|
* @param string $prefix Prefix to add at each callback_data of the button. Eg.: 'list/1'. |
204
|
|
|
*/ |
205
|
1 |
|
public function addListKeyboard(int $index, int $list, $prefix = 'list') |
206
|
|
|
{ |
207
|
1 |
|
$buttons = []; |
208
|
|
|
|
209
|
1 |
|
if (($list > 0) && ($index >= 0)) { |
210
|
1 |
|
if ($index == 0) { |
211
|
|
|
if ($list > 1) { |
212
|
|
|
if ($list > 2) { |
213
|
|
|
if ($list > 3) { |
214
|
|
|
if ($list > 4) { |
215
|
|
|
if ($list > 5) { |
216
|
|
|
$buttons = [ |
217
|
|
|
[ |
218
|
|
|
'text' => '1', |
219
|
|
|
'callback_data' => $prefix . '/1' |
220
|
|
|
], |
221
|
|
|
[ |
222
|
|
|
'text' => '2', |
223
|
|
|
'callback_data' => $prefix . '/2' |
224
|
|
|
], |
225
|
|
|
[ |
226
|
|
|
'text' => '3', |
227
|
|
|
'callback_data' => $prefix . '/3' |
228
|
|
|
], |
229
|
|
|
[ |
230
|
|
|
'text' => '4 ›', |
231
|
|
|
'callback_data' => $prefix . '/4' |
232
|
|
|
], |
233
|
|
|
[ |
234
|
|
|
'text' => "$list ››", |
235
|
|
|
'callback_data' => $prefix . "/$list" |
236
|
|
|
] |
237
|
|
|
]; |
238
|
|
|
} else { |
239
|
|
|
$buttons = [ |
240
|
|
|
[ |
241
|
|
|
'text' => '1', |
242
|
|
|
'callback_data' => $prefix . '/1' |
243
|
|
|
], |
244
|
|
|
[ |
245
|
|
|
'text' => '2', |
246
|
|
|
'callback_data' => $prefix . '/2' |
247
|
|
|
], |
248
|
|
|
[ |
249
|
|
|
'text' => '3', |
250
|
|
|
'callback_data' => $prefix . '/3' |
251
|
|
|
], |
252
|
|
|
[ |
253
|
|
|
'text' => '4', |
254
|
|
|
'callback_data' => $prefix . '/4' |
255
|
|
|
], |
256
|
|
|
[ |
257
|
|
|
'text' => '5', |
258
|
|
|
'callback_data' => $prefix . '/5' |
259
|
|
|
] |
260
|
|
|
]; |
261
|
|
|
} |
262
|
|
|
} else { |
263
|
|
|
$buttons = [ |
264
|
|
|
[ |
265
|
|
|
'text' => '1', |
266
|
|
|
'callback_data' => $prefix . '/1' |
267
|
|
|
], |
268
|
|
|
[ |
269
|
|
|
'text' => '2', |
270
|
|
|
'callback_data' => $prefix . '/2' |
271
|
|
|
], |
272
|
|
|
[ |
273
|
|
|
'text' => '3', |
274
|
|
|
'callback_data' => $prefix . '/3' |
275
|
|
|
], |
276
|
|
|
[ |
277
|
|
|
'text' => '4', |
278
|
|
|
'callback_data' => $prefix . '/4' |
279
|
|
|
], |
280
|
|
|
]; |
281
|
|
|
} |
282
|
|
|
} else { |
283
|
|
|
$buttons = [ |
284
|
|
|
[ |
285
|
|
|
'text' => '1', |
286
|
|
|
'callback_data' => $prefix . '/1' |
287
|
|
|
], |
288
|
|
|
[ |
289
|
|
|
'text' => '2', |
290
|
|
|
'callback_data' => $prefix . '/2' |
291
|
|
|
], |
292
|
|
|
[ |
293
|
|
|
'text' => '3', |
294
|
|
|
'callback_data' => $prefix . '/3' |
295
|
|
|
], |
296
|
|
|
]; |
297
|
|
|
} |
298
|
|
|
} elseif ($list == 2) { |
299
|
|
|
$buttons = [ |
300
|
|
|
[ |
301
|
|
|
'text' => '1', |
302
|
|
|
'callback_data' => $prefix . '/1' |
303
|
|
|
], |
304
|
|
|
[ |
305
|
|
|
'text' => '2', |
306
|
|
|
'callback_data' => $prefix . '/2' |
307
|
|
|
], |
308
|
|
|
]; |
309
|
|
|
} |
310
|
|
|
} else { |
311
|
|
|
$buttons = [ |
312
|
|
|
[ |
313
|
|
|
'text' => '1', |
314
|
|
|
'callback_data' => $prefix . '/1' |
315
|
|
|
] |
316
|
|
|
]; |
317
|
|
|
} |
318
|
1 |
|
} elseif ($index == 1) { |
319
|
|
|
if ($list > 1) { |
320
|
|
View Code Duplication |
if ($list > 2) { |
|
|
|
|
321
|
|
|
if ($list > 3) { |
322
|
|
|
if ($list > 4) { |
323
|
|
|
if ($list > 5) { |
324
|
|
|
$buttons = [ |
325
|
|
|
[ |
326
|
|
|
'text' => '• 1 •', |
327
|
|
|
'callback_data' => 'null' |
328
|
|
|
], |
329
|
|
|
[ |
330
|
|
|
'text' => '2', |
331
|
|
|
'callback_data' => $prefix . '/2' |
332
|
|
|
], |
333
|
|
|
[ |
334
|
|
|
'text' => '3', |
335
|
|
|
'callback_data' => $prefix . '/3' |
336
|
|
|
], |
337
|
|
|
[ |
338
|
|
|
'text' => '4 ›', |
339
|
|
|
'callback_data' => $prefix . '/4' |
340
|
|
|
], |
341
|
|
|
[ |
342
|
|
|
'text' => "$list ››", |
343
|
|
|
'callback_data' => $prefix . "/$list" |
344
|
|
|
] |
345
|
|
|
]; |
346
|
|
|
} else { |
347
|
|
|
$buttons = [ |
348
|
|
|
[ |
349
|
|
|
'text' => '• 1 •', |
350
|
|
|
'callback_data' => 'null' |
351
|
|
|
], |
352
|
|
|
[ |
353
|
|
|
'text' => '2', |
354
|
|
|
'callback_data' => $prefix . '/2' |
355
|
|
|
], |
356
|
|
|
[ |
357
|
|
|
'text' => '3', |
358
|
|
|
'callback_data' => $prefix . '/3' |
359
|
|
|
], |
360
|
|
|
[ |
361
|
|
|
'text' => '4', |
362
|
|
|
'callback_data' => $prefix . '/4' |
363
|
|
|
], |
364
|
|
|
[ |
365
|
|
|
'text' => '5', |
366
|
|
|
'callback_data' => $prefix . '/5' |
367
|
|
|
] |
368
|
|
|
]; |
369
|
|
|
} |
370
|
|
|
} else { |
371
|
|
|
$buttons = [ |
372
|
|
|
[ |
373
|
|
|
'text' => '• 1 •', |
374
|
|
|
'callback_data' => 'null' |
375
|
|
|
], |
376
|
|
|
[ |
377
|
|
|
'text' => '2', |
378
|
|
|
'callback_data' => $prefix . '/2' |
379
|
|
|
], |
380
|
|
|
[ |
381
|
|
|
'text' => '3', |
382
|
|
|
'callback_data' => $prefix . '/3' |
383
|
|
|
], |
384
|
|
|
[ |
385
|
|
|
'text' => '4', |
386
|
|
|
'callback_data' => $prefix . '/4' |
387
|
|
|
] |
388
|
|
|
]; |
389
|
|
|
} |
390
|
|
|
} else { |
391
|
|
|
$buttons = [ |
392
|
|
|
[ |
393
|
|
|
'text' => '• 1 •', |
394
|
|
|
'callback_data' => 'null' |
395
|
|
|
], |
396
|
|
|
[ |
397
|
|
|
'text' => '2', |
398
|
|
|
'callback_data' => $prefix . '/2' |
399
|
|
|
], |
400
|
|
|
[ |
401
|
|
|
'text' => '3', |
402
|
|
|
'callback_data' => $prefix . '/3' |
403
|
|
|
] |
404
|
|
|
]; |
405
|
|
|
} |
406
|
|
|
} elseif ($list == 2) { |
407
|
|
|
$buttons = [ |
408
|
|
|
[ |
409
|
|
|
'text' => '• 1 •', |
410
|
|
|
'callback_data' => 'null' |
411
|
|
|
], |
412
|
|
|
[ |
413
|
|
|
'text' => '2', |
414
|
|
|
'callback_data' => $prefix . '/2' |
415
|
|
|
] |
416
|
|
|
]; |
417
|
|
|
} |
418
|
|
|
} else { |
419
|
|
|
$buttons = [ |
420
|
|
|
[ |
421
|
|
|
'text' => '• 1 •', |
422
|
|
|
'callback_data' => 'null' |
423
|
|
|
] |
424
|
|
|
]; |
425
|
|
|
} |
426
|
1 |
View Code Duplication |
} elseif ($index == 2) { |
|
|
|
|
427
|
|
|
if ($list > 3) { |
428
|
|
|
if ($list > 4) { |
429
|
|
|
if ($list > 5) { |
430
|
|
|
$buttons = [ |
431
|
|
|
[ |
432
|
|
|
'text' => '1', |
433
|
|
|
'callback_data' => $prefix . '/1' |
434
|
|
|
], |
435
|
|
|
[ |
436
|
|
|
'text' => '• 2 •', |
437
|
|
|
'callback_data' => 'null' |
438
|
|
|
], |
439
|
|
|
[ |
440
|
|
|
'text' => '3', |
441
|
|
|
'callback_data' => $prefix . '/3' |
442
|
|
|
], |
443
|
|
|
[ |
444
|
|
|
'text' => '4 ›', |
445
|
|
|
'callback_data' => $prefix . '/4' |
446
|
|
|
], |
447
|
|
|
[ |
448
|
|
|
'text' => "$list ››", |
449
|
|
|
'callback_data' => $prefix . "/$list" |
450
|
|
|
] |
451
|
|
|
]; |
452
|
|
|
} else { |
453
|
|
|
$buttons = [ |
454
|
|
|
[ |
455
|
|
|
'text' => '1', |
456
|
|
|
'callback_data' => $prefix . '/1' |
457
|
|
|
], |
458
|
|
|
[ |
459
|
|
|
'text' => '• 2 •', |
460
|
|
|
'callback_data' => 'null' |
461
|
|
|
], |
462
|
|
|
[ |
463
|
|
|
'text' => '3', |
464
|
|
|
'callback_data' => $prefix . '/3' |
465
|
|
|
], |
466
|
|
|
[ |
467
|
|
|
'text' => '4', |
468
|
|
|
'callback_data' => '4' |
469
|
|
|
], |
470
|
|
|
[ |
471
|
|
|
'text' => '5', |
472
|
|
|
'callback_data' => $prefix . '/5' |
473
|
|
|
] |
474
|
|
|
]; |
475
|
|
|
} |
476
|
|
|
} else { |
477
|
|
|
$buttons = [ |
478
|
|
|
[ |
479
|
|
|
'text' => '1', |
480
|
|
|
'callback_data' => $prefix . '/1' |
481
|
|
|
], |
482
|
|
|
[ |
483
|
|
|
'text' => '• 2 •', |
484
|
|
|
'callback_data' => 'null' |
485
|
|
|
], |
486
|
|
|
[ |
487
|
|
|
'text' => '3', |
488
|
|
|
'callback_data' => $prefix . '/3' |
489
|
|
|
], |
490
|
|
|
[ |
491
|
|
|
'text' => '4', |
492
|
|
|
'callback_data' => $prefix . '/4' |
493
|
|
|
] |
494
|
|
|
]; |
495
|
|
|
} |
496
|
|
|
} elseif ($list == 3) { |
497
|
|
|
$buttons = [ |
498
|
|
|
[ |
499
|
|
|
'text' => '1', |
500
|
|
|
'callback_data' => $prefix . '/1' |
501
|
|
|
], |
502
|
|
|
[ |
503
|
|
|
'text' => '• 2 •', |
504
|
|
|
'callback_data' => 'null' |
505
|
|
|
], |
506
|
|
|
[ |
507
|
|
|
'text' => '3', |
508
|
|
|
'callback_data' => $prefix . '/3' |
509
|
|
|
] |
510
|
|
|
]; |
511
|
|
|
} else { |
512
|
|
|
$buttons = [ |
513
|
|
|
[ |
514
|
|
|
'text' => '1', |
515
|
|
|
'callback_data' => $prefix . '/1' |
516
|
|
|
], |
517
|
|
|
[ |
518
|
|
|
'text' => '• 2 •', |
519
|
|
|
'callback_data' => 'null' |
520
|
|
|
] |
521
|
|
|
]; |
522
|
|
|
} |
523
|
1 |
|
} elseif ($index == 3) { |
524
|
|
|
if ($list > 4) { |
525
|
|
|
if ($list > 5) { |
526
|
|
|
$buttons = [ |
527
|
|
|
[ |
528
|
|
|
'text' => '1', |
529
|
|
|
'callback_data' => $prefix . '/1' |
530
|
|
|
], |
531
|
|
|
[ |
532
|
|
|
'text' => '2', |
533
|
|
|
'callback_data' => $prefix . '/2' |
534
|
|
|
], |
535
|
|
|
[ |
536
|
|
|
'text' => '• 3 •', |
537
|
|
|
'callback_data' => 'null' |
538
|
|
|
], |
539
|
|
|
[ |
540
|
|
|
'text' => '4 ›', |
541
|
|
|
'callback_data' => $prefix . '/4' |
542
|
|
|
], |
543
|
|
|
[ |
544
|
|
|
'text' => "$list ››", |
545
|
|
|
'callback_data' => $prefix . "/$list" |
546
|
|
|
] |
547
|
|
|
]; |
548
|
|
|
} else { |
549
|
|
|
$buttons = [ |
550
|
|
|
[ |
551
|
|
|
'text' => '1', |
552
|
|
|
'callback_data' => $prefix . '/1' |
553
|
|
|
], |
554
|
|
|
[ |
555
|
|
|
'text' => '2', |
556
|
|
|
'callback_data' => $prefix . '/2' |
557
|
|
|
], |
558
|
|
|
[ |
559
|
|
|
'text' => '• 3 •', |
560
|
|
|
'callback_data' => 'null' |
561
|
|
|
], |
562
|
|
|
[ |
563
|
|
|
'text' => '4', |
564
|
|
|
'callback_data' => $prefix . '/4' |
565
|
|
|
], |
566
|
|
|
[ |
567
|
|
|
'text' => '5', |
568
|
|
|
'callback_data' => $prefix . '/5' |
569
|
|
|
] |
570
|
|
|
]; |
571
|
|
|
} |
572
|
|
|
} elseif ($list == 4) { |
573
|
|
|
$buttons = [ |
574
|
|
|
[ |
575
|
|
|
'text' => '1', |
576
|
|
|
'callback_data' => $prefix . '/1' |
577
|
|
|
], |
578
|
|
|
[ |
579
|
|
|
'text' => '2', |
580
|
|
|
'callback_data' => $prefix . '/2' |
581
|
|
|
], |
582
|
|
|
[ |
583
|
|
|
'text' => '• 3 •', |
584
|
|
|
'callback_data' => 'null' |
585
|
|
|
], |
586
|
|
|
[ |
587
|
|
|
'text' => '4', |
588
|
|
|
'callback_data' => $prefix . '/4' |
589
|
|
|
] |
590
|
|
|
]; |
591
|
|
|
} else { |
592
|
|
|
$buttons = [ |
593
|
|
|
[ |
594
|
|
|
'text' => '1', |
595
|
|
|
'callback_data' => $prefix . '/1' |
596
|
|
|
], |
597
|
|
|
[ |
598
|
|
|
'text' => '2', |
599
|
|
|
'callback_data' => $prefix . '/2' |
600
|
|
|
], |
601
|
|
|
[ |
602
|
|
|
'text' => '• 3 •', |
603
|
|
|
'callback_data' => 'null' |
604
|
|
|
] |
605
|
|
|
]; |
606
|
|
|
} |
607
|
1 |
|
} elseif ($index == 4 && $list <= 5) { |
608
|
|
|
if ($list == 4) { |
609
|
|
|
$buttons = [ |
610
|
|
|
[ |
611
|
|
|
'text' => '1', |
612
|
|
|
'callback_data' => $prefix . '/1' |
613
|
|
|
], |
614
|
|
|
[ |
615
|
|
|
'text' => '2', |
616
|
|
|
'callback_data' => $prefix . '/2' |
617
|
|
|
], |
618
|
|
|
[ |
619
|
|
|
'text' => '3', |
620
|
|
|
'callback_data' => $prefix . '/3' |
621
|
|
|
], |
622
|
|
|
[ |
623
|
|
|
'text' => '• 4 •', |
624
|
|
|
'callback_data' => 'null' |
625
|
|
|
] |
626
|
|
|
]; |
627
|
|
|
} elseif ($list == 5) { |
628
|
|
|
$buttons = [ |
629
|
|
|
[ |
630
|
|
|
'text' => '1', |
631
|
|
|
'callback_data' => $prefix . '/1' |
632
|
|
|
], |
633
|
|
|
[ |
634
|
|
|
'text' => '2', |
635
|
|
|
'callback_data' => $prefix . '/2' |
636
|
|
|
], |
637
|
|
|
[ |
638
|
|
|
'text' => '3', |
639
|
|
|
'callback_data' => $prefix . '/3' |
640
|
|
|
], |
641
|
|
|
[ |
642
|
|
|
'text' => '• 4 •', |
643
|
|
|
'callback_data' => 'null' |
644
|
|
|
], |
645
|
|
|
[ |
646
|
|
|
'text' => '5', |
647
|
|
|
'callback_data' => $prefix . '/5' |
648
|
|
|
] |
649
|
|
|
]; |
650
|
|
|
} |
651
|
1 |
|
} elseif ($index == 5 && $list == 5) { |
652
|
|
|
$buttons = [ |
653
|
|
|
[ |
654
|
|
|
'text' => '1', |
655
|
|
|
'callback_data' => $prefix . '/1' |
656
|
|
|
], |
657
|
|
|
[ |
658
|
|
|
'text' => '2', |
659
|
|
|
'callback_data' => $prefix . '/2' |
660
|
|
|
], |
661
|
|
|
[ |
662
|
|
|
'text' => '3', |
663
|
|
|
'callback_data' => $prefix . '/3' |
664
|
|
|
], |
665
|
|
|
[ |
666
|
|
|
'text' => '4', |
667
|
|
|
'callback_data' => $prefix . '/4' |
668
|
|
|
], |
669
|
|
|
[ |
670
|
|
|
'text' => '• 5 •', |
671
|
|
|
'callback_data' => 'null' |
672
|
|
|
] |
673
|
|
|
]; |
674
|
|
|
} else { |
675
|
1 |
|
if ($index < $list - 2) { |
676
|
1 |
|
$indexm = $index - 1; |
677
|
1 |
|
$indexp = $index + 1; |
678
|
|
|
$buttons = [ |
679
|
|
|
[ |
680
|
1 |
|
'text' => '‹‹ 1', |
681
|
1 |
|
'callback_data' => $prefix . '/1' |
682
|
|
|
], |
683
|
|
|
[ |
684
|
1 |
|
'text' => '‹ ' . $indexm, |
685
|
1 |
|
'callback_data' => $prefix . '/' . $indexm |
686
|
|
|
], |
687
|
|
|
[ |
688
|
1 |
|
'text' => '• ' . $index . ' •', |
689
|
1 |
|
'callback_data' => 'null', |
690
|
|
|
], |
691
|
|
|
[ |
692
|
1 |
|
'text' => $indexp . ' ›', |
693
|
1 |
|
'callback_data' => $prefix . '/' . $indexp |
694
|
|
|
], |
695
|
|
|
[ |
696
|
1 |
|
'text' => $list . ' ››', |
697
|
1 |
|
'callback_data' => $prefix . '/' . $list |
698
|
|
|
] |
699
|
|
|
]; |
700
|
|
|
} elseif ($index == ($list - 2)) { |
701
|
|
|
$indexm = $index - 1; |
702
|
|
|
$indexp = $index + 1; |
703
|
|
|
$buttons = [ |
704
|
|
|
[ |
705
|
|
|
'text' => '‹‹1', |
706
|
|
|
'callback_data' => $prefix . '/1' |
707
|
|
|
], |
708
|
|
|
[ |
709
|
|
|
'text' => '' . $indexm, |
710
|
|
|
'callback_data' => $prefix . '/' . $indexm |
711
|
|
|
], |
712
|
|
|
[ |
713
|
|
|
'text' => '• ' . $index . ' •', |
714
|
|
|
'callback_data' => 'null', |
715
|
|
|
], |
716
|
|
|
[ |
717
|
|
|
'text' => '' . $indexp, |
718
|
|
|
'callback_data' => $prefix . '/' . $indexp |
719
|
|
|
], |
720
|
|
|
[ |
721
|
|
|
'text' => "$list", |
722
|
|
|
'callback_data' => $prefix . "/$list" |
723
|
|
|
] |
724
|
|
|
]; |
725
|
|
|
} elseif ($index == ($list - 1)) { |
726
|
|
|
$indexm = $index - 1; |
727
|
|
|
$indexmm = $index - 2; |
728
|
|
|
$buttons = [ |
729
|
|
|
[ |
730
|
|
|
'text' => '‹‹ 1', |
731
|
|
|
'callback_data' => $prefix . '/1' |
732
|
|
|
], |
733
|
|
|
[ |
734
|
|
|
'text' => '‹ ' . $indexmm, |
735
|
|
|
'callback_data' => $prefix . '/' . $indexmm |
736
|
|
|
], |
737
|
|
|
[ |
738
|
|
|
'text' => '' . $indexm, |
739
|
|
|
'callback_data' => $prefix . '/' . $indexm |
740
|
|
|
], |
741
|
|
|
[ |
742
|
|
|
'text' => '• ' . $index . ' •', |
743
|
|
|
'callback_data' => $prefix . '/' . $index |
744
|
|
|
], |
745
|
|
|
[ |
746
|
|
|
'text' => "$list", |
747
|
|
|
'callback_data' => $prefix . "/$list" |
748
|
|
|
] |
749
|
|
|
]; |
750
|
|
|
} elseif ($index == $list) { |
751
|
|
|
$indexm = $index - 1; |
752
|
|
|
$indexmm = $index - 2; |
753
|
|
|
$indexmmm = $index - 3; |
754
|
|
|
$buttons = [ |
755
|
|
|
[ |
756
|
|
|
'text' => '‹‹ 1', |
757
|
|
|
'callback_data' => $prefix . '/1' |
758
|
|
|
], |
759
|
|
|
[ |
760
|
|
|
'text' => '‹ ' . $indexmmm, |
761
|
|
|
'callback_data' => $prefix . '/' . $indexmmm |
762
|
|
|
], |
763
|
|
|
[ |
764
|
|
|
'text' => '' . $indexmm, |
765
|
|
|
'callback_data' => $prefix . '/' . $indexmm, |
766
|
|
|
], |
767
|
|
|
[ |
768
|
|
|
'text' => '' . $indexm, |
769
|
|
|
'callback_data' => $prefix . '/' . $indexm |
770
|
|
|
], |
771
|
|
|
[ |
772
|
|
|
'text' => '• ' . $index . ' •', |
773
|
|
|
'callback_data' => $prefix . '/' . $index |
774
|
|
|
] |
775
|
|
|
]; |
776
|
|
|
} |
777
|
|
|
} |
778
|
|
|
} |
779
|
|
|
|
780
|
|
|
// If there are other buttons in this row (checking the column) |
781
|
1 |
|
if ($this->column !== 0) { |
782
|
|
|
// Go to the next |
783
|
|
|
$this->changeRow(); |
784
|
|
|
} |
785
|
|
|
|
786
|
1 |
|
$this->inline_keyboard[$this->row] = $buttons; |
787
|
|
|
|
788
|
|
|
// We added a row |
789
|
1 |
|
$this->changeRow(); |
790
|
1 |
|
} |
791
|
|
|
|
792
|
|
|
/** @} */ |
793
|
|
|
|
794
|
|
|
/** @} */ |
795
|
|
|
} |
796
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.