1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Base abstract class for supported by Telegram Method classes. |
5
|
|
|
* |
6
|
|
|
* @package Teebot (Telegram bot framework) |
7
|
|
|
* |
8
|
|
|
* @author Stanislav Drozdov <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Teebot\Method; |
12
|
|
|
|
13
|
|
|
use Teebot\Entity\AbstractEntity; |
14
|
|
|
use Teebot\Exception\Critical; |
15
|
|
|
use Teebot\Command\Executor; |
16
|
|
|
use Teebot\Exception\Output; |
17
|
|
|
use Teebot\Traits\Property; |
18
|
|
|
use Teebot\Entity\Inline\InlineKeyboardMarkup; |
19
|
|
|
use Teebot\Entity\ReplyKeyboardMarkup; |
20
|
|
|
use Teebot\Entity\ReplyKeyboardHide; |
21
|
|
|
use Teebot\Entity\ForceReply; |
22
|
|
|
|
23
|
|
|
abstract class AbstractMethod { |
24
|
|
|
|
25
|
|
|
use Property; |
26
|
|
|
|
27
|
|
|
const NAME = null; |
28
|
|
|
|
29
|
|
|
const RETURN_ENTITY = null; |
30
|
|
|
|
31
|
|
|
protected $parent = null; |
32
|
|
|
|
33
|
|
|
protected $hasAttachedData = false; |
34
|
|
|
|
35
|
|
|
protected $reply_markup; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* List of properties supported by method in format: property name => required or not |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $supportedProperties = []; |
43
|
|
|
|
44
|
|
|
protected $supportedMarkups = [ |
45
|
|
|
InlineKeyboardMarkup::class, |
46
|
|
|
ReplyKeyboardMarkup::class, |
47
|
|
|
ReplyKeyboardHide::class, |
48
|
|
|
ForceReply::class |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Constructs extended method's class and sets properties from array if passed. |
53
|
|
|
* |
54
|
|
|
* @param array $args |
55
|
|
|
*/ |
56
|
|
|
public function __construct($args = []) |
57
|
|
|
{ |
58
|
|
|
if (empty($args)) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->setProperties($args); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns method's name. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getName() |
71
|
|
|
{ |
72
|
|
|
return static::NAME; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns entity's name that should be return in method execution result. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getReturnEntity() |
81
|
|
|
{ |
82
|
|
|
return static::RETURN_ENTITY; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Triggers execution of the method |
87
|
|
|
* |
88
|
|
|
* @param bool $silentMode Execute method silently without processing the result |
89
|
|
|
* |
90
|
|
|
* @return \Teebot\Response |
91
|
|
|
*/ |
92
|
|
|
public function trigger($silentMode = true) |
93
|
|
|
{ |
94
|
|
|
$executor = Executor::getInstance(); |
95
|
|
|
|
96
|
|
|
return $executor->callRemoteMethod($this, $silentMode, $this->parent); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns flag which idicates that method has attached data (audio, voice, video, photo etc.) |
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function hasAttachedData() |
105
|
|
|
{ |
106
|
|
|
return $this->hasAttachedData; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Checks that passed markup is currently supported |
111
|
|
|
* |
112
|
|
|
* @param AbstractEntity $markup Markup class instance |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
protected function isValidMarkup(AbstractEntity $markup) |
117
|
|
|
{ |
118
|
|
|
foreach ($this->supportedMarkups as $className) { |
119
|
|
|
if ($markup instanceof $className) { |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Sets reply markup class |
129
|
|
|
* |
130
|
|
|
* @param AbstractEntity $markup Markup class instance |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function setReplyMarkup(AbstractEntity $markup) |
135
|
|
|
{ |
136
|
|
|
try { |
137
|
|
|
$isValidMarkup = $this->isValidMarkup($markup); |
138
|
|
|
|
139
|
|
|
if (!$isValidMarkup) { |
140
|
|
|
throw new Critical("Markup is not supported!"); |
141
|
|
|
} |
142
|
|
|
} catch (Critical $e) { |
143
|
|
|
Output::log($e); |
144
|
|
|
|
145
|
|
|
$markup = null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->reply_markup = $markup; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns reply markup as JSON encoded string if reply_markup is an instance of AbstractEntity |
155
|
|
|
* |
156
|
|
|
* @return string|mixed |
157
|
|
|
*/ |
158
|
|
|
public function getReplyMarkup() |
159
|
|
|
{ |
160
|
|
|
if ($this->reply_markup instanceof AbstractEntity) { |
161
|
|
|
|
162
|
|
|
return str_replace('\\\\', '\\', $this->reply_markup->asJson()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this->reply_markup; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns parent entity which triggered method's execution. |
170
|
|
|
* |
171
|
|
|
* @return AbstractEntity |
172
|
|
|
*/ |
173
|
|
|
public function getParent() |
174
|
|
|
{ |
175
|
|
|
return $this->parent; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Sets parent entity which triggered method's execution. |
180
|
|
|
* |
181
|
|
|
* @param AbstractEntity $parent |
182
|
|
|
* |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
|
|
public function setParent(AbstractEntity $parent) |
186
|
|
|
{ |
187
|
|
|
$this->parent = $parent; |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|