@@ -127,20 +127,18 @@ class BotMan
127
127
* @param array $config
128
128
* @param StorageInterface $storage
129
129
*/
130
- public function __construct (CacheInterface $ cache , DriverInterface $ driver , $ config , StorageInterface $ storage )
130
+ public function __construct (CacheInterface $ cache , DriverInterface $ driver , $ config , StorageInterface $ storage, ? Matcher $ matcher = null )
131
131
{
132
- if (!isset ($ config ['bot_id ' ])) {
133
- $ config ['bot_id ' ] = '' ;
134
- }
132
+ $ this ->config = $ config ;
133
+ $ this ->config ['bot_id ' ] = $ this ->config ['bot_id ' ] ?? '' ;
135
134
136
135
$ this ->cache = $ cache ;
137
- $ this ->message = new IncomingMessage ('' , '' , '' , null , $ config ['bot_id ' ]);
136
+ $ this ->message = new IncomingMessage ('' , '' , '' , null , $ this -> config ['bot_id ' ]);
138
137
$ this ->driver = $ driver ;
139
- $ this ->config = $ config ;
140
138
$ this ->storage = $ storage ;
141
139
$ this ->matcher = new Matcher ();
142
140
$ this ->middleware = new MiddlewareManager ($ this );
143
- $ this ->conversationManager = new ConversationManager ();
141
+ $ this ->conversationManager = new ConversationManager ($ matcher );
144
142
$ this ->exceptionHandler = new ExceptionHandler ();
145
143
}
146
144
@@ -234,13 +232,16 @@ public function runsOnSocket($running = null)
234
232
*/
235
233
public function getUser ()
236
234
{
237
- if ($ user = $ this ->cache ->get ('user_ ' . $ this ->driver ->getName (). '_ ' . $ this ->getMessage ()->getSender ())) {
235
+ if ($ user = $ this ->cache ->get ('user_ ' . $ this ->driver ->getName () . '_ ' . $ this ->getMessage ()->getSender ())) {
238
236
return $ user ;
239
237
}
240
238
241
239
$ user = $ this ->getDriver ()->getUser ($ this ->getMessage ());
242
- $ this ->cache ->put ('user_ ' .$ this ->driver ->getName ().'_ ' .$ user ->getId (), $ user ,
243
- $ this ->config ['user_cache_time ' ] ?? 30 );
240
+ $ this ->cache ->put (
241
+ 'user_ ' . $ this ->driver ->getName () . '_ ' . $ user ->getId (),
242
+ $ user ,
243
+ $ this ->config ['user_cache_time ' ] ?? 30
244
+ );
244
245
245
246
return $ user ;
246
247
}
@@ -269,7 +270,7 @@ protected function compileParameterNames($value)
269
270
public function hears ($ pattern , $ callback , $ in = null )
270
271
{
271
272
if (is_array ($ pattern )) {
272
- $ pattern = '(?| ' . implode ('| ' , $ pattern ). ') ' ;
273
+ $ pattern = '(?| ' . implode ('| ' , $ pattern ) . ') ' ;
273
274
}
274
275
275
276
$ command = new Command ($ pattern , $ callback , $ in );
@@ -288,7 +289,7 @@ public function hears($pattern, $callback, $in = null)
288
289
*/
289
290
public function on ($ names , $ callback )
290
291
{
291
- if (! is_array ($ names )) {
292
+ if (!is_array ($ names )) {
292
293
$ names = [$ names ];
293
294
}
294
295
@@ -314,7 +315,7 @@ public function receivesImages($callback)
314
315
}
315
316
316
317
/**
317
- * Listening for image files.
318
+ * Listening for video files.
318
319
*
319
320
* @param $callback
320
321
* @return Command
@@ -419,7 +420,7 @@ public function listen()
419
420
try {
420
421
$ isVerificationRequest = $ this ->verifyServices ();
421
422
422
- if (! $ isVerificationRequest ) {
423
+ if (!$ isVerificationRequest ) {
423
424
$ this ->fireDriverEvents ();
424
425
425
426
if ($ this ->firedDriverEvents === false ) {
@@ -428,15 +429,15 @@ public function listen()
428
429
if ($ this ->loadedConversation === false ) {
429
430
$ this ->callMatchingMessages ();
430
431
}
432
+ }
431
433
432
- /*
433
- * If the driver has a "messagesHandled" method, call it.
434
- * This method can be used to trigger driver methods
435
- * once the messages are handles.
436
- */
437
- if (method_exists ($ this ->getDriver (), 'messagesHandled ' )) {
438
- $ this ->getDriver ()->messagesHandled ();
439
- }
434
+ /*
435
+ * If the driver has a "messagesHandled" method, call it.
436
+ * This method can be used to trigger driver methods
437
+ * once the messages are handles.
438
+ */
439
+ if (method_exists ($ this ->getDriver (), 'messagesHandled ' )) {
440
+ $ this ->getDriver ()->messagesHandled ();
440
441
}
441
442
442
443
$ this ->firedDriverEvents = false ;
@@ -452,8 +453,12 @@ public function listen()
452
453
*/
453
454
protected function callMatchingMessages ()
454
455
{
455
- $ matchingMessages = $ this ->conversationManager ->getMatchingMessages ($ this ->getMessages (), $ this ->middleware ,
456
- $ this ->getConversationAnswer (), $ this ->getDriver ());
456
+ $ matchingMessages = $ this ->conversationManager ->getMatchingMessages (
457
+ $ this ->getMessages (),
458
+ $ this ->middleware ,
459
+ $ this ->getConversationAnswer (),
460
+ $ this ->getDriver ()
461
+ );
457
462
458
463
foreach ($ matchingMessages as $ matchingMessage ) {
459
464
$ this ->command = $ matchingMessage ->getCommand ();
@@ -468,15 +473,18 @@ protected function callMatchingMessages()
468
473
return $ middleware instanceof Heard;
469
474
})->toArray ();
470
475
471
- $ this ->message = $ this ->middleware ->applyMiddleware ('heard ' , $ matchingMessage ->getMessage (),
472
- $ commandMiddleware );
476
+ $ this ->message = $ this ->middleware ->applyMiddleware (
477
+ 'heard ' ,
478
+ $ matchingMessage ->getMessage (),
479
+ $ commandMiddleware
480
+ );
473
481
474
482
$ parameterNames = $ this ->compileParameterNames ($ this ->command ->getPattern ());
475
483
476
484
$ parameters = $ matchingMessage ->getMatches ();
477
485
if (\count ($ parameterNames ) !== \count ($ parameters )) {
478
486
$ parameters = array_merge (
479
- //First, all named parameters (eg. function ($a, $b, $c))
487
+ //First, all named parameters (eg. function ($a, $b, $c))
480
488
array_filter (
481
489
$ parameters ,
482
490
'\is_string ' ,
@@ -501,7 +509,7 @@ protected function callMatchingMessages()
501
509
}
502
510
}
503
511
504
- if (empty ($ matchingMessages ) && empty ($ this ->getBotMessages ()) && ! \is_null ($ this ->fallbackMessage )) {
512
+ if (empty ($ matchingMessages ) && empty ($ this ->getBotMessages ()) && !\is_null ($ this ->fallbackMessage )) {
505
513
$ this ->callFallbackMessage ();
506
514
}
507
515
}
@@ -513,7 +521,7 @@ protected function callFallbackMessage()
513
521
{
514
522
$ messages = $ this ->getMessages ();
515
523
516
- if (! isset ($ messages [0 ])) {
524
+ if (!isset ($ messages [0 ])) {
517
525
return ;
518
526
}
519
527
@@ -537,7 +545,7 @@ protected function verifyServices()
537
545
/**
538
546
* @param string|Question|OutgoingMessage $message
539
547
* @param string|array $recipients
540
- * @param string|DriverInterface|null $driver
548
+ * @param string|DriverInterface|string| null $driver
541
549
* @param array $additionalParameters
542
550
* @return Response
543
551
* @throws BotManException
@@ -559,12 +567,8 @@ public function say($message, $recipients, $driver = null, $additionalParameters
559
567
560
568
$ recipients = \is_array ($ recipients ) ? $ recipients : [$ recipients ];
561
569
562
- if (!isset ($ this ->config ['bot_id ' ])) {
563
- $ this ->config ['bot_id ' ] = '' ;
564
- }
565
-
566
570
foreach ($ recipients as $ recipient ) {
567
- $ this ->message = new IncomingMessage ('' , $ recipient , '' , null , $ this ->config ['bot_id ' ]);
571
+ $ this ->message = new IncomingMessage ('' , $ recipient , '' , null , $ this ->config ['bot_id ' ] ?? '' );
568
572
$ response = $ this ->reply ($ message , $ additionalParameters );
569
573
}
570
574
@@ -584,7 +588,7 @@ public function say($message, $recipients, $driver = null, $additionalParameters
584
588
*/
585
589
public function ask ($ question , $ next , $ additionalParameters = [], $ recipient = null , $ driver = null )
586
590
{
587
- if (! \is_null ($ recipient ) && ! \is_null ($ driver )) {
591
+ if (!\is_null ($ recipient ) && !\is_null ($ driver )) {
588
592
if (\is_string ($ driver )) {
589
593
$ driver = DriverManager::loadFromName ($ driver , $ this ->config );
590
594
}
@@ -609,13 +613,12 @@ public function types()
609
613
}
610
614
611
615
/**
612
- * @param int $seconds Number of seconds to wait
616
+ * @param float $seconds Number of seconds to wait
613
617
* @return $this
614
618
*/
615
- public function typesAndWaits ($ seconds )
619
+ public function typesAndWaits (float $ seconds )
616
620
{
617
- $ this ->getDriver ()->types ($ this ->message );
618
- sleep ($ seconds );
621
+ $ this ->getDriver ()->typesAndWaits ($ this ->message , $ seconds );
619
622
620
623
return $ this ;
621
624
}
@@ -635,7 +638,7 @@ public function sendRequest($endpoint, $additionalParameters = [])
635
638
return $ driver ->sendRequest ($ endpoint , $ additionalParameters , $ this ->message );
636
639
}
637
640
638
- throw new BadMethodCallException ('The driver ' . $ this ->getDriver ()->getName (). ' does not support low level requests. ' );
641
+ throw new BadMethodCallException ('The driver ' . $ this ->getDriver ()->getName () . ' does not support low level requests. ' );
639
642
}
640
643
641
644
/**
@@ -647,8 +650,11 @@ public function reply($message, $additionalParameters = [])
647
650
{
648
651
$ this ->outgoingMessage = \is_string ($ message ) ? OutgoingMessage::create ($ message ) : $ message ;
649
652
650
- return $ this ->sendPayload ($ this ->getDriver ()->buildServicePayload ($ this ->outgoingMessage , $ this ->message ,
651
- $ additionalParameters ));
653
+ return $ this ->sendPayload ($ this ->getDriver ()->buildServicePayload (
654
+ $ this ->outgoingMessage ,
655
+ $ this ->message ,
656
+ $ additionalParameters
657
+ ));
652
658
}
653
659
654
660
/**
@@ -683,13 +689,14 @@ public function randomReply(array $messages)
683
689
*/
684
690
protected function makeInvokableAction ($ action )
685
691
{
686
- if (! method_exists ($ action , '__invoke ' )) {
692
+ if (!method_exists ($ action , '__invoke ' )) {
687
693
throw new UnexpectedValueException (sprintf (
688
- 'Invalid hears action: [%s] ' , $ action
694
+ 'Invalid hears action: [%s] ' ,
695
+ $ action
689
696
));
690
697
}
691
698
692
- return $ action. '@__invoke ' ;
699
+ return $ action . '@__invoke ' ;
693
700
}
694
701
695
702
/**
@@ -755,7 +762,7 @@ public function __call($name, $arguments)
755
762
return \call_user_func_array ([$ this ->getDriver (), $ name ], $ arguments );
756
763
}
757
764
758
- throw new BadMethodCallException ('Method [ ' . $ name. '] does not exist. ' );
765
+ throw new BadMethodCallException ('Method [ ' . $ name . '] does not exist. ' );
759
766
}
760
767
761
768
/**
0 commit comments