1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the TelegramBot package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\TelegramBot\Commands\UserCommands; |
12
|
|
|
|
13
|
|
|
use Longman\TelegramBot\Commands\Command; |
14
|
|
|
use Longman\TelegramBot\Commands\UserCommand; |
15
|
|
|
use Longman\TelegramBot\Request; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* User "/help" command |
19
|
|
|
*/ |
20
|
|
|
class HelpCommand extends UserCommand |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $name = 'help'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = 'Show bot commands help'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $usage = '/help or /help <command>'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $version = '1.1.0'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Command execute method |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
47
|
|
|
*/ |
48
|
|
|
public function execute() |
49
|
|
|
{ |
50
|
|
|
$message = $this->getMessage(); |
51
|
|
|
$chat_id = $message->getChat()->getId(); |
52
|
|
|
|
53
|
|
|
$message_id = $message->getMessageId(); |
54
|
|
|
$command = trim($message->getText(true)); |
55
|
|
|
|
56
|
|
|
//Only get enabled Admin and User commands |
57
|
|
|
/** @var Command[] $command_objs */ |
58
|
|
|
$command_objs = array_filter($this->telegram->getCommandsList(), function ($command_obj) { |
59
|
|
|
/** @var Command $command_obj */ |
60
|
|
|
return !$command_obj->isSystemCommand() && $command_obj->isEnabled(); |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
//If no command parameter is passed, show the list |
64
|
|
|
if ($command === '') { |
65
|
|
|
$text = sprintf( |
66
|
|
|
'%s v. %s' . PHP_EOL . PHP_EOL . 'Commands List:' . PHP_EOL, |
67
|
|
|
$this->telegram->getBotName(), |
68
|
|
|
$this->telegram->getVersion() |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
foreach ($command_objs as $command) { |
72
|
|
|
if (!$command->showInHelp()) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$text .= sprintf( |
77
|
|
|
'/%s - %s' . PHP_EOL, |
78
|
|
|
$command->getName(), |
79
|
|
|
$command->getDescription() |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$text .= PHP_EOL . 'For exact command help type: /help <command>'; |
84
|
|
|
} else { |
85
|
|
|
$command = str_replace('/', '', $command); |
86
|
|
|
if (isset($command_objs[$command])) { |
87
|
|
|
/** @var Command $command_obj */ |
88
|
|
|
$command_obj = $command_objs[$command]; |
89
|
|
|
$text = sprintf( |
90
|
|
|
'Command: %s v%s' . PHP_EOL . |
91
|
|
|
'Description: %s' . PHP_EOL . |
92
|
|
|
'Usage: %s', |
93
|
|
|
$command_obj->getName(), |
94
|
|
|
$command_obj->getVersion(), |
95
|
|
|
$command_obj->getDescription(), |
96
|
|
|
$command_obj->getUsage() |
97
|
|
|
); |
98
|
|
|
} else { |
99
|
|
|
$text = 'No help available: Command /' . $command . ' not found'; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$data = [ |
104
|
|
|
'chat_id' => $chat_id, |
105
|
|
|
'reply_to_message_id' => $message_id, |
106
|
|
|
'text' => $text, |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
return Request::sendMessage($data); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|