Closed progS1m closed 8 years ago
What version of the library are you using? And what PHP version are you on?
Your code works for me.
PHP Version 5.6.20-0+deb8u1
And your library version? Is it the latest one from the repository, 0.33?
Exactly.
And without the logs activated, everything works perfectly, yes?
Also, do you mind posting your hook.php
file please, without the real bot name and API of course.
Yes. Also the /whois command.
What I've tried either is using this config:
$log = new \Longman\TelegramBot\TelegramLog;
$log->initialize();
$log->initErrorLog($monoLogPath . '/' . $BOT_NAME . '_error.log');
$log->initDebugLog($monoLogPath . '/' . $BOT_NAME . '_debug.log');
$log->initUpdateLog($monoLogPath . '/' . $BOT_NAME . '_update.log');
$log->update("test");
after execution I see the text "test" in the update log...
There is no need to instantiate the TelegramLog
class, your first usage was correct. You can omit the ::initialize()
, as this is only important if you are using a custom Monolog instance.
Try this at the beginning of your hook.php
file:
$BOT_NAME = 'myBotName';
\Longman\TelegramBot\TelegramLog::initUpdateLog(__DIR__ . '/' . $BOT_NAME . '_update.log');
\Longman\TelegramBot\TelegramLog::initDebugLog(__DIR__ . '/' . $BOT_NAME . '_debug.log');
\Longman\TelegramBot\TelegramLog::initErrorLog(__DIR__ . '/' . $BOT_NAME . '_error.log');
Do you get any output in *_update.log
file?
<?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 = 'myKey';
$BOT_NAME = 'myBot';
$commands_path = __DIR__ . '/commands/';
$mysql_credentials = [
'host' => 'localhost',
'user' => 'db_user',
'password' => 'myPw',
'database' => 'myDb',
];
$monoLogPath = __DIR__;
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(['a number']);
//$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
// $log = new \Longman\TelegramBot\TelegramLog;
// $log->initialize();
// $log->initErrorLog($monoLogPath . '/' . $BOT_NAME . '_error.log');
// $log->initDebugLog($monoLogPath . '/' . $BOT_NAME . '_debug.log');
// $log->initUpdateLog($monoLogPath . '/' . $BOT_NAME . '_update.log');
// $log->update("test");
// print_r($log);
\Longman\TelegramBot\TelegramLog::initialize();
\Longman\TelegramBot\TelegramLog::initErrorLog($monoLogPath . '/' . $BOT_NAME . '_error.log');
\Longman\TelegramBot\TelegramLog::initDebugLog($monoLogPath . '/' . $BOT_NAME . '_debug.log');
// Doesn't work at the moment...
\Longman\TelegramBot\TelegramLog::initUpdateLog($monoLogPath . '/' . $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;
}
Sorry I go to bed now, here it's already midnight... I will answer further questions tomorrow :-) Thanks a lot for your help!
Does not work either when I add it to the begin. Still the same. OK thx, I was unsure if I need to initialize. As soon I comment out the init UpdateLog then I get response on the telegram app calling a command...
Hmm, should be fine.
I've cleaned up the hook file a bit and added the $telegram->setCustomInput()
command at the bottom, just before handling the update.
Take an entry from your update log and enter the JSON string into this command's parameter. Then call your hook.php
file directly in your browser to get an output.
<?php
// Load composer
require __DIR__ . '/vendor/autoload.php';
$API_KEY = 'myKey';
$BOT_NAME = 'myBot';
$commands_path = __DIR__ . '/commands/';
$mysql_credentials = [
'host' => 'localhost',
'user' => 'db_user',
'password' => 'myPw',
'database' => 'myDb',
];
$monoLogPath = __DIR__;
try {
// Create Telegram API object
$telegram = new \Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
// 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(['a number']);
// Logging
\Longman\TelegramBot\TelegramLog::initUpdateLog($monoLogPath . '/' . $BOT_NAME . '_update.log');
\Longman\TelegramBot\TelegramLog::initDebugLog($monoLogPath . '/' . $BOT_NAME . '_debug.log');
\Longman\TelegramBot\TelegramLog::initErrorLog($monoLogPath . '/' . $BOT_NAME . '_error.log');
// Set custom Upload and Download path
$telegram->setDownloadPath('/Download');
$telegram->setUploadPath('/Upload');
// Set a custom input here:
$telegram->setCustomInput('JSON string in update log');
// Handle telegram webhook request
$telegram->handle();
} catch (\Longman\TelegramBot\Exception\TelegramException $e) {
\Longman\TelegramBot\TelegramLog::error($e);
echo $e;
} catch (\Longman\TelegramBot\Exception\TelegramLogException $e) {
echo $e;
}
Unfortunately my update logfile is empty. There was never data stored in. Do you have an example data set to test it?
{"update_id":1,"message":{"message_id":1,"from":{"id":1,"first_name":"First","username":"user"},"chat":{"id":1,"first_name":"First","username":"user","type":"private"},"date":1234567890,"text":"/whoami"}}
Output should be empty, but I'm guessing you'll get an error of some sort.
Interesting :+1:
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/var/www/mydomain.ch/mrx/php-telegram-bot/cnnMrXbot_update.log" could not be opened: failed to open stream: Permission denied' in /var/www/mydomain.ch/mrx/php-telegram-bot/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:97 Stack trace: #0 /var/www/mydomain.ch/mrx/php-telegram-bot/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\StreamHandler->write(Array) #1 /var/www/mydomain.ch/mrx/php-telegram-bot/vendor/monolog/monolog/src/Monolog/Logger.php(336): Monolog\Handler\AbstractProcessingHandler->handle(Array) #2 /var/www/mydomain.ch/mrx/php-telegram-bot/vendor/monolog/monolog/src/Monolog/Logger.php(545): Monolog\Logger->addRecord(200, '{"update_id":1,...', Array) #3 /var/www/mydomain.ch/mrx/php-telegram-bot/src/TelegramLog.php(217): Monolog\Logger->info('{"update_id":1,...') #4 /var/www/mydomain.ch/mrx/php-telegram-bot/src/Request.php(98): Longman\TelegramBot\TelegramLo in /var/www/mydomain.ch/mrx/php-telegram-bot/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 97
Its definitively a permission problem, because the web server is running under a different user it can't write to the file. Because i've ran once the hook.php via CLI it was able to generate and write to the files and I though it could not be an permission problem...
I will fix that tomorrow and let you know. Thanks a lot! Now I either do know how to debug with the setCustominput function :-) Thanks for your very fast help, you are the best! Grüsse von der Schweiz in die Schweiz ;-)
You're so welcome, had a strong feeling it was permissions problems 😉 I'll leave this issue open till it's 100% fixed.
Grüsse zurück an dich!!
I've modified the folder permissions now everything works like a charm. All logs are getting filled with data. You can close the issue.
Actually I hoped I could use the log to find out why I can't see files in the upload folder. (This is a second problem I do have.) It has nothing to do with this issue. I will open a new issue for that.
Ok :+1:
I've setup php-telegram-bot according to the documentation. In general everything works fine. Inclusive webhook, mysql db, etc.
But when I try to use TelegramLog, some features doesn't work properly any more.
If this is activated in hook.php:
Per example the command /whoami doesn't work any more. I don't get a reply any more. When I comment these lines, every thing works fine again.
With this line is uncommented:
I don't get a reply for all commands any more. Per example /help,/echo, etc.
How can I analyse what happens wrong in my setup? I saw that the logfiles were created successful on my server. But they are resting empty. Because I used at the beginning this configuration:
There are some older log entries in the _error.log:
This shows me that php is able to create and write into files. And monolog is functional on the server.
I would appreciate your help very much! Please let me know if you need some more info...