-
Notifications
You must be signed in to change notification settings - Fork 27
/
ListenerCollector.php
480 lines (450 loc) · 14.7 KB
/
ListenerCollector.php
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
declare(strict_types=1);
namespace Zanzara\Listener;
use DI\DependencyException;
use DI\NotFoundException;
use Psr\Container\ContainerInterface;
use Zanzara\Middleware\MiddlewareCollector;
use Zanzara\Middleware\MiddlewareInterface;
use Zanzara\Telegram\Type\CallbackQuery;
use Zanzara\Telegram\Type\ChannelPost;
use Zanzara\Telegram\Type\ChosenInlineResult;
use Zanzara\Telegram\Type\EditedChannelPost;
use Zanzara\Telegram\Type\EditedMessage;
use Zanzara\Telegram\Type\InlineQuery;
use Zanzara\Telegram\Type\Message;
use Zanzara\Telegram\Type\Passport\PassportData;
use Zanzara\Telegram\Type\Poll\Poll;
use Zanzara\Telegram\Type\Poll\PollAnswer;
use Zanzara\Telegram\Type\ReplyToMessage;
use Zanzara\Telegram\Type\Shipping\PreCheckoutQuery;
use Zanzara\Telegram\Type\Shipping\ShippingQuery;
use Zanzara\Telegram\Type\Shipping\SuccessfulPayment;
use Zanzara\Telegram\Type\Update;
/**
* Class ListenerCollector
* @package Zanzara\Listener
*/
abstract class ListenerCollector
{
/**
* Associative array for listeners.
* Key is always the listener type that can be either a simple string (eg. messages, cb_query_texts) or the class
* name of the Update type, @see Update::detectUpdateType().
* Value can be an ordered array of @see Listener or another associative array where the key
* is the listenerId and the value the actual @see Listener.
*
* Eg.
* [
* 'messages' => [
* '/start' => Listener(),
* 'Simple text' => Listener(),
* ],
* 'Zanzara\Telegram\Type\CallbackQuery' => [
* Listener(),
* Listener(),
* Listener()
* ]
* ]
*
* @var array
*/
protected $listeners = [];
/**
* @var Listener
*/
protected $fallbackListener;
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var array
*/
protected $middleware = [];
/**
* Listen for the specified command.
* Eg. $bot->onCommand('start', function(Context $ctx) {});
*
* @param string $command
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onCommand(string $command, $callback): MiddlewareCollector
{
$command = "/^\/$command$/";
$listener = new Listener($callback, $this->container, $command);
$this->listeners['messages'][$command] = $listener;
return $listener;
}
/**
* Listen for a message with the specified text.
* Eg. $bot->onText('What time is it?', function(Context $ctx) {});
*
* Text is a regex, so you could also do something like:
* $bot->onText('[a-zA-Z]{15}?', function(Context $ctx) {});
*
* @param string $text
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onText(string $text, $callback): MiddlewareCollector
{
$text = "/$text/";
$listener = new Listener($callback, $this->container, $text);
$this->listeners['messages'][$text] = $listener;
return $listener;
}
/**
* Listen for a generic message.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onMessage(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onMessage($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[Message::class][] = $listener;
return $listener;
}
/**
* Listen for a message that is a reply of another message.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onReplyToMessage(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onReplyToMessage($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[ReplyToMessage::class][] = $listener;
return $listener;
}
/**
* Listen for an edited message.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onEditedMessage(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onEditedMessage($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[EditedMessage::class][] = $listener;
return $listener;
}
/**
* Listen for a callback query with the specified message text.
*
* Eg. $bot->onCbQueryText('How many apples do you want?', function(Context $ctx) {});
*
* Text is a regex, so you could also do something like:
* $bot->onCbQueryText('[a-zA-Z]{27}?', function(Context $ctx) {});
*
* @param string $text
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onCbQueryText(string $text, $callback): MiddlewareCollector
{
$text = "/$text/";
$listener = new Listener($callback, $this->container, $text);
$this->listeners['cb_query_texts'][$text] = $listener;
return $listener;
}
/**
* Listen for a callback query with the specified callback data.
*
* Eg. $bot->onCbQueryData(['accept', 'refuse'], function(Context $ctx) {});
*
* Data values are a regex, so you could also do something like:
* $bot->onCbQueryData(['acc.'], function(Context $ctx) {});
*
* @param array $data
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onCbQueryData(array $data, $callback): MiddlewareCollector
{
// merge values with "|" (eg. "accept|refuse|later"), then ListenerResolver will check the callback data
// against that regex.
$id = '/'.implode('|', $data).'/';
$listener = new Listener($callback, $this->container, $id);
$this->listeners['cb_query_data'][$id] = $listener;
return $listener;
}
/**
* Listen for a generic callback query.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onCbQuery(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onCbQuery($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[CallbackQuery::class][] = $listener;
return $listener;
}
/**
* Listener for a shipping query.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onShippingQuery(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onShippingQuery($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[ShippingQuery::class][] = $listener;
return $listener;
}
/**
* Listen for a pre checkout query.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onPreCheckoutQuery(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onPreCheckoutQuery($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[PreCheckoutQuery::class][] = $listener;
return $listener;
}
/**
* Listen for a successful payment.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onSuccessfulPayment(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onSuccessfulPayment($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[SuccessfulPayment::class][] = $listener;
return $listener;
}
/**
* Listen for a passport data message.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onPassportData(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onPassportData($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[PassportData::class][] = $listener;
return $listener;
}
/**
* Listen for an inline query.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onInlineQuery(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onInlineQuery($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[InlineQuery::class][] = $listener;
return $listener;
}
/**
* Listen for a chosen inline result.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onChosenInlineResult(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onChosenInlineResult($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[ChosenInlineResult::class][] = $listener;
return $listener;
}
/**
* Listen for a channel post.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onChannelPost(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onChannelPost($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[ChannelPost::class][] = $listener;
return $listener;
}
/**
* Listen for an edited channel post.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onEditedChannelPost(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onEditedChannelPost($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[EditedChannelPost::class][] = $listener;
return $listener;
}
/**
* Listen for a poll.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onPoll(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onPoll($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[Poll::class][] = $listener;
return $listener;
}
/**
* Listen for a poll answer.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onPollAnswer(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onPollAnswer($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[PollAnswer::class][] = $listener;
return $listener;
}
/**
* Listen for a generic update.
* You can call this function more than once, every callback will be executed.
*
* Eg. $bot->onUpdate(function(Context $ctx) {});
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function onUpdate($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->listeners[Update::class][] = $listener;
return $listener;
}
/**
* If no listener matches the current update, this listener will be called if specified.
*
* @param $callback
* @return MiddlewareCollector
* @throws DependencyException
* @throws NotFoundException
*/
public function fallback($callback): MiddlewareCollector
{
$listener = new Listener($callback, $this->container);
$this->fallbackListener = $listener;
return $listener;
}
/**
* Define a middleware that will be executed for every listener function and before listener-specific middleware.
*
* Eg:
* $bot = new Zanzara($_ENV['BOT_TOKEN']);
* $bot->middleware(new GenericMiddleware());
*
* $bot->onCommand('start', function(Context $ctx) {
* $ctx->sendMessage('Hello');
* })->middleware(new SpecificMiddleware());
*
* In this case GenericMiddleware will be executed before SpecificMiddleware.
*
* @param MiddlewareInterface|callable $middleware
* @return self
*/
public function middleware($middleware): self
{
array_unshift($this->middleware, $middleware);
return $this;
}
/**
* Add cross-request middleware to each listener middleware chain.
*
*/
protected function feedMiddlewareStack()
{
array_walk_recursive($this->listeners, function ($value) {
if ($value instanceof Listener) {
foreach ($this->middleware as $m) {
$value->middleware($m);
}
}
});
}
}