-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmanager.php
97 lines (82 loc) · 2.87 KB
/
manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* This file is part of the PHP Telegram Support Bot.
*
* (c) PHP Telegram Bot Team (https://github.com/php-telegram-bot)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace TelegramBot\SupportBot;
use Dotenv\Dotenv;
use Exception;
use PhpTelegramBot\Core\Exception\TelegramLogException;
use PhpTelegramBot\Core\TelegramLog;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\NullLogger;
use TelegramBot\TelegramBotManager\BotManager;
use Throwable;
// Composer autoloader.
require_once __DIR__ . '/../vendor/autoload.php';
Dotenv::create(__DIR__ . '/..')->load();
try {
// Vitals!
$params = [
'api_key' => getenv('TG_API_KEY'),
];
foreach (['bot_username', 'secret'] as $extra) {
if ($param = getenv('TG_' . strtoupper($extra))) {
$params[$extra] = $param;
}
}
// Database connection.
if (getenv('TG_DB_HOST')) {
$params['mysql'] = [
'host' => getenv('TG_DB_HOST'),
'user' => getenv('TG_DB_USER'),
'password' => getenv('TG_DB_PASSWORD'),
'database' => getenv('TG_DB_DATABASE'),
];
}
// Optional extras.
$extras = ['admins', 'commands', 'cron', 'limiter', 'paths', 'valid_ips', 'webhook'];
foreach ($extras as $extra) {
if ($param = getenv('TG_' . strtoupper($extra))) {
$params[$extra] = json_decode($param, true);
}
}
initLogging();
$bot = new BotManager($params);
$bot->run();
} catch (TelegramLogException $e) {
// Silence... beautiful silence =)
} catch (Throwable $e) {
TelegramLog::error($e->getMessage());
}
/**
* Initialise the logging.
*
* @throws Exception
*/
function initLogging(): void
{
// Logging.
$logging_paths = json_decode(getenv('TG_LOGGING'), true) ?? [];
$debug_log = $logging_paths['debug'] ?? null;
$error_log = $logging_paths['error'] ?? null;
$update_log = $logging_paths['update'] ?? null;
// Main logger that handles all 'debug' and 'error' logs.
$logger = ($debug_log || $error_log) ? new Logger('telegram_bot') : new NullLogger();
$debug_log && $logger->pushHandler((new StreamHandler($debug_log, Logger::DEBUG))->setFormatter(new LineFormatter(null, null, true)));
$error_log && $logger->pushHandler((new StreamHandler($error_log, Logger::ERROR))->setFormatter(new LineFormatter(null, null, true)));
// Updates logger for raw updates.
$update_logger = new NullLogger();
if ($update_log) {
$update_logger = new Logger('telegram_bot_updates');
$update_logger->pushHandler((new StreamHandler($update_log, Logger::INFO))->setFormatter(new LineFormatter('%message%' . PHP_EOL)));
}
TelegramLog::initialize($logger, $update_logger);
}