HelpCommand.php 2.35 KB
Newer Older
LONGMAN's avatar
LONGMAN committed
1
<?php
2
/**
LONGMAN's avatar
LONGMAN committed
3
 * This file is part of the TelegramBot package.
LONGMAN's avatar
LONGMAN committed
4 5 6 7 8
 *
 * (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
9 10
 */

11
namespace Longman\TelegramBot\Commands\UserCommands;
LONGMAN's avatar
LONGMAN committed
12

13
use Longman\TelegramBot\Commands\UserCommand;
14
use Longman\TelegramBot\Request;
LONGMAN's avatar
LONGMAN committed
15

16 17 18
/**
 * User "/help" command
 */
19
class HelpCommand extends UserCommand
LONGMAN's avatar
LONGMAN committed
20
{
21 22
    /**#@+
     * {@inheritdoc}
23
     */
24 25 26
    protected $name = 'help';
    protected $description = 'Show bot commands help';
    protected $usage = '/help or /help <command>';
27
    protected $version = '1.0.1';
28
    /**#@-*/
29

30
    /**
31
     * {@inheritdoc}
32
     */
33 34 35 36
    public function execute()
    {
        $message = $this->getMessage();
        $chat_id = $message->getChat()->getId();
37

38
        $message_id = $message->getMessageId();
39
        $command = trim($message->getText(true));
40

41 42 43 44
        //Only get enabled Admin and User commands
        $commands = array_filter($this->telegram->getCommandsList(), function ($command) {
            return (!$command->isSystemCommand() && $command->isEnabled());
        });
45

46 47 48 49
        //If no command parameter is passed, show the list
        if ($command === '') {
            $text = $this->telegram->getBotName() . ' v. ' . $this->telegram->getVersion() . "\n\n";
            $text .= 'Commands List:' . "\n";
50
            foreach ($commands as $command) {
51
                $text .= '/' . $command->getName() . ' - ' . $command->getDescription() . "\n";
52 53
            }

54
            $text .= "\n" . 'For exact command help type: /help <command>';
55
        } else {
56 57 58 59 60 61
            $command = str_replace('/', '', $command);
            if (isset($commands[$command])) {
                $command = $commands[$command];
                $text = 'Command: ' . $command->getName() . ' v' . $command->getVersion() . "\n";
                $text .= 'Description: ' . $command->getDescription() . "\n";
                $text .= 'Usage: ' . $command->getUsage();
62
            } else {
63
                $text = 'No help available: Command /' . $command . ' not found';
64 65 66
            }
        }

67 68 69
        $data = [
            'chat_id'             => $chat_id,
            'reply_to_message_id' => $message_id,
70
            'text'                => $text,
71
        ];
72

73
        return Request::sendMessage($data);
74
    }
LONGMAN's avatar
LONGMAN committed
75
}