Core: Commands not working

Created on 2 Aug 2017  路  6Comments  路  Source: php-telegram-bot/core

Required Information

  • Operating system: Windows 10
  • PHP version: 5.6.30
  • PHP Telegram Bot version: 0.46.0
  • Using MySQL database: no
  • MySQL version: no
  • Update Method: Webhook
  • Self-signed certificate: yes
  • RAW update (if available):

Expected behaviour

When I send "teste" to my bot, it should response "Testing OK"
I'm trying to test with a chat directly with the bot:
test
/test
@my_bot test
@my_bot /test

Actual behaviour

Nothing happens

Steps to reproduce

I configured my Hook file setting variable $commands_paths = [ __DIR__ . '/ComandosPersonalizados', ];, that there is only one file in this folder, called TestCommand.php.

hook.php:

<?php
// Load composer
require __DIR__ . '/vendor/autoload.php';

$API_KEY = '';
$BOT_NAME = '';
$commands_paths = [
    __DIR__ . '/ComandosPersonalizados',
];
try {
    // Create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);

    // Handle telegram webhook request
    $telegram->handle();

} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    // Silence is golden!
    // log telegram errors
    // echo $e;
}

TestCommand.php:

<?php

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'    => 'Testing OK', // Set message to send
        ];

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

All 6 comments

In hook.php I forgot to add $telegram->addCommandsPaths($commands_paths); after create telegram class

@JulyanoF you included API token in your message, I've removed it but be sure to generate new one in @BotFather.

Thankyou @jacklul, but how can I generate e new one?

Use /revoke in @BotFather.

@jacklul I found it, thank you a lot :)

Hello!
I installed php on termux but when I run php bot.php it's not working. Please help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sineverba picture sineverba  路  3Comments

noplanman picture noplanman  路  3Comments

Bl0ck154 picture Bl0ck154  路  3Comments

vansanblch picture vansanblch  路  3Comments

abbe-cipher picture abbe-cipher  路  4Comments