1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Zanzara; |
6
|
|
|
|
7
|
|
|
use DI\Container; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use React\Cache\CacheInterface; |
10
|
|
|
use React\EventLoop\LoopInterface; |
11
|
|
|
use React\Socket\Connector; |
12
|
|
|
use Zanzara\UpdateMode\Polling; |
13
|
|
|
use Zanzara\UpdateMode\ReactPHPWebhook; |
14
|
|
|
use Zanzara\UpdateMode\UpdateModeInterface; |
15
|
|
|
use Zanzara\UpdateMode\Webhook; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class Config |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
public const WEBHOOK_MODE = Webhook::class; |
24
|
|
|
public const POLLING_MODE = Polling::class; |
25
|
|
|
public const REACTPHP_WEBHOOK_MODE = ReactPHPWebhook::class; |
26
|
|
|
|
27
|
|
|
public const PARSE_MODE_HTML = "HTML"; |
28
|
|
|
public const PARSE_MODE_MARKDOWN = "MarkdownV2"; |
29
|
|
|
public const PARSE_MODE_MARKDOWN_LEGACY = "Markdown"; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $botToken; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var LoopInterface|null |
38
|
|
|
*/ |
39
|
|
|
private $loop; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var CacheInterface|null |
43
|
|
|
*/ |
44
|
|
|
private $cache; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var boolean |
48
|
|
|
*/ |
49
|
|
|
private $useReactFileSystem = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Container|null |
53
|
|
|
*/ |
54
|
|
|
private $container; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string|UpdateModeInterface |
58
|
|
|
*/ |
59
|
|
|
private $updateMode = self::POLLING_MODE; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string|null |
63
|
|
|
*/ |
64
|
|
|
private $parseMode; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $updateStream = 'php://input'; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
private $apiTelegramUrl = 'https://api.telegram.org'; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
private $serverUri = "0.0.0.0:8080"; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
private $serverContext = []; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var float |
88
|
|
|
*/ |
89
|
|
|
private $bulkMessageInterval = 2.0; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var bool |
93
|
|
|
*/ |
94
|
|
|
private $webhookTokenCheck = false; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling. Should be positive, short polling |
98
|
|
|
* should be used for testing purposes only. |
99
|
|
|
* |
100
|
|
|
* @var int |
101
|
|
|
*/ |
102
|
|
|
private $pollingTimeout = 50; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Limits the number of updates to be retrieved. Values between 1-100 are accepted. Defaults to 100. |
106
|
|
|
* |
107
|
|
|
* @var int |
108
|
|
|
*/ |
109
|
|
|
private $pollingLimit = 100; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* A JSON-serialized list of the update types you want your bot to receive. For example, specify |
113
|
|
|
* [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types. See Update for a |
114
|
|
|
* complete list of available update types. Specify an empty list to receive all updates regardless of type |
115
|
|
|
* (default). If not specified, the previous setting will be used. Please note that this parameter doesn't affect |
116
|
|
|
* updates created before the call to the getUpdates, so unwanted updates may be received for a short period of time. |
117
|
|
|
* |
118
|
|
|
* @var array |
119
|
|
|
*/ |
120
|
|
|
private $pollingAllowedUpdates = []; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @var LoggerInterface|null |
124
|
|
|
*/ |
125
|
|
|
private $logger; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @var callable|null |
129
|
|
|
*/ |
130
|
|
|
private $errorHandler; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Default ttl in seconds. Null means that item will stay in the cache |
134
|
|
|
* for as long as the underlying implementation supports. |
135
|
|
|
* Check reactphp cache implementation for more information |
136
|
|
|
* @var float|null |
137
|
|
|
*/ |
138
|
|
|
private $cacheTtl = 180; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @var Connector|null |
142
|
|
|
*/ |
143
|
|
|
private $connector; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* |
147
|
|
|
* @since 0.5.1 |
148
|
|
|
* @var array |
149
|
|
|
*/ |
150
|
|
|
private $connectorOptions = []; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* |
154
|
|
|
* @since 0.5.1 |
155
|
|
|
* @var string|null |
156
|
|
|
*/ |
157
|
|
|
private $proxyUrl; |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* |
161
|
|
|
* @since 0.5.1 |
162
|
|
|
* @var array |
163
|
|
|
*/ |
164
|
|
|
private $proxyHttpHeaders = []; |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return LoopInterface|null |
168
|
|
|
*/ |
169
|
|
|
public function getLoop(): ?LoopInterface |
170
|
|
|
{ |
171
|
|
|
return $this->loop; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param LoopInterface|null $loop |
176
|
|
|
*/ |
177
|
|
|
public function setLoop(?LoopInterface $loop): void |
178
|
|
|
{ |
179
|
|
|
$this->loop = $loop; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string|UpdateModeInterface |
184
|
|
|
*/ |
185
|
|
|
public function getUpdateMode() |
186
|
|
|
{ |
187
|
|
|
return $this->updateMode; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $updateMode |
192
|
|
|
*/ |
193
|
|
|
public function setUpdateMode(string $updateMode): void |
194
|
|
|
{ |
195
|
|
|
$this->updateMode = $updateMode; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return string|null |
200
|
|
|
*/ |
201
|
|
|
public function getParseMode(): ?string |
202
|
|
|
{ |
203
|
|
|
return $this->parseMode; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string|null $parseMode |
208
|
|
|
*/ |
209
|
|
|
public function setParseMode(?string $parseMode): void |
210
|
|
|
{ |
211
|
|
|
$this->parseMode = $parseMode; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public function getUpdateStream(): string |
218
|
|
|
{ |
219
|
|
|
return $this->updateStream; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param string $updateStream |
224
|
|
|
*/ |
225
|
|
|
public function setUpdateStream(string $updateStream): void |
226
|
|
|
{ |
227
|
|
|
$this->updateStream = $updateStream; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
|
|
public function getApiTelegramUrl(): string |
234
|
|
|
{ |
235
|
|
|
return $this->apiTelegramUrl; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string $apiTelegramUrl |
240
|
|
|
*/ |
241
|
|
|
public function setApiTelegramUrl(string $apiTelegramUrl): void |
242
|
|
|
{ |
243
|
|
|
$this->apiTelegramUrl = $apiTelegramUrl; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
public function getServerUri(): string |
250
|
|
|
{ |
251
|
|
|
return $this->serverUri; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param string $serverUri |
256
|
|
|
*/ |
257
|
|
|
public function setServerUri(string $serverUri): void |
258
|
|
|
{ |
259
|
|
|
$this->serverUri = $serverUri; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return array |
264
|
|
|
*/ |
265
|
|
|
public function getServerContext(): array |
266
|
|
|
{ |
267
|
|
|
return $this->serverContext; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param array $serverContext |
272
|
|
|
*/ |
273
|
|
|
public function setServerContext(array $serverContext): void |
274
|
|
|
{ |
275
|
|
|
$this->serverContext = $serverContext; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return float |
280
|
|
|
*/ |
281
|
|
|
public function getBulkMessageInterval(): float |
282
|
|
|
{ |
283
|
|
|
return $this->bulkMessageInterval; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param float $bulkMessageInterval |
288
|
|
|
*/ |
289
|
|
|
public function setBulkMessageInterval(float $bulkMessageInterval): void |
290
|
|
|
{ |
291
|
|
|
$this->bulkMessageInterval = $bulkMessageInterval; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @return bool |
296
|
|
|
*/ |
297
|
|
|
public function isWebhookTokenCheckEnabled(): bool |
298
|
|
|
{ |
299
|
|
|
return $this->webhookTokenCheck; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param bool $webhookTokenCheck |
304
|
|
|
*/ |
305
|
|
|
public function enableWebhookTokenCheck(bool $webhookTokenCheck): void |
306
|
|
|
{ |
307
|
|
|
$this->webhookTokenCheck = $webhookTokenCheck; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @return int |
312
|
|
|
*/ |
313
|
|
|
public function getPollingTimeout(): int |
314
|
|
|
{ |
315
|
|
|
return $this->pollingTimeout; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @param int $pollingTimeout |
320
|
|
|
*/ |
321
|
|
|
public function setPollingTimeout(int $pollingTimeout): void |
322
|
|
|
{ |
323
|
|
|
$this->pollingTimeout = $pollingTimeout; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @return int |
328
|
|
|
*/ |
329
|
|
|
public function getPollingLimit(): int |
330
|
|
|
{ |
331
|
|
|
return $this->pollingLimit; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @param int $pollingLimit |
336
|
|
|
*/ |
337
|
|
|
public function setPollingLimit(int $pollingLimit): void |
338
|
|
|
{ |
339
|
|
|
$this->pollingLimit = $pollingLimit; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @return array |
344
|
|
|
*/ |
345
|
|
|
public function getPollingAllowedUpdates(): array |
346
|
|
|
{ |
347
|
|
|
return $this->pollingAllowedUpdates; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @param array $pollingAllowedUpdates |
352
|
|
|
*/ |
353
|
|
|
public function setPollingAllowedUpdates(array $pollingAllowedUpdates): void |
354
|
|
|
{ |
355
|
|
|
$this->pollingAllowedUpdates = $pollingAllowedUpdates; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @return LoggerInterface|null |
360
|
|
|
*/ |
361
|
|
|
public function getLogger(): ?LoggerInterface |
362
|
|
|
{ |
363
|
|
|
return $this->logger; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @param LoggerInterface|null $logger |
368
|
|
|
*/ |
369
|
|
|
public function setLogger(?LoggerInterface $logger): void |
370
|
|
|
{ |
371
|
|
|
$this->logger = $logger; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @return CacheInterface|null |
376
|
|
|
*/ |
377
|
|
|
public function getCache(): ?CacheInterface |
378
|
|
|
{ |
379
|
|
|
return $this->cache; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* @param CacheInterface|null $cache |
384
|
|
|
*/ |
385
|
|
|
public function setCache(?CacheInterface $cache): void |
386
|
|
|
{ |
387
|
|
|
$this->cache = $cache; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* @return Container|null |
392
|
|
|
*/ |
393
|
|
|
public function getContainer(): ?Container |
394
|
|
|
{ |
395
|
|
|
return $this->container; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @param Container|null $container |
400
|
|
|
*/ |
401
|
|
|
public function setContainer(?Container $container): void |
402
|
|
|
{ |
403
|
|
|
$this->container = $container; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @return string |
408
|
|
|
*/ |
409
|
|
|
public function getBotToken(): string |
410
|
|
|
{ |
411
|
|
|
return $this->botToken; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* @param string $botToken |
416
|
|
|
*/ |
417
|
|
|
public function setBotToken(string $botToken): void |
418
|
|
|
{ |
419
|
|
|
$this->botToken = $botToken; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @param bool $bool |
424
|
|
|
*/ |
425
|
|
|
public function useReactFileSystem(bool $bool) |
426
|
|
|
{ |
427
|
|
|
$this->useReactFileSystem = $bool; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* @return bool |
432
|
|
|
*/ |
433
|
|
|
public function isReactFileSystem() |
434
|
|
|
{ |
435
|
|
|
return $this->useReactFileSystem; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @return callable|null |
440
|
|
|
*/ |
441
|
|
|
public function getErrorHandler(): ?callable |
442
|
|
|
{ |
443
|
|
|
return $this->errorHandler; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* @param callable|null $errorHandler |
448
|
|
|
*/ |
449
|
|
|
public function setErrorHandler(?callable $errorHandler): void |
450
|
|
|
{ |
451
|
|
|
$this->errorHandler = $errorHandler; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* @return float|null |
456
|
|
|
*/ |
457
|
|
|
public function getCacheTtl(): ?float |
458
|
|
|
{ |
459
|
|
|
return $this->cacheTtl; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* @param float|null $cacheTtl |
464
|
|
|
*/ |
465
|
|
|
public function setCacheTtl(?float $cacheTtl): void |
466
|
|
|
{ |
467
|
|
|
$this->cacheTtl = $cacheTtl; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* @return Connector|null |
472
|
|
|
*/ |
473
|
|
|
public function getConnector(): ?Connector |
474
|
|
|
{ |
475
|
|
|
return $this->connector; |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* @param Connector|null $connector |
480
|
|
|
*/ |
481
|
|
|
public function setConnector(?Connector $connector): void |
482
|
|
|
{ |
483
|
|
|
$this->connector = $connector; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* @return array |
488
|
|
|
*/ |
489
|
|
|
public function getConnectorOptions(): array |
490
|
|
|
{ |
491
|
|
|
return $this->connectorOptions; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* @param array $connectorOptions |
496
|
|
|
*/ |
497
|
|
|
public function setConnectorOptions(array $connectorOptions): void |
498
|
|
|
{ |
499
|
|
|
$this->connectorOptions = $connectorOptions; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* @return string|null |
504
|
|
|
*/ |
505
|
|
|
public function getProxyUrl(): ?string |
506
|
|
|
{ |
507
|
|
|
return $this->proxyUrl; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* @param string|null $proxyUrl |
512
|
|
|
*/ |
513
|
|
|
public function setProxyUrl(?string $proxyUrl): void |
514
|
|
|
{ |
515
|
|
|
$this->proxyUrl = $proxyUrl; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* @return array |
520
|
|
|
*/ |
521
|
|
|
public function getProxyHttpHeaders(): array |
522
|
|
|
{ |
523
|
|
|
return $this->proxyHttpHeaders; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* @param array $proxyHttpHeaders |
528
|
|
|
*/ |
529
|
|
|
public function setProxyHttpHeaders(array $proxyHttpHeaders): void |
530
|
|
|
{ |
531
|
|
|
$this->proxyHttpHeaders = $proxyHttpHeaders; |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
} |
535
|
|
|
|