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 Modules |
23
|
|
|
* @{ |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
/** \class CallbackCommandHandler |
27
|
|
|
*/ |
28
|
|
|
trait CommandHandler |
29
|
|
|
{ |
30
|
|
|
/** @} */ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* \addtogroup Commands |
34
|
|
|
* \brief What commands are |
35
|
|
|
* @{ |
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
/** \brief (<i>Internal</i>)contains all command that can trigger the bot. |
39
|
|
|
* \details E.g. Add each type of command processed by the bot into this array to avoid overhead. */ |
40
|
|
|
protected $_command_types; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* \brief (<i>Internal</i>) Init commands to speed up processing. |
44
|
|
|
* \details Get all command that the bot handle, and put them in priority. |
45
|
|
|
*/ |
46
|
|
|
protected function initCommands() |
47
|
|
|
{ |
48
|
|
|
// All command types with respective update |
49
|
|
|
static $commands = ['MessageCommand' => |
50
|
|
|
['var' => '_message_commands', 'update' => 'message', 'prior' => '1'], |
51
|
|
|
'CallbackCommand' => |
52
|
|
|
['var' => '_callback_commands', 'update' => 'callback_query', 'prior' => '1'], |
53
|
|
|
'MessageRegexCommand' => ['var' => '_message_regex_commands', 'update' => 'message', 'prior' => '2']]; |
54
|
|
|
|
55
|
|
|
// Sort them by priority |
56
|
|
|
uasort($commands, 'PhpBotFramework\Commands\CommandHandler::sortingPrior'); |
57
|
|
|
|
58
|
|
|
// Iterate over each |
59
|
|
|
foreach ($commands as $index => $command) { |
60
|
|
|
// If there is at least a command of that type (by checking that the container exists and it is not empty) |
61
|
|
|
if (isset($this->{$command['var']}) && !empty($this->{$command['var']})) { |
62
|
|
|
// Add the type to the command container |
63
|
|
|
$this->_command_types[] = ['method' => "process$index", 'update' => $command['update']]; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* \brief Process updates handling first commands, and then general methods (e.g. BaseBot::processMessage()) |
70
|
|
|
* @param $update Update to process. |
71
|
|
|
* @return Id of the update processed. |
72
|
|
|
*/ |
73
|
|
|
protected function processUpdate(array $update) : int |
74
|
|
|
{ |
75
|
|
|
// For each command active (checked by initCommands()) |
76
|
|
|
foreach ($this->_command_types as $index => $command) { |
77
|
|
|
// If the update type is right and the update triggered a command |
78
|
|
|
if (isset($update[$command['update']]) && $this->{$command['method']}($update[$command['update']])) { |
79
|
|
|
// Return the id as we already processed this update |
80
|
|
|
return $update['update_id']; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Call the parent method because this update didn't trigger any command |
85
|
|
|
return parent::processUpdate($update); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Use to sort _command_types based on prior |
89
|
|
|
public static function sortingPrior($a, $b) |
90
|
|
|
{ |
91
|
|
|
if ($a['prior'] > $b['prior']) { |
92
|
|
|
return 1; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($a['prior'] < $b['prior']) { |
96
|
|
|
return -1; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($a['prior'] == $b['prior']) { |
100
|
|
|
return 0; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** @} */ |
105
|
|
|
} |
106
|
|
|
|