php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.9k stars 953 forks source link

Can't get bot to respond to any commands (Webhook). #278

Closed jamesdcampbell closed 8 years ago

jamesdcampbell commented 8 years ago

I have been at it for a few hours searching through posts and I have yet to find a solution as to why my bot is not replying back.

--I have a valid https:// on my domain and the ssl shows a rating of A- (not self-generated) --Webhook does work and will throw an error at api.telegram.com/bot if you try to "getUpdates" --hook.php is not generating any errors (blank white screen) and no log files are being created (even when I had the option uncommitted). If the log options are uncommitted, the server throws an error 500. There is no "Input is empty" error either when accessed directly via the browser. --Permissions are wide open right now for testing and apache is the owner of every file.

--PHP 5.6.24 (cli) --centos-release-6-7.el6.centos.12.3.x86_64

Here are my files:

set.php

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

$API_KEY  = 'MYAPIKEY';
$BOT_NAME = 'MYBOTNAME';
$hook_url = 'https://MYDOMAIN.com/telegrambots/VB/hook.php';

try {
    // Create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);

    // Set webhook
    $result = $telegram->setWebHook($hook_url);
    if ($result->isOk()) {
        echo $result->getDescription();
    }
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    echo $e;
}

hook.php

<?php

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

$API_KEY = 'MYAPIKEY';
$BOT_NAME = 'MYBOTNAME';
$commands_path = __DIR__ . '/Commands/';

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 gold!
    // echo $e;
    // log telegram errors
    \Longman\TelegramBot\TelegramLog::error($e);
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
    // Silence is gold! Uncomment this to catch log initilization errors
    echo $e;
}

After all these hours I know I must be looking right at the error and not noticing.

noplanman commented 8 years ago

Hi @jamesdcampbell

The logging isn't active in your hook.php file. I assume you removed it to post here but have it enabled, yes?

getUpdates won't work without a database, so you would need a MySQL instance for that!

Also, "Input is empty!" message isn't output, because the echo $e; is commented in the catch (TelegramException $e) block.

What version are you using?

May I suggest you check out the readme again to make sure that all steps have been done correctly.

jamesdcampbell commented 8 years ago

My apologies, I had posted this at the end of the night and the code reflected was after some testing. I looked over the readme once more and followed it precisely in order to get a better environment. However, I am still getting the same issue as last night.

My version is 0.35. Here are the steps I completed this morning:

-Grabbed a copy of version 0.35 and extracted it into a new directory

-Changed the composer.json file and ran an update (which ran without issue and fetched the vendor folder)

-Grabbed a copy of set.php, hook.php, and unset.php from the examples directory.

-Changed the $API_KEY and $BOT_NAME for each file and changed the $hook_url for set.php as well.

-Ran set.php via browser and was given the message "Webhook was set"

-Navigated to "https://api.telegram.org/botBOTNAME:BOTTOKEN/getUpdates" which returned "{"ok":false,"error_code":409,"description":"Conflict: can't use getUpdates method while webhook is active"}" which means that the webhook is active and was set. (Not actually trying to use getUpdates, but this is a good method of seeing if telegram sees the hook)

-At this point, bot still will not respond to commands via client

-I checked hook.php and allowed for $e to print out the errors via browser (which this time it did print out the "Input is empty!" message via browser).

-I opened up the entire directory as 777 and set the ownership to apache.

-I uncommented out logging in hook.php which lead to a HTTP Error 500.

-I tested each line one by one and it turns out that the line "\Longman\TelegramBot\TelegramLog::initUpdateLog($path . '/' . $BOT_NAME . '_update.log');" is the one causing the http error.

-Even with the previous line commented, none of the logging is taking place as there are no files being generated for any of the lines, the update line seems to be the only one causing the http error though.

Here is my code:

Hook.php

<?php
//README
//This configuration file is intended to run the bot with the webhook method.
//Uncommented parameters must be filled
//Please notice 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 Telegram server.

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

$API_KEY = 'MYAPIKEY';
$BOT_NAME = 'MYBOTNAME';
$commands_path = __DIR__ . '/Commands/';
//$mysql_credentials = [
//    'host'     => 'localhost',
//    'user'     => 'dbuser',
//    'password' => 'dbpass',
//    'database' => 'dbname',
//];

try {
    // Create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);

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

    //// Enable MySQL with table prefix
    //$telegram->enableMySQL($mysql_credentials, $BOT_NAME . '_');

    //// Add an additional commands path
    //$telegram->addCommandsPath($commands_path);

    //// Here you can enable admin interface for the channel you want to manage
    //$telegram->enableAdmins(['your_telegram_id']);
    //$telegram->setCommandConfig('sendtochannel', ['your_channel' => '@type_here_your_channel']);

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

    //// Logging
    \Longman\TelegramBot\TelegramLog::initialize($your_external_monolog_instance);
    \Longman\TelegramBot\TelegramLog::initErrorLog($path . '/' . $BOT_NAME . '_error.log');
    \Longman\TelegramBot\TelegramLog::initDebugLog($path . '/' . $BOT_NAME . '_debug.log');
    \Longman\TelegramBot\TelegramLog::initUpdateLog($path . '/' . $BOT_NAME . '_update.log');

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

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

    // Handle telegram webhook request
    $telegram->handle();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    // Silence is gold!
     echo $e;
    // log telegram errors
    \Longman\TelegramBot\TelegramLog::error($e);
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
    // Silence is gold! Uncomment this to catch log initilization errors
    echo $e;
}

Set.php

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

$API_KEY = 'MYBOTAPIKEY';
$BOT_NAME = 'MYBOTNAME';
$hook_url = 'https://MYDOMAIN.com/VB/hook.php';
try {
    // Create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);

    // Set webhook
    $result = $telegram->setWebHook($hook_url);

    // Uncomment to use certificate
    //$result = $telegram->setWebHook($hook_url, $path_certificate);

    if ($result->isOk()) {
        echo $result->getDescription();
    }
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    echo $e;

Composer.json

{
    "name": "myproject/VB",
    "type": "project",
    "require": {
        "php": ">=5.5.0",
        "longman/telegram-bot": "*"
    }
}
noplanman commented 8 years ago

Ok, it's a little mistake here.

When you're setting the loggers, the $path variable isn't set, so the path is interpreted as '/' . $BOT_NAME . '_error.log', which is obviously a location that the Apache has no access to (I SURE HOPE SO! 😉 ). Also, you have no external monolog instance, so the first line should be commented.

Try this:

// Logging
\Longman\TelegramBot\TelegramLog::initErrorLog(__DIR__ . '/' . $BOT_NAME . '_error.log');
\Longman\TelegramBot\TelegramLog::initDebugLog(__DIR__ . '/' . $BOT_NAME . '_debug.log');
\Longman\TelegramBot\TelegramLog::initUpdateLog(__DIR__ . '/' . $BOT_NAME . '_update.log');

(or alternatively set the $path variable to a valid location)

jamesdcampbell commented 8 years ago

Viola! Now I am starting to get some errors:

Notice: Undefined variable: path in /var/www/html/telegrambots/VB/hook.php on line 43 Notice: Undefined variable: path in /var/www/html/telegrambots/VB/hook.php on line 44 Notice: Undefined variable: path in /var/www/html/telegrambots/VB/hook.php on line 45

are all new ones, and then I am getting a permissions problem that starts with:

Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/mybot_update.log" could not be opened: failed to open stream: Permission denied' in /var/www/html/telegrambots/VB/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:107

which is odd because all files have open permissions and apache owns the entire directory. This issue was referenced and solved in https://github.com/akalongman/php-telegram-bot/issues/217 at the end of the discussion, but the user never posted the specific permissions needed to get it to work.

noplanman commented 8 years ago

Hi @jamesdcampbell

Read the error message 😉

Undefined variable: path

Like I wrote in my previous comment, the $path variable you are using isn't set, so it tries to write the logfile to the root of you system! (The stream or file "/mybot_update.log" could not be opened: failed to open stream: Permission denied)

If you check my example, I use __DIR__ instead of $path. __DIR__ is a shortcut to the current folder, a place where Apache has permissions.

Hope this clears things up 👍

jamesdcampbell commented 8 years ago

I am so sorry, I totally misread that, I just removed the one line without looking to see your changes. The bot is now seeing the commands in the _update.log so I think everything is good :) thank you so much for your prompt help and for putting up with the noobie mistakes!

noplanman commented 8 years ago

You're so welcome, great to hear that it's working now.

More often than not it's the little things 😊

If you have any other problems, don't hesitate to open a new issue.