TelegramBot / Api

Native PHP Wrapper for Telegram BOT API
MIT License
1.07k stars 324 forks source link

Syntax Error in repons to quite valid request #316

Open H1l4nd0r opened 3 years ago

H1l4nd0r commented 3 years ago

I'm making requests from local machine to bot web hook

Request data {"update_id":49608015, "message":{"message_id":344,"from":{"id":118265506,"is_bot":false,"first_name":"ChEl","username":"h1l4nd0r","language_code":"ru"},"chat":{"id":118265506,"first_name":"ChEl","username":"h1l4nd0r","type":"private"},"date":1612448172,"text":"kkjlk"}}

and Here is my code

header('Content-Type: text/json; charset=utf-8'); require DIR . '/config.php';

define('APIKEY', '....');

IngineTools::logIt('bot', print_r($_REQUEST,true) . file_get_contents('php://input'));

try {
    $bot = new \TelegramBot\Api\Client(APIKEY);
    print_r($bot);
    $keyboard = new \TelegramBot\Api\Types\ReplyKeyboardMarkup(
        [
            ["one","two","three"]
        ], 
        true); // true for one-time keyboard
    // or initialize with botan.io tracker api key

    //Handle /ping command
    $bot->command('ping', function ($message) use ($bot, $keyboard) {
        $bot->sendMessage($message->getChat()->getId(), 'pong!'/*, null, false, null, $keyboard*/);
    });

    // команда для start
    $bot->command('start', function ($message) use ($bot, $keyboard) {
        $answer = 'Welcome! ';
        $bot->sendMessage($message->getChat()->getId(), $answer/*, null, false, null, $keyboard*/);
    });

    // команда для помощи
    $bot->command('help', function ($message) use ($bot, $keyboard) {
        $answer = 'Команды: /help - вывод справки';

        $bot->sendMessage($message->getChat()->getId(), $answer/*, null, false, null, $keyboard*/);
    });

    //Handle text messages
    $bot->on(function (\TelegramBot\Api\Types\Update $update) use ($bot, $keyboard) {
        $message = $update->getMessage();
        $id = $message->getChat()->getId();
        $bot->sendMessage($id, 'Ваше сообщение: ' . $message->getText()/*, null, false, null, $keyboard*/);
    }, function () {
        return true;
    });

    $bot->run();

} catch (\TelegramBot\Api\Exception $e) {
    echo $e->getMessage();
    echo print_r($e->getTrace(),true);
}

and in response I get Syntax Error and trace

Array ( [0] => Array ( [file] => /siteroot/vendor/telegram-bot/api/src/Client.php [line] => 134 [function] => jsonValidate [class] => TelegramBot\Api\BotApi [type] => :: [args] => Array ( [0] => [1] => 1 )

    )

[1] => Array
    (
        [file] => /siteroot/mysqcript.php
        [line] => 56
        [function] => run
        [class] => TelegramBot\Api\Client
        [type] => ->
        [args] => Array
            (
            )

    )

)

BoShurik commented 3 years ago

What version of php do you use? Looks like php < 5.6 did not support reading from php://input multiple times. Try to remove IngineTools::logIt('bot', print_r($_REQUEST,true) . file_get_contents('php://input')); line