1 | <?php declare(strict_types=1); |
||
15 | class Params |
||
16 | { |
||
17 | /** |
||
18 | * @var array List of valid script parameters. |
||
19 | */ |
||
20 | private static $valid_script_params = [ |
||
21 | 's', // secret |
||
22 | 'a', // action |
||
23 | 'l', // loop |
||
24 | 'i', // interval |
||
25 | 'g', // group (for cron) |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * @var array List of vital parameters that must be passed. |
||
30 | */ |
||
31 | private static $valid_vital_bot_params = [ |
||
32 | 'api_key', |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * @var array List of valid extra parameters that can be passed. |
||
37 | */ |
||
38 | private static $valid_extra_bot_params = [ |
||
39 | 'bot_username', |
||
40 | 'secret', |
||
41 | 'validate_request', |
||
42 | 'valid_ips', |
||
43 | 'webhook', |
||
44 | 'logging', |
||
45 | 'limiter', |
||
46 | 'admins', |
||
47 | 'mysql', |
||
48 | 'paths', |
||
49 | 'commands', |
||
50 | 'botan', |
||
51 | 'cron', |
||
52 | 'custom_input', |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * @var array List of all params passed to the script. |
||
57 | */ |
||
58 | private $script_params = []; |
||
59 | |||
60 | /** |
||
61 | * @var array List of all params passed at construction, predefined with defaults. |
||
62 | */ |
||
63 | private $bot_params = [ |
||
64 | 'validate_request' => true, |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * Params constructor. |
||
69 | * |
||
70 | * api_key (string) Telegram Bot API key |
||
71 | * bot_username (string) Telegram Bot username |
||
72 | * secret (string) Secret string to validate calls |
||
73 | * validate_request (bool) Only allow webhook access from valid Telegram API IPs and defined valid_ips |
||
74 | * valid_ips (array) Any IPs, besides Telegram API IPs, that are allowed to access the webhook |
||
75 | * webhook (array) |
||
76 | * - url (string) URI of the webhook |
||
77 | * - certificate (string) Path to the self-signed certificate |
||
78 | * - max_connections (int) Maximum allowed simultaneous HTTPS connections to the webhook |
||
79 | * - allowed_updates (array) List the types of updates you want your bot to receive |
||
80 | * logging (array) Array of logger files to set. |
||
81 | * limiter (array) |
||
82 | * - enabled (bool) Set limiter. |
||
83 | * - options (array) Limiter options. |
||
84 | * admins (array) List of admins to enable. |
||
85 | * mysql (array) MySQL credentials to use. |
||
86 | * paths (array) |
||
87 | * - download (string) Custom download path to set. |
||
88 | * - upload (string) Custom upload path to set. |
||
89 | * commands (array) |
||
90 | * - paths (array) Custom commands paths to set. |
||
91 | * - configs (array) List of custom command configs. |
||
92 | * botan (array) |
||
93 | * - token (string) Botan token to enable botan.io support. |
||
94 | * - options (array) Botan options. |
||
95 | * custom_input (string) Custom raw JSON string to use as input. |
||
96 | * cron (array) |
||
97 | * - groups (array) Groups of cron commands to run. |
||
98 | * - default (array) Default group of cron commands. |
||
99 | * |
||
100 | * @param array $params All params to set the bot up with. |
||
101 | * |
||
102 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidParamsException |
||
103 | */ |
||
104 | public function __construct(array $params) |
||
109 | |||
110 | /** |
||
111 | * Validate and set up the vital and extra params. |
||
112 | * |
||
113 | * @param array $params |
||
114 | * |
||
115 | * @return \TelegramBot\TelegramBotManager\Params |
||
116 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidParamsException |
||
117 | */ |
||
118 | private function validateAndSetBotParams(array $params): self |
||
126 | |||
127 | /** |
||
128 | * Set all vital params. |
||
129 | * |
||
130 | * @param array $params |
||
131 | * |
||
132 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidParamsException |
||
133 | */ |
||
134 | private function validateAndSetBotParamsVital(array $params) |
||
144 | |||
145 | /** |
||
146 | * Special case parameters. |
||
147 | * |
||
148 | * @param array $params |
||
149 | * |
||
150 | * @throws \TelegramBot\TelegramBotManager\Exception\InvalidParamsException |
||
151 | */ |
||
152 | private function validateAndSetBotParamsSpecial(array $params) |
||
162 | |||
163 | /** |
||
164 | * Set all extra params. |
||
165 | * |
||
166 | * @param array $params |
||
167 | */ |
||
168 | private function validateAndSetBotParamsExtra(array $params) |
||
178 | |||
179 | /** |
||
180 | * Handle all script params, via web server handler or CLI. |
||
181 | * |
||
182 | * https://url/entry.php?s=<secret>&a=<action>&l=<loop> |
||
183 | * $ php entry.php s=<secret> a=<action> l=<loop> |
||
184 | * |
||
185 | * @return \TelegramBot\TelegramBotManager\Params |
||
186 | */ |
||
187 | private function validateAndSetScriptParams(): self |
||
194 | |||
195 | /** |
||
196 | * Set script parameters from query string or CLI. |
||
197 | */ |
||
198 | private function setScriptParams() |
||
216 | |||
217 | /** |
||
218 | * Keep only valid script parameters. |
||
219 | */ |
||
220 | private function validateScriptParams() |
||
227 | |||
228 | /** |
||
229 | * Get a specific bot param, allowing array-dot notation. |
||
230 | * |
||
231 | * @param string $param |
||
232 | * @param mixed $default |
||
233 | * |
||
234 | * @return mixed |
||
235 | */ |
||
236 | public function getBotParam(string $param, $default = null) |
||
250 | |||
251 | /** |
||
252 | * Get an array of all bot params. |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | public function getBotParams(): array |
||
260 | |||
261 | /** |
||
262 | * Get a specific script param. |
||
263 | * |
||
264 | * @param string $param |
||
265 | * @param mixed $default |
||
266 | * |
||
267 | * @return mixed |
||
268 | */ |
||
269 | public function getScriptParam(string $param, $default = null) |
||
273 | |||
274 | /** |
||
275 | * Get an array of all script params. |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | public function getScriptParams(): array |
||
283 | } |
||
284 |