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
|
|
|
use PhpBotFramework\Entities\Message; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* \addtogroup Commands |
25
|
|
|
* @{ |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
/** \class MessageRegexCommand |
29
|
|
|
*/ |
30
|
|
|
class MessageRegexCommand extends BasicCommand |
31
|
|
|
{ |
32
|
|
|
/** @} */ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* \brief Add a function that will be executed everytime a message contain a command |
36
|
|
|
* that match the regular expression. |
37
|
|
|
* |
38
|
|
|
* \details Use this syntax: |
39
|
|
|
* |
40
|
|
|
* addMessageCommandRegex("number\d", function($bot, $message, $result) { |
41
|
|
|
* $bot->sendMessage("You sent me a number"); }); |
42
|
|
|
* @param string $regex_rule Regex rule that will called for evalueting the command received. |
43
|
|
|
* @param callable $script The function that will be triggered by a command. |
44
|
|
|
* Must take an object(the bot) and an array(the message received). |
45
|
|
|
*/ |
46
|
|
|
public function __construct(string $regex_rule, callable $script) |
47
|
|
|
{ |
48
|
|
|
$this->script = $script; |
49
|
|
|
$this->regex_rule = $regex_rule; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @internal |
54
|
|
|
* \brief Process the message to check if it triggers a command of this type. |
55
|
|
|
* @param array $message Message to process. |
56
|
|
|
* @return bool True if the message triggered a command. |
57
|
|
|
*/ |
58
|
|
|
public function checkCommand(array $message) : bool |
59
|
|
|
{ |
60
|
|
|
// If the message contains a bot command at the start |
61
|
|
|
$message_is_command = (isset($message['entities']) && $message['entities'][0]['type'] === 'bot_command') ? true : false; |
62
|
|
|
|
63
|
|
|
// Use preg_match to check if it is true |
64
|
|
|
if ($message_is_command && preg_match("/{$this->regex_rule}/", substr($message['text'], $message['entities'][0]['offset'] + 1, $message['entities'][0]['length']))) { |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: