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 CallbackCommand |
27
|
|
|
*/ |
28
|
|
|
class CallbackCommand extends BasicCommand |
29
|
|
|
{ |
30
|
|
|
/** @} */ |
31
|
|
|
|
32
|
|
|
public static $type = 'callback_query'; |
33
|
|
|
|
34
|
|
|
public static $object_class = 'PhpBotFramework\Entities\CallbackQuery'; |
35
|
|
|
|
36
|
|
|
public static $priority = 1; |
37
|
|
|
|
38
|
|
|
private $data; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* \brief Add a function that will be executed everytime a callback query contains a string as data |
42
|
|
|
* \details Use this syntax: |
43
|
|
|
* |
44
|
|
|
* $bot->addCallbackCommand("menu", function($bot, $callback_query) { |
45
|
|
|
* $bot->editMessageText($callback_query['message']['message_id'], "This is the menu"); |
46
|
|
|
* }); |
47
|
|
|
* |
48
|
|
|
* @param string $data The string that will trigger this function. |
49
|
|
|
* @param callable $script The function that will be triggered by the callback query if it contains |
50
|
|
|
* the $data string. Must take an object(the bot) and an array(the callback query received). |
51
|
|
|
*/ |
52
|
|
|
public function __construct(string $data, callable $script) |
53
|
|
|
{ |
54
|
|
|
$this->data = $data; |
55
|
|
|
$this->script = $script; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* \brief (<i>Internal</i>) Process the callback query and check if it triggers a command of this type. |
60
|
|
|
* @param array $callback_query Callback query to process. |
61
|
|
|
* @return bool True if the callback query triggered a command. |
62
|
|
|
*/ |
63
|
|
|
public function checkCommand(array $callback_query) : bool |
64
|
|
|
{ |
65
|
|
|
if (isset($callback_query['data'])) { |
66
|
|
|
// If command is found in callback data |
67
|
|
|
if (strpos($this->data, $callback_query['data']) !== false) { |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @} */ |
76
|
|
|
|
77
|
|
|
/** @} */ |
78
|
|
|
} |
79
|
|
|
|