1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpBotFramework. |
5
|
|
|
* |
6
|
|
|
* PhpBotFramework is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, version 3. |
9
|
|
|
* |
10
|
|
|
* PhpBotFramework is distributed in the hope that it will be useful, but |
11
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13
|
|
|
* Lesser General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace PhpBotFramework; |
20
|
|
|
|
21
|
|
|
use PhpBotFramework\Exceptions\BotException; |
22
|
|
|
|
23
|
|
|
use PhpBotFramework\Entities\Message; |
24
|
|
|
use PhpBotFramework\Entities\CallbackQuery; |
25
|
|
|
use PhpBotFramework\Entities\ChosenInlineResult; |
26
|
|
|
use PhpBotFramework\Entities\InlineQuery; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* \class Bot Bot |
30
|
|
|
* \brief Bot class to handle updates and commands. |
31
|
|
|
* \details Class Bot to handle task like API request, or more specific API method like sendMessage, editMessageText, etc.. |
32
|
|
|
* An example of its usage is available in webhook.php |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
class BasicBot extends Core\CoreBot |
36
|
|
|
{ |
37
|
|
|
use \PhpBotFramework\Commands\CommandHandler; |
38
|
|
|
|
39
|
|
|
/** @internal |
40
|
|
|
* \brief True if the bot is using webhook? */ |
41
|
|
|
protected $_is_webhook; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* \brief Construct an empty base bot. |
45
|
|
|
* \details Construct a base bot that can handle updates. |
46
|
|
|
*/ |
47
|
1 |
|
public function __construct(string $token) |
48
|
|
|
{ |
49
|
1 |
|
parent::__construct($token); |
50
|
|
|
|
51
|
|
|
// Add alias for entity classes |
52
|
1 |
|
class_alias('PhpBotFramework\Entities\Message', 'PhpBotFramework\Entities\EditedMessage'); |
53
|
1 |
|
class_alias('PhpBotFramework\Entities\Message', 'PhpBotFramework\Entities\ChannelPost'); |
54
|
1 |
|
class_alias('PhpBotFramework\Entities\Message', 'PhpBotFramework\Entities\EditedChannelPost'); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** @} */ |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* \addtogroup Bot |
61
|
|
|
* @{ |
62
|
|
|
*/ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* \brief Get update and process it. |
66
|
|
|
* \details Call this method if user is using webhook. |
67
|
|
|
* It'll get bot's update from php::\input, check it and then process it using <b>processUpdate</b>. |
68
|
|
|
*/ |
69
|
|
|
public function processWebhookUpdate() |
70
|
|
|
{ |
71
|
|
|
$this->_is_webhook = true; |
72
|
|
|
|
73
|
|
|
$this->initCommands(); |
74
|
|
|
$this->processUpdate(json_decode(file_get_contents('php://input'), true)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* \brief Get updates received by the bot, and hold the offset in $offset. |
79
|
|
|
* \details Get the <code>update_id</code> of the first update to parse, set it in $offset and |
80
|
|
|
* then it start an infinite loop where it processes updates and keep $offset on the update_id of the last update received. |
81
|
|
|
* Each processUpdate() method call is surrounded by a try/catch. |
82
|
|
|
* @see getUpdates |
83
|
|
|
* @param int $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
84
|
|
|
* @param int $timeout <i>Optional</i>. Timeout in seconds for long polling. |
85
|
|
|
*/ |
86
|
|
|
public function getUpdatesLocal(int $limit = 100, int $timeout = 60) |
87
|
|
|
{ |
88
|
|
|
$update = []; |
|
|
|
|
89
|
|
|
|
90
|
|
|
// While there aren't updates to process |
91
|
|
|
while (empty($update = $this->getUpdates(0, 1))) { |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$offset = $update[0]['update_id']; |
95
|
|
|
$this->initCommands(); |
96
|
|
|
|
97
|
|
|
// Process all updates |
98
|
|
|
while (true) { |
99
|
|
|
$updates = $this->execRequest("getUpdates?offset=$offset&limit=$limit&timeout=$timeout"); |
100
|
|
|
|
101
|
|
|
foreach ($updates as $key => $update) { |
|
|
|
|
102
|
|
|
try { |
103
|
|
|
$this->processUpdate($update); |
104
|
|
|
} catch (BotException $e) { |
105
|
|
|
echo $e->getMessage(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$offset += sizeof($updates); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** @} */ |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @internal |
117
|
|
|
* \brief Dispatch each update to the right method (processMessage, processCallbackQuery, etc). |
118
|
|
|
* \details Set $chat_id for each update, $text, $data and $query are set for each update that contains them. |
119
|
|
|
* @param array $update Reference to the update received. |
120
|
|
|
* @return int The id of the update processed. |
121
|
|
|
*/ |
122
|
5 |
|
protected function processUpdate(array $update) : int |
123
|
|
|
{ |
124
|
5 |
|
static $updates_type = [ |
125
|
|
|
'message' => 'Message', |
126
|
|
|
'callback_query' => 'CallbackQuery', |
127
|
|
|
'inline_query' => 'InlineQuery', |
128
|
|
|
'channel_post' => 'ChannelPost', |
129
|
|
|
'edited_message' => 'EditedMessage', |
130
|
|
|
'edited_channel_post' => 'EditedChannelPost', |
131
|
|
|
'chosen_inline_result' => 'ChosenInlineResult', |
132
|
|
|
'pre_checkout_query' => 'PreCheckoutQuery', |
133
|
|
|
'shipping_query' => 'ShippingQuery' |
134
|
|
|
]; |
135
|
|
|
|
136
|
5 |
|
if ($this->processCommands($update)) { |
137
|
3 |
|
return $update['update_id']; |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
foreach ($updates_type as $offset => $class) { |
141
|
2 |
|
if (isset($update[$offset])) { |
142
|
2 |
|
$object_class = "PhpBotFramework\Entities\\$class"; |
143
|
2 |
|
$object = new $object_class($update[$offset]); |
144
|
|
|
|
145
|
2 |
|
$this->_chat_id = $object->getChatID(); |
146
|
|
|
|
147
|
2 |
|
if (method_exists($object, 'getBotParameter')) { |
148
|
|
|
$var = $object->getBotParameter(); |
149
|
|
|
$this->{$var['var']} = $var['id']; |
150
|
|
|
} |
151
|
|
|
|
152
|
2 |
|
$this->{"process$class"}($object); |
153
|
|
|
|
154
|
2 |
|
return $update['update_id']; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* \brief Called every message received by the bot. |
161
|
|
|
* \details Override it to script the bot answer for each message. |
162
|
|
|
* <code>$chat_id</code> and <code>$text</code>, if the message contains text(use getMessageText() to access it), set inside of this function. |
163
|
|
|
* @param Message $message Reference to the message received. |
164
|
|
|
*/ |
165
|
|
|
protected function processMessage(Message $message) |
166
|
|
|
{ |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* \brief Process updates which involve PreCheckout queries (part of Payments API). |
171
|
|
|
* \details Ovveride it to script the bot answer for each PreCheckout query. |
172
|
|
|
* @param PreCheckoutQuery $pre_checkout_query Reference to the query received. |
173
|
|
|
*/ |
174
|
|
|
protected function processPreCheckoutQuery(PreCheckoutQuery $pre_checkout_query) |
175
|
|
|
{ |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* \brief Process updates which involve shipping queries (part of Payments API). |
180
|
|
|
* \details Ovveride it to script the bot answer for each shipping query. |
181
|
|
|
* @param ShippingQuery $shipping_query Reference to the query received. |
182
|
|
|
*/ |
183
|
|
|
protected function processShippingQuery(ShippingQuery $shipping_query) |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* \brief Called every callback query received by the bot. |
190
|
|
|
* \details Override it to script the bot answer for each callback. |
191
|
|
|
* <code>$chat_id</code> and <code>$data</code>, if set in the callback query(use getCallbackData() to access it) set inside of this function. |
192
|
|
|
* @param CallbackQuery $callback_query Reference to the callback query received. |
193
|
|
|
*/ |
194
|
|
|
protected function processCallbackQuery(CallbackQuery $callback_query) |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* \brief Called every inline query received by the bot. |
200
|
|
|
* \details Override it to script the bot answer for each inline query. |
201
|
|
|
* $chat_id and $query(use getInlineQuery() to access it) set inside of this function. |
202
|
|
|
* @param InlineQuery $inline_query Reference to the inline query received. |
203
|
|
|
*/ |
204
|
|
|
protected function processInlineQuery(InlineQuery $inline_query) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* \brief Called every chosen inline result received by the bot. |
210
|
|
|
* \details Override it to script the bot answer for each chosen inline result. |
211
|
|
|
* <code>$chat_id</code> set inside of this function. |
212
|
|
|
* @param ChosenInlineResult $chosen_inline_result Reference to the chosen inline result received. |
213
|
|
|
*/ |
214
|
|
|
protected function processChosenInlineResult(ChosenInlineResult $chosen_inline_result) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* \brief Called every chosen edited message received by the bot. |
220
|
|
|
* \details Override it to script the bot answer for each edited message. |
221
|
|
|
* <code>$chat_id</code> set inside of this function. |
222
|
|
|
* @param Message $edited_message The message edited by the user. |
223
|
|
|
*/ |
224
|
|
|
protected function processEditedMessage(Message $edited_message) |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* \brief Called every new post in the channel where the bot is in. |
230
|
|
|
* \details Override it to script the bot answer for each post sent in a channel. |
231
|
|
|
* <code>$chat_id</code> set inside of this function. |
232
|
|
|
* @param Message $post The message sent in the channel. |
233
|
|
|
*/ |
234
|
|
|
protected function processChannelPost(Message $post) |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* \brief Called every time a post get edited in the channel where the bot is in. |
240
|
|
|
* \details Override it to script the bot answer for each post edited in a channel. |
241
|
|
|
* <code>$chat_id</code> set inside of this function. |
242
|
|
|
* @param Message $post The message edited in the channel. |
|
|
|
|
243
|
|
|
*/ |
244
|
|
|
protected function processEditedChannelPost(Message $edited_post) |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** @} */ |
249
|
|
|
} |
250
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.