Core: Wrong response from the webhook: 500 Internal Server Error

Created on 28 Aug 2017  路  10Comments  路  Source: php-telegram-bot/core

Required Information

  • Operating system: Ubuntu 14.04
  • PHP version: 5.5.9
  • PHP Telegram Bot version: 0.48.0
  • Using MySQL database: no
  • Update Method: Webhook
  • Self-signed certificate: no

Expected behaviour

WebHook should be set, and bot should receive and response to commands.

Actual behaviour

When open set.php in browser it shows Webhook was set but if check https://api.telegram.org/bot12345678:ytDWc-snTFNkdGRyGE/getWebhookInfo, it shows the next response:

{
  "ok": true,
  "result": {
    "url": "https://example.com/hook.php",
    "has_custom_certificate": false,
    "pending_update_count": 50,
    "last_error_date": 1503949200,
    "last_error_message": "Wrong response from the webhook: 500 Internal Server Error",
    "max_connections": 40
  }
}

Steps to reproduce

  • Copy php-telegram-bot/example-bot to a folder on a server.
  • Give the folder www-data permissions by sudo chown -R www-data:www-data ~/www && sudo chmod -R g+w ~/www
  • Install composer.
  • Change set.php,hook.php, composer.json with my own data like token to access HTTP API, botUsername and WebHook URL.
  • Run composer install from the example-bot folder.
  • Open https://mydomain.com/path/to/bot/set.php in browser to get Webhook was set response.
  • Check https://api.telegram.org/bot12345678:BOT_API_TOKEN/getWebhookInfo

Extra details

Server uses not Self-signed certificate. Certificate was issued by Comodo.
Here is the code of set.php:

<?php
/**
 * README
 * This file is intended to set the webhook.
 * Uncommented parameters must be filled
 */
// Load composer
require_once __DIR__ . '/vendor/autoload.php';
// Add you bot's API key and name
$bot_api_key  = '12345678:AAN_yug_FGLKge-fayecJFC6fgo8YDIuh';
$bot_username = 'UsernameBot';
// Define the URL to your hook.php file
$hook_url     = 'https://www.example.com/path/to/hook.php';
try {
    // Create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
    // Set webhook
    $result = $telegram->setWebhook($hook_url);
    // To use a self-signed certificate, use this line instead
    //$result = $telegram->setWebhook($hook_url, ['certificate' => $certificate_path]);
    if ($result->isOk()) {
        echo $result->getDescription();
    }
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    echo $e->getMessage();
}

hook.php code:

<?php
/**
 * README
 * This configuration file is intended to run the bot with the webhook method.
 * Uncommented parameters must be filled
 *
 * Please note that if you open this file with your browser you'll get the "Input is empty!" Exception.
 * This is a normal behaviour because this address has to be reached only by the Telegram servers.
 */

// Load composer
require_once __DIR__ . '/vendor/autoload.php';

// Add you bot's API key and name
$bot_api_key  = '12345678:AAN_yug_FGLKge-fayecJFC6fgo8YDIuh';
$bot_username = 'UsernameBot';

// Define all IDs of admin users in this array (leave as empty array if not used)
$admin_users = [
    123,
];

// Define all paths for your custom commands in this array (leave as empty array if not used)
$commands_paths = [
    __DIR__ . '/Commands/',
];

// Enter your MySQL database credentials
//$mysql_credentials = [
//    'host'     => 'localhost',
//    'user'     => 'dbuser',
//    'password' => 'dbpass',
//    'database' => 'dbname',
//];

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);

    // Enable MySQL
    //$telegram->enableMySql($mysql_credentials);

    // Logging (Error, Debug and Raw Updates)
    Longman\TelegramBot\TelegramLog::initErrorLog(__DIR__ . "/{$bot_username}_error.log");
    Longman\TelegramBot\TelegramLog::initDebugLog(__DIR__ . "/{$bot_username}_debug.log");
    //Longman\TelegramBot\TelegramLog::initUpdateLog(__DIR__ . "/{$bot_username}_update.log");

    // If you are using a custom Monolog instance for logging, use this instead of the above
    //Longman\TelegramBot\TelegramLog::initialize($your_external_monolog_instance);

    // Set custom Upload and Download paths
    //$telegram->setDownloadPath(__DIR__ . '/Download');
    //$telegram->setUploadPath(__DIR__ . '/Upload');

    // Here you can set some command specific parameters
    // e.g. Google geocode/timezone api key for /date command
    //$telegram->setCommandConfig('date', ['google_api_key' => 'your_google_api_key_here']);

    // Botan.io integration
    //$telegram->enableBotan('your_botan_token');

    // Requests Limiter (tries to prevent reaching Telegram API limits)
    $telegram->enableLimiter();

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


} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    // Silence is golden!
    //echo $e;
    // Log telegram errors
    Longman\TelegramBot\TelegramLog::error($e);
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
    // Silence is golden!
    // Uncomment this to catch log initialisation errors
    //echo $e;
}

composer.json

{
    "name": "UsernameBot/UsernameBot",
    "type": "project",
    "require": {
        "php": ">=5.5",
        "longman/telegram-bot": "^0.48.0"
    }
}

Most helpful comment

Input is empty! is correct when using the webhook :+1:

Add yourself as an admin and execute /debug command. Does that work?

Have you made any custom changes to the bot, or is it "out-of-the-box"?

Also, make sure the permissions of your files are correct.

(P.S. I have removed the URL to your hook.php in your first comment, please always keep it secret! Otherwise it's possible to impersonate Telegram and manipulate your bot 馃槹)

All 10 comments

It's getWebhookInfo.

Thanx, sorry, was my fault. But now it shows the SSL error:

337047686, error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
I have read that I need some full chained certificate. Is this may cause the issue?

SSL error on Telegram API or SSL error response from Telegram API? Telegram server must be able to verify the certificate as trusted.

SSL error when I check it by https://api.telegram.org/bot12345678:ytDWc-snTFNkdGRyGE/getWebhookInfo. I updated the issue.

Then it's an issue with the certificate itself, I'm not a specialist here so I don't think I will be much of help...

I had to install a full chained certificate to my server. Just added SSLCertificateChainFile /path/to/cert.ca-bundle to the /etc/apache2/sites-available/example.com.conf file.

You can check if you need a full chained cert. by opening this link:
https://www.ssllabs.com/ssltest/analyze.html?d=www.example.com&hideResults=on

change example.com with your code. If you get Chain issues: incomplete then you need to add full chained cert.

But now I'm getting a new error: "last_error_message":"Wrong response from the webhook: 500 Internal Server Error" when checking getWebhookInfo. When I open hook.php in browser it writes Input is empty! in the log file.

Input is empty! is correct when using the webhook :+1:

Add yourself as an admin and execute /debug command. Does that work?

Have you made any custom changes to the bot, or is it "out-of-the-box"?

Also, make sure the permissions of your files are correct.

(P.S. I have removed the URL to your hook.php in your first comment, please always keep it secret! Otherwise it's possible to impersonate Telegram and manipulate your bot 馃槹)

Thanx :) I thought I replaced all the links.
Yeah, I have seen it in "issues" section that Input is empty! is a correct answer.
I can't add myself into admins because the bot doesn't respond to any, even "/test" commands copied into "Commands" folder from the wiki.
I haven't done any changes to the code at all, excluding API ID, bot name and hook url.
Permissions are 0664 to all the files. Ownership has been set by:
sudo chown -R www-data:www-data ~/www
sudo chmod -R g+w ~/www


Update.
I have found the problem.
I tried to change the code in "Commands" section in the file "StartCommand.php". I changed it like in "wiki" to make it namespace Longman\TelegramBot\Commands\UserCommands; instead of SystemCommands. Sorry for bothering. Now everything works as it suposed to.

It's no bother 馃憣 Main thing is that it works now!

I got "Wrong response from the webhook: 500 Internal Server Error"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mohsenshahab picture mohsenshahab  路  4Comments

esomkin picture esomkin  路  3Comments

Recouse picture Recouse  路  3Comments

smaznet picture smaznet  路  4Comments

irmmr picture irmmr  路  3Comments