1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Abstract entity event class is base class for creating the events for received in response entities. It |
5
|
|
|
* includes methods to be able to quickly get a chat id and reply on message, get parent entity which |
6
|
|
|
* triggered this entity event. |
7
|
|
|
* |
8
|
|
|
* @package Teebot (Telegram bot framework) |
9
|
|
|
* |
10
|
|
|
* @author Stanislav Drozdov <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Teebot\Api\Command; |
14
|
|
|
|
15
|
|
|
use Teebot\Api\Entity\EntityInterface; |
16
|
|
|
use Teebot\Api\Method\SendMessage; |
17
|
|
|
use Teebot\Api\Entity\Message; |
18
|
|
|
|
19
|
|
|
abstract class AbstractEntityEvent implements EventInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Processor $processor |
23
|
|
|
*/ |
24
|
|
|
protected $processor; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $params; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var EntityInterface $entity |
33
|
|
|
*/ |
34
|
|
|
protected $entity = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Abstract method of each entity event which will be called if certain entity presents in response |
38
|
|
|
* and it's class exists or mapped in the config. |
39
|
|
|
* |
40
|
|
|
* @return null|bool |
41
|
|
|
*/ |
42
|
|
|
abstract public function run(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Sets configuration parameters |
46
|
|
|
* |
47
|
|
|
* @param array $params |
48
|
|
|
* |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function setParams(array $params) |
52
|
|
|
{ |
53
|
|
|
$this->params = $params; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns chat id if the type of calling entity or it's parent is Message |
60
|
|
|
* |
61
|
|
|
* @return null|int |
62
|
|
|
*/ |
63
|
|
|
public function getChatId() |
64
|
|
|
{ |
65
|
|
|
if ($this->entity instanceof Message) { |
66
|
|
|
return $this->entity->getChatId(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$parent = $this->entity->getParent(); |
70
|
|
|
|
71
|
|
|
if ($parent instanceof Message) { |
72
|
|
|
return $parent->getChatId(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Processor $processor |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function setProcessor(Processor $processor) |
84
|
|
|
{ |
85
|
|
|
$this->processor = $processor; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Sets entity |
92
|
|
|
* |
93
|
|
|
* @param EntityInterface $entity Entity object |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function setEntity(EntityInterface $entity) |
98
|
|
|
{ |
99
|
|
|
$this->entity = $entity; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Sends text message to current chat |
106
|
|
|
* |
107
|
|
|
* @param string $text Message text |
108
|
|
|
* |
109
|
|
|
* @return bool|\Teebot\Api\Response |
110
|
|
|
*/ |
111
|
|
|
protected function sendMessage($text) |
112
|
|
|
{ |
113
|
|
|
return $this->reply( |
114
|
|
|
(new SendMessage()) |
115
|
|
|
->setText($text) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Replies on the message |
121
|
|
|
* |
122
|
|
|
* @param SendMessage $sendMessage Object of the SendMessage method |
123
|
|
|
* |
124
|
|
|
* @return bool|\Teebot\Api\Response |
125
|
|
|
*/ |
126
|
|
|
protected function reply(SendMessage $sendMessage) { |
127
|
|
|
$chatId = $this->getChatId(); |
128
|
|
|
|
129
|
|
|
if ((int) $chatId == 0) { |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$sendMessage->setChatId($chatId); |
134
|
|
|
|
135
|
|
|
return $this->processor->call($sendMessage, true, $this->entity); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|