Commit 959a76f5 authored by Armando Lüscher's avatar Armando Lüscher Committed by GitHub

Merge pull request #387 from noplanman/debug_command

Debug command
parents 4e511e9d aecd80b3
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands\AdminCommands;
use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Request;
/**
* Admin "/debug" command
*/
class DebugCommand extends AdminCommand
{
/**
* @var string
*/
protected $name = 'debug';
/**
* @var string
*/
protected $description = 'Debug command to help find issues';
/**
* @var string
*/
protected $usage = '/debug';
/**
* @var string
*/
protected $version = '1.1.0';
/**
* Command execute method
*
* @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function execute()
{
$pdo = DB::getPdo();
$message = $this->getMessage();
$chat = $message->getChat();
$text = strtolower($message->getText(true));
$data = ['chat_id' => $chat->getId()];
if ($text !== 'glasnost' && !$chat->isPrivateChat()) {
$data['text'] = 'Only available in a private chat.';
return Request::sendMessage($data);
}
$debug_info = [];
$debug_info[] = sprintf('*TelegramBot version:* `%s`', $this->telegram->getVersion());
$debug_info[] = sprintf('*Download path:* `%s`', $this->telegram->getDownloadPath());
$debug_info[] = sprintf('*Upload path:* `%s`', $this->telegram->getUploadPath());
// Commands paths.
$debug_info[] = '*Commands paths:*';
$debug_info[] = sprintf(
'```' . PHP_EOL . '%s```',
json_encode($this->telegram->getCommandsPaths(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
);
$php_bit = '';
PHP_INT_SIZE === 4 && $php_bit = ' (32bit)';
PHP_INT_SIZE === 8 && $php_bit = ' (64bit)';
$debug_info[] = sprintf('*PHP version:* `%1$s%2$s; %3$s; %4$s`', PHP_VERSION, $php_bit, PHP_SAPI, PHP_OS);
$debug_info[] = sprintf('*Maximum PHP script execution time:* `%d seconds`', ini_get('max_execution_time'));
$mysql_version = $pdo ? $pdo->query('SELECT VERSION() AS version')->fetchColumn() : null;
$debug_info[] = sprintf('*MySQL version:* `%s`', $mysql_version ?: 'disabled');
$debug_info[] = sprintf('*Operating System:* `%s`', php_uname());
if (isset($_SERVER['SERVER_SOFTWARE'])) {
$debug_info[] = sprintf('*Web Server:* `%s`', $_SERVER['SERVER_SOFTWARE']);
}
if (function_exists('curl_init')) {
$curlversion = curl_version();
$debug_info[] = sprintf('*curl version:* `%1$s; %2$s`', $curlversion['version'], $curlversion['ssl_version']);
}
$webhook_info_title = '*Webhook Info:*';
try {
// Check if we're actually using the Webhook method.
if (Request::getInput() === '') {
$debug_info[] = $webhook_info_title . ' `Using getUpdates method, not Webhook.`';
} else {
$webhook_info_result = json_encode(json_decode(Request::getWebhookInfo(), true)['result'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$debug_info[] = $webhook_info_title;
$debug_info[] = sprintf(
'```' . PHP_EOL . '%s```',
$webhook_info_result
);
}
} catch (\Exception $e) {
$debug_info[] = $webhook_info_title . sprintf(' `Failed to get webhook info! (%s)`', $e->getMessage());
}
$data['parse_mode'] = 'Markdown';
$data['text'] = implode(PHP_EOL, $debug_info);
return Request::sendMessage($data);
}
}
...@@ -619,6 +619,16 @@ class Telegram ...@@ -619,6 +619,16 @@ class Telegram
return $this; return $this;
} }
/**
* Return the list of commands paths
*
* @return array
*/
public function getCommandsPaths()
{
return $this->commands_paths;
}
/** /**
* Set custom upload path * Set custom upload path
* *
......
...@@ -116,22 +116,30 @@ class TelegramTest extends TestCase ...@@ -116,22 +116,30 @@ class TelegramTest extends TestCase
{ {
$tg = $this->telegram; $tg = $this->telegram;
$this->assertAttributeCount(1, 'commands_paths', $tg); $this->assertCount(1, $tg->getCommandsPaths());
$tg->addCommandsPath($this->custom_commands_paths[0]); $tg->addCommandsPath($this->custom_commands_paths[0]);
$this->assertAttributeCount(2, 'commands_paths', $tg); $this->assertCount(2, $tg->getCommandsPaths());
$this->assertArraySubset(
[$this->custom_commands_paths[0]],
$tg->getCommandsPaths()
);
$tg->addCommandsPath('/invalid/path'); $tg->addCommandsPath('/invalid/path');
$this->assertAttributeCount(2, 'commands_paths', $tg); $this->assertCount(2, $tg->getCommandsPaths());
$tg->addCommandsPaths([ $tg->addCommandsPaths([
$this->custom_commands_paths[1], $this->custom_commands_paths[1],
$this->custom_commands_paths[2], $this->custom_commands_paths[2],
]); ]);
$this->assertAttributeCount(4, 'commands_paths', $tg); $this->assertCount(4, $tg->getCommandsPaths());
$this->assertArraySubset(
array_reverse($this->custom_commands_paths),
$tg->getCommandsPaths()
);
$tg->addCommandsPath($this->custom_commands_paths[0]); $tg->addCommandsPath($this->custom_commands_paths[0]);
$this->assertAttributeCount(4, 'commands_paths', $tg); $this->assertCount(4, $tg->getCommandsPaths());
} }
public function testGetCommandsList() public function testGetCommandsList()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment