1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Conversation\Traits; |
6
|
|
|
|
7
|
|
|
use FondBot\Bot; |
8
|
|
|
use FondBot\Queue\Queue; |
9
|
|
|
use FondBot\Drivers\User; |
10
|
|
|
use FondBot\Conversation\Keyboard; |
11
|
|
|
use FondBot\Drivers\Commands\SendMessage; |
12
|
|
|
use FondBot\Drivers\Commands\SendAttachment; |
13
|
|
|
use FondBot\Drivers\ReceivedMessage\Attachment; |
14
|
|
|
|
15
|
|
|
trait SendsMessages |
16
|
|
|
{ |
17
|
|
|
/** @var Bot */ |
18
|
|
|
protected $bot; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Send message to user. |
22
|
|
|
* |
23
|
|
|
* @param string $text |
24
|
|
|
* @param Keyboard|null $keyboard |
25
|
|
|
*/ |
26
|
2 |
View Code Duplication |
protected function sendMessage(string $text, Keyboard $keyboard = null): void |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
/** @var Queue $queue */ |
29
|
2 |
|
$queue = $this->bot->get(Queue::class); |
30
|
|
|
|
31
|
2 |
|
$queue->push($this->bot->getDriver(), new SendMessage($this->user(), $text, $keyboard)); |
32
|
2 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Send message to user with delay. |
36
|
|
|
* |
37
|
|
|
* @param int $delay |
38
|
|
|
* @param string $text |
39
|
|
|
* @param Keyboard|null $keyboard |
40
|
|
|
*/ |
41
|
1 |
View Code Duplication |
protected function sendDelayedMessage(int $delay, string $text, Keyboard $keyboard = null): void |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
/** @var Queue $queue */ |
44
|
1 |
|
$queue = $this->bot->get(Queue::class); |
45
|
|
|
|
46
|
1 |
|
$queue->later($this->bot->getDriver(), new SendMessage($this->user(), $text, $keyboard), $delay); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Send attachment to user. |
51
|
|
|
* |
52
|
|
|
* @param Attachment $attachment |
53
|
|
|
* @param int $delay |
54
|
|
|
*/ |
55
|
2 |
|
protected function sendAttachment(Attachment $attachment, int $delay = 0): void |
56
|
|
|
{ |
57
|
|
|
/** @var Queue $queue */ |
58
|
2 |
|
$queue = $this->bot->get(Queue::class); |
59
|
|
|
|
60
|
2 |
|
if ($delay === 0) { |
61
|
1 |
|
$queue->push($this->bot->getDriver(), new SendAttachment($this->user(), $attachment)); |
62
|
|
|
} else { |
63
|
1 |
|
$queue->later($this->bot->getDriver(), new SendAttachment($this->user(), $attachment), $delay); |
64
|
|
|
} |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get user. |
69
|
|
|
* |
70
|
|
|
* @return User |
71
|
|
|
*/ |
72
|
|
|
abstract protected function user(): User; |
73
|
|
|
} |
74
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.