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\Commands; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* \addtogroup Commands |
23
|
|
|
* @{ |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
/** \class AdminCommand |
27
|
|
|
*/ |
28
|
|
|
class AdminCommand extends MessageCommand |
29
|
|
|
{ |
30
|
|
|
/** @} */ |
31
|
|
|
|
32
|
|
|
public static $type = 'message'; |
33
|
|
|
|
34
|
|
|
public static $object_class = 'PhpBotFramework\Entities\Message'; |
35
|
|
|
|
36
|
|
|
public static $priority = 1; |
37
|
|
|
|
38
|
|
|
private $command; |
|
|
|
|
39
|
|
|
|
40
|
|
|
private $length; |
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* \brief Registers commands that can be triggered only by administrators. |
44
|
|
|
* \details It works like <code>MessageCommand</code> but it requires a |
45
|
|
|
* third argument: the list of Telegram IDs which represents the users |
46
|
|
|
* that can execute the command. |
47
|
|
|
* |
48
|
|
|
* $start_command = new PhpBotFramework\Commands\AdminCommand("getusers", |
49
|
|
|
* function ($bot, $message) { |
50
|
|
|
* $bot->sendMessage("Hello, folks!"); |
51
|
|
|
* }, |
52
|
|
|
* array(3299130043, -439991220, 12221004)); |
53
|
|
|
* |
54
|
|
|
* @param string $command The command that will trigger this function (e.g. start) |
55
|
|
|
* @param callable $script The function that will be triggered by a command. |
56
|
|
|
* @param array $ids The users who can execute the command. |
57
|
|
|
* Must take an object(the bot) and an array(the message received). |
58
|
|
|
*/ |
59
|
|
|
public function __construct(string $command, callable $script, array $ids) |
60
|
|
|
{ |
61
|
|
|
$this->command = "/$command"; |
62
|
|
|
$this->length = strlen($command) + 1; |
63
|
|
|
$this->script = $script; |
64
|
|
|
|
65
|
|
|
$this->ids = $ids; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @internal |
70
|
|
|
* \brief Process a message checking if it trigger any MessageCommand. |
71
|
|
|
* @param string $message Message to process. |
72
|
|
|
* @return bool True if the message trigger any command. |
73
|
|
|
*/ |
74
|
|
|
public function checkCommand(array $message) : bool |
75
|
|
|
{ |
76
|
|
|
// If the message contains a bot command at the start |
77
|
|
|
$message_is_command = (isset($message['entities']) && $message['entities'][0]['type'] === 'bot_command') ? true : false; |
78
|
|
|
|
79
|
|
|
// If we found a valid command (check first lenght, then use strpos) |
80
|
|
|
if ($message_is_command && $this->length == $message['entities'][0]['length'] && |
81
|
|
|
mb_strpos($this->command, $message['text'], $message['entities'][0]['offset']) !== false) { |
82
|
|
|
// Check that the user can execute the command |
83
|
|
|
if (in_array($message['from']['id'], $this->ids)) { |
84
|
|
|
return true; |
85
|
|
|
} else { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|