1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the TelegramBot package. |
5
|
|
|
* |
6
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Longman\TelegramBot\Commands; |
13
|
|
|
|
14
|
|
|
use Longman\TelegramBot\Conversation; |
15
|
|
|
use Longman\TelegramBot\Entities\ServerResponse; |
16
|
|
|
use Longman\TelegramBot\Exception\TelegramException; |
17
|
|
|
use Longman\TelegramBot\Request; |
18
|
|
|
|
19
|
|
|
abstract class SystemCommand extends Command |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var bool Try to execute any deprecated system command. |
23
|
|
|
*/ |
24
|
|
|
public static $execute_deprecated = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @{inheritdoc} |
28
|
|
|
* |
29
|
|
|
* Set to empty string to disallow users calling system commands. |
30
|
|
|
*/ |
31
|
|
|
protected $usage = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A system command just executes |
35
|
|
|
* |
36
|
|
|
* Although system commands should just work and return a successful ServerResponse, |
37
|
|
|
* each system command can override this method to add custom functionality. |
38
|
|
|
* |
39
|
|
|
* @return ServerResponse |
40
|
|
|
*/ |
41
|
|
|
public function execute(): ServerResponse |
42
|
|
|
{ |
43
|
|
|
// System command, return empty ServerResponse by default |
44
|
|
|
return Request::emptyResponse(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Method to execute any active conversation. |
49
|
|
|
* |
50
|
|
|
* @return ServerResponse|null |
51
|
|
|
* @throws TelegramException |
52
|
|
|
* @internal |
53
|
|
|
*/ |
54
|
|
|
protected function executeActiveConversation(): ?ServerResponse |
55
|
|
|
{ |
56
|
|
|
$message = $this->getMessage() ?: $this->getEditedMessage() ?: $this->getChannelPost() ?: $this->getEditedChannelPost(); |
57
|
|
|
if ($message === null) { |
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$user = $message->getFrom(); |
62
|
|
|
$chat = $message->getChat(); |
63
|
|
|
if ($user === null || $chat === null) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// If a conversation is busy, execute the conversation command after handling the message. |
68
|
|
|
$conversation = new Conversation($user->getId(), $chat->getId()); |
69
|
|
|
|
70
|
|
|
// Fetch conversation command if it exists and execute it. |
71
|
|
|
if ($conversation->exists() && ($command = $conversation->getCommand())) { |
72
|
|
|
return $this->getTelegram()->executeCommand($command); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* BC helper method to execute deprecated system commands. |
80
|
|
|
* |
81
|
|
|
* @return ServerResponse|null |
82
|
|
|
* @throws TelegramException |
83
|
|
|
* @internal |
84
|
|
|
*/ |
85
|
|
|
protected function executeDeprecatedSystemCommand(): ?ServerResponse |
86
|
|
|
{ |
87
|
|
|
$message = $this->getMessage() ?: $this->getEditedMessage() ?: $this->getChannelPost() ?: $this->getEditedChannelPost(); |
88
|
|
|
if ($message === null) { |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// List of service messages previously handled internally. |
93
|
|
|
$service_message_getters = [ |
94
|
|
|
'newchatmembers' => 'getNewChatMembers', |
95
|
|
|
'leftchatmember' => 'getLeftChatMember', |
96
|
|
|
'newchattitle' => 'getNewChatTitle', |
97
|
|
|
'newchatphoto' => 'getNewChatPhoto', |
98
|
|
|
'deletechatphoto' => 'getDeleteChatPhoto', |
99
|
|
|
'groupchatcreated' => 'getGroupChatCreated', |
100
|
|
|
'supergroupchatcreated' => 'getSupergroupChatCreated', |
101
|
|
|
'channelchatcreated' => 'getChannelChatCreated', |
102
|
|
|
'migratefromchatid' => 'getMigrateFromChatId', |
103
|
|
|
'migratetochatid' => 'getMigrateToChatId', |
104
|
|
|
'pinnedmessage' => 'getPinnedMessage', |
105
|
|
|
'successfulpayment' => 'getSuccessfulPayment', |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
foreach ($service_message_getters as $command => $service_message_getter) { |
109
|
|
|
// Let's check if this message is a service message. |
110
|
|
|
if ($message->$service_message_getter() === null) { |
111
|
|
|
continue; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Make sure the command exists otherwise GenericCommand would be executed. |
115
|
|
|
if ($this->getTelegram()->getCommandObject($command) === null) { |
116
|
|
|
break; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->getTelegram()->executeCommand($command); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return null; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|