M
M
Maxim Lagoysky2019-03-06 16:42:22
PHP
Maxim Lagoysky, 2019-03-06 16:42:22

How to respond to commands with a bot?

Good afternoon everyone, I want to create a bot using the longman/telegram-bot library . I create my own team by example.

namespace Longman\TelegramBot\Commands\UserCommands;

use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;

class TestCommand extends UserCommand
{
    protected $name = 'test';                      // Your command's name
    protected $description = 'A command for test'; // Your command description
    protected $usage = '/test';                    // Usage of your command
    protected $version = '1.0.0';                  // Version of your command

    public function execute()
    {
        $message = $this->getMessage();            // Get Message object
        $chat_id = $message->getChat()->getId();   // Get the current Chat ID

        $data = [                                  // Set up the new message data
            'chat_id' => $chat_id,                 // Set Chat ID to send the message to
            'text'    => 'This is just a Test...', // Set message to send
        ];

        return Request::sendMessage($data);        // Send message!
    }
}

in the file on which I hung the webhook I write the following code
$bot_api_key  = 'your:bot_api_key';
$bot_username = 'username_bot';

$admin_users = [
//    123,
];
$commands_paths = [
    __DIR__ . '/TelegramCommands/',
];

try {
    // Create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
    // Add commands paths containing your custom commands
    $telegram->addCommandsPaths($commands_paths);
    // Enable admin users
    $telegram->enableAdmins($admin_users);
    // Handle telegram webhook request
    $telegram->handle();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    // Silence is golden!
    //echo $e;
    // Log telegram errors
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
    // Silence is golden!
    // Uncomment this to catch log initialisation errors
    //echo $e;
}

And I don't understand how to call my TestCommand when I send /test in the bot. I rummaged through the methods and realized that the method
$telegram->handleGetUpdates();
returns the response object to me. Actually the question is how to do this so that the bot would respond to my commands?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question