-
-
Notifications
You must be signed in to change notification settings - Fork 962
/
Copy pathCommand.php
450 lines (400 loc) · 11.4 KB
/
Command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\CallbackQuery;
use Longman\TelegramBot\Entities\ChatJoinRequest;
use Longman\TelegramBot\Entities\ChatMemberUpdated;
use Longman\TelegramBot\Entities\ChosenInlineResult;
use Longman\TelegramBot\Entities\InlineQuery;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\Payments\PreCheckoutQuery;
use Longman\TelegramBot\Entities\Payments\ShippingQuery;
use Longman\TelegramBot\Entities\Poll;
use Longman\TelegramBot\Entities\PollAnswer;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
/**
* Class Command
*
* Base class for commands. It includes some helper methods that can fetch data directly from the Update object.
*
* @method Message getMessage() Optional. New incoming message of any kind — text, photo, sticker, etc.
* @method Message getEditedMessage() Optional. New version of a message that is known to the bot and was edited
* @method Message getChannelPost() Optional. New post in the channel, can be any kind — text, photo, sticker, etc.
* @method Message getEditedChannelPost() Optional. New version of a post in the channel that is known to the bot and was edited
* @method InlineQuery getInlineQuery() Optional. New incoming inline query
* @method ChosenInlineResult getChosenInlineResult() Optional. The result of an inline query that was chosen by a user and sent to their chat partner.
* @method CallbackQuery getCallbackQuery() Optional. New incoming callback query
* @method ShippingQuery getShippingQuery() Optional. New incoming shipping query. Only for invoices with flexible price
* @method PreCheckoutQuery getPreCheckoutQuery() Optional. New incoming pre-checkout query. Contains full information about checkout
* @method Poll getPoll() Optional. New poll state. Bots receive only updates about polls, which are sent or stopped by the bot
* @method PollAnswer getPollAnswer() Optional. A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself.
* @method ChatMemberUpdated getMyChatMember() Optional. The bot's chat member status was updated in a chat. For private chats, this update is received only when the bot is blocked or unblocked by the user.
* @method ChatMemberUpdated getChatMember() Optional. A chat member's status was updated in a chat. The bot must be an administrator in the chat and must explicitly specify “chat_member” in the list of allowed_updates to receive these updates.
* @method ChatJoinRequest getChatJoinRequest() Optional. A request to join the chat has been sent. The bot must have the can_invite_users administrator right in the chat to receive these updates.
*/
abstract class Command
{
/**
* Auth level for user commands
*/
public const AUTH_USER = 'User';
/**
* Auth level for system commands
*/
public const AUTH_SYSTEM = 'System';
/**
* Auth level for admin commands
*/
public const AUTH_ADMIN = 'Admin';
/**
* Telegram object
*
* @var Telegram
*/
protected $telegram;
/**
* Update object
*
* @var Update
*/
protected $update;
/**
* Name
*
* @var string
*/
protected $name = '';
/**
* Description
*
* @var string
*/
protected $description = 'Command description';
/**
* Usage
*
* @var string
*/
protected $usage = 'Command usage';
/**
* Show in Help
*
* @var bool
*/
protected $show_in_help = true;
/**
* Version
*
* @var string
*/
protected $version = '1.0.0';
/**
* If this command is enabled
*
* @var bool
*/
protected $enabled = true;
/**
* If this command needs mysql
*
* @var bool
*/
protected $need_mysql = false;
/*
* Make sure this command only executes on a private chat.
*
* @var bool
*/
protected $private_only = false;
/**
* Command config
*
* @var array
*/
protected $config = [];
/**
* Constructor
*
* @param Telegram $telegram
* @param Update|null $update
*/
public function __construct(Telegram $telegram, ?Update $update = null)
{
$this->telegram = $telegram;
if ($update !== null) {
$this->setUpdate($update);
}
$this->config = $telegram->getCommandConfig($this->name);
}
/**
* Set update object
*
* @param Update $update
*
* @return Command
*/
public function setUpdate(Update $update): Command
{
$this->update = $update;
return $this;
}
/**
* Pre-execute command
*
* @return ServerResponse
* @throws TelegramException
*/
public function preExecute(): ServerResponse
{
if ($this->need_mysql && !($this->telegram->isDbEnabled() && DB::isDbConnected())) {
return $this->executeNoDb();
}
if ($this->isPrivateOnly() && $this->removeNonPrivateMessage()) {
$message = $this->getMessage();
if ($user = $message->getFrom()) {
return Request::sendMessage([
'chat_id' => $user->getId(),
'parse_mode' => 'Markdown',
'text' => sprintf(
"/%s command is only available in a private chat.\n(`%s`)",
$this->getName(),
$message->getText()
),
]);
}
return Request::emptyResponse();
}
return $this->execute();
}
/**
* Execute command
*
* @return ServerResponse
* @throws TelegramException
*/
abstract public function execute(): ServerResponse;
/**
* Execution if MySQL is required but not available
*
* @return ServerResponse
* @throws TelegramException
*/
public function executeNoDb(): ServerResponse
{
return $this->replyToChat('Sorry no database connection, unable to execute "' . $this->name . '" command.');
}
/**
* Get update object
*
* @return Update|null
*/
public function getUpdate(): ?Update
{
return $this->update;
}
/**
* Relay any non-existing function calls to Update object.
*
* This is purely a helper method to make requests from within execute() method easier.
*
* @param string $name
* @param array $arguments
*
* @return Command
*/
public function __call(string $name, array $arguments)
{
if ($this->update === null) {
return null;
}
return call_user_func_array([$this->update, $name], $arguments);
}
/**
* Get command config
*
* Look for config $name if found return it, if not return $default.
* If $name is not set return all set config.
*
* @param string|null $name
* @param mixed $default
*
* @return mixed
*/
public function getConfig(?string $name = null, $default = null)
{
if ($name === null) {
return $this->config;
}
return $this->config[$name] ?? $default;
}
/**
* Get telegram object
*
* @return Telegram
*/
public function getTelegram(): Telegram
{
return $this->telegram;
}
/**
* Get usage
*
* @return string
*/
public function getUsage(): string
{
return $this->usage;
}
/**
* Get version
*
* @return string
*/
public function getVersion(): string
{
return $this->version;
}
/**
* Get description
*
* @return string
*/
public function getDescription(): string
{
return $this->description;
}
/**
* Get name
*
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* Get Show in Help
*
* @return bool
*/
public function showInHelp(): bool
{
return $this->show_in_help;
}
/**
* Check if command is enabled
*
* @return bool
*/
public function isEnabled(): bool
{
return $this->enabled;
}
/**
* If this command is intended for private chats only.
*
* @return bool
*/
public function isPrivateOnly(): bool
{
return $this->private_only;
}
/**
* If this is a SystemCommand
*
* @return bool
*/
public function isSystemCommand(): bool
{
return ($this instanceof SystemCommand);
}
/**
* If this is an AdminCommand
*
* @return bool
*/
public function isAdminCommand(): bool
{
return ($this instanceof AdminCommand);
}
/**
* If this is a UserCommand
*
* @return bool
*/
public function isUserCommand(): bool
{
return ($this instanceof UserCommand);
}
/**
* Delete the current message if it has been called in a non-private chat.
*
* @return bool
*/
protected function removeNonPrivateMessage(): bool
{
$message = $this->getMessage() ?: $this->getEditedMessage();
if ($message) {
$chat = $message->getChat();
if (!$chat->isPrivateChat()) {
// Delete the falsely called command message.
Request::deleteMessage([
'chat_id' => $chat->getId(),
'message_id' => $message->getMessageId(),
]);
return true;
}
}
return false;
}
/**
* Helper to reply to a chat directly.
*
* @param string $text
* @param array $data
*
* @return ServerResponse
* @throws TelegramException
*/
public function replyToChat(string $text, array $data = []): ServerResponse
{
if ($message = $this->getMessage() ?: $this->getEditedMessage() ?: $this->getChannelPost() ?: $this->getEditedChannelPost()) {
return Request::sendMessage(array_merge([
'chat_id' => $message->getChat()->getId(),
'text' => $text,
], $data));
}
return Request::emptyResponse();
}
/**
* Helper to reply to a user directly.
*
* @param string $text
* @param array $data
*
* @return ServerResponse
* @throws TelegramException
*/
public function replyToUser(string $text, array $data = []): ServerResponse
{
if ($message = $this->getMessage() ?: $this->getEditedMessage()) {
return Request::sendMessage(array_merge([
'chat_id' => $message->getFrom()->getId(),
'text' => $text,
], $data));
}
return Request::emptyResponse();
}
}