1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BPT\tools; |
4
|
|
|
|
5
|
|
|
use BPT\constants\loggerTypes; |
6
|
|
|
use BPT\constants\pollType; |
7
|
|
|
use BPT\exception\bptException; |
8
|
|
|
use BPT\logger; |
9
|
|
|
use BPT\types\inlineKeyboardButton; |
10
|
|
|
use BPT\types\inlineKeyboardMarkup; |
11
|
|
|
use BPT\types\keyboardButton; |
12
|
|
|
use BPT\types\keyboardButtonPollType; |
13
|
|
|
use BPT\types\replyKeyboardMarkup; |
14
|
|
|
use BPT\types\webAppInfo; |
15
|
|
|
|
16
|
|
|
trait generator { |
17
|
|
|
/** |
18
|
|
|
* Generate random string |
19
|
|
|
* |
20
|
|
|
* e.g. => tools::randomString(); |
21
|
|
|
* |
22
|
|
|
* e.g. => tools::randomString(16,'abcdefg'); |
23
|
|
|
* |
24
|
|
|
* e.g. => tools::randomString(length: 16,characters: 'abcdefg'); |
25
|
|
|
* |
26
|
|
|
* @param int $length length of generated string |
27
|
|
|
* @param string $characters string constructor characters |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public static function randomString (int $length = 16, string $characters = 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ'): string { |
32
|
|
|
$rand_string = ''; |
33
|
|
|
$char_len = strlen($characters) - 1; |
34
|
|
|
for ($i = 0; $i < $length; $i ++) { |
35
|
|
|
$rand_string .= $characters[rand(0, $char_len)]; |
36
|
|
|
} |
37
|
|
|
return $rand_string; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* create normal keyboard and inline keyboard easily |
42
|
|
|
* |
43
|
|
|
* you must set keyboard parameter(for normal keyboard) or inline parameter(for inline keyboard) |
44
|
|
|
* |
45
|
|
|
* if you set both , keyboard will be processed and inline will be ignored |
46
|
|
|
* |
47
|
|
|
* |
48
|
|
|
* |
49
|
|
|
* con for request contact , loc for request location, web||URL for webapp, pull||POLLTYPE for poll |
50
|
|
|
* |
51
|
|
|
* e.g. => tools::easyKey([['button 1 in row 1','button 2 in row 1'],['button 1 in row 2'],['contact button in row 3||con'],['location button in row 4||loc']]); |
52
|
|
|
* |
53
|
|
|
* |
54
|
|
|
* |
55
|
|
|
* e.g. => tools::easyKey(inline: [[['button 1 in row 1','this is callback button'],['button 2 in row 1','https://this-is-url-button.com']],[['demo button in row 2']]]); |
56
|
|
|
* |
57
|
|
|
* @param string[][] $keyboard array of array(as rows) of array(buttons) of string |
58
|
|
|
* @param array[][] $inline array of array(as rows) of array(buttons) |
59
|
|
|
* |
60
|
|
|
* @return inlineKeyboardMarkup|replyKeyboardMarkup replyKeyboardMarkup for keyboard and inlineKeyboardMarkup for inline |
61
|
|
|
* @throws bptException |
62
|
|
|
*/ |
63
|
|
|
public static function easyKey(array $keyboard = [], array $inline = []): inlineKeyboardMarkup|replyKeyboardMarkup { |
64
|
|
|
if (!empty($keyboard)) { |
65
|
|
|
$keyboard_object = new replyKeyboardMarkup(); |
66
|
|
|
$keyboard_object->setResize_keyboard($keyboard['resize'] ?? true); |
67
|
|
|
if (isset($keyboard['one_time'])) { |
68
|
|
|
$keyboard_object->setOne_time_keyboard($keyboard['one_time']) ; |
69
|
|
|
} |
70
|
|
|
foreach ($keyboard as $row) { |
71
|
|
|
$buttons = []; |
72
|
|
|
foreach ($row as $base_button) { |
73
|
|
|
$button_info = explode('||', $base_button); |
74
|
|
|
$button = new keyboardButton(); |
75
|
|
|
$button->setText($button_info[0] ?? $base_button); |
76
|
|
|
if (count($button_info) > 1) { |
77
|
|
|
if ($button_info[1] === 'con') { |
78
|
|
|
$button->setRequest_contact(true); |
79
|
|
|
} |
80
|
|
|
elseif ($button_info[1] === 'loc') { |
81
|
|
|
$button->setRequest_location(true); |
82
|
|
|
} |
83
|
|
|
elseif ($button_info[1] === 'poll') { |
84
|
|
|
$type = $button_info[2] === pollType::QUIZ ? pollType::QUIZ : pollType::REGULAR; |
85
|
|
|
$button->setRequest_poll((new keyboardButtonPollType())->setType($type)); |
86
|
|
|
} |
87
|
|
|
elseif ($button_info[1] === 'web' && isset($button_info[2])) { |
88
|
|
|
$url = $button_info[2]; |
89
|
|
|
$button->setWeb_app((new webAppInfo())->setUrl($url)); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
$buttons[] = $button; |
93
|
|
|
} |
94
|
|
|
$keyboard_object->setKeyboard([$buttons]); |
95
|
|
|
} |
96
|
|
|
return $keyboard_object; |
97
|
|
|
} |
98
|
|
|
elseif (!empty($inline)) { |
99
|
|
|
$keyboard_object = new inlineKeyboardMarkup(); |
100
|
|
|
foreach ($inline as $row) { |
101
|
|
|
$buttons = []; |
102
|
|
|
foreach ($row as $button_info) { |
103
|
|
|
$button = new inlineKeyboardButton(); |
104
|
|
|
if (isset($button_info[1])) { |
105
|
|
|
if (filter_var($button_info[1], FILTER_VALIDATE_URL) && str_starts_with($button_info[1], 'http')) { |
106
|
|
|
$button->setText($button_info[0])->setUrl($button_info[1]); |
107
|
|
|
} |
108
|
|
|
else { |
109
|
|
|
$button->setText($button_info[0])->setCallback_data($button_info[1]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
else { |
113
|
|
|
$button->setText($button_info[0])->setUrl('https://t.me/BPT_CH'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
$keyboard_object->setInline_keyboard([$buttons]); |
117
|
|
|
} |
118
|
|
|
return $keyboard_object; |
119
|
|
|
} |
120
|
|
|
else { |
121
|
|
|
logger::write("tools::eKey function used\nkeyboard or inline parameter must be set",loggerTypes::ERROR); |
122
|
|
|
throw new bptException('ARGUMENT_NOT_FOUND_KEYBOARD_INLINE'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |