SynicalBot/botsrc/main

136 lines
5.0 KiB
Plaintext

<?php
/** @noinspection PhpDefineCanBeReplacedWithConstInspection */
/** @noinspection DuplicatedCode */
/**
* main is the main execution point for the bot to start polling, this method uses BackgroundWorker to
* instantly process a batch of updates in the background without waiting for the updates to be completed.
*
* In exchange for this performance upgrade, each worker will use up database connections, make sure
* the database can handle these connections without maxing out
*/
use BackgroundWorker\BackgroundWorker;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
use ppm\ppm;
use VerboseAdventure\Abstracts\EventType;
use VerboseAdventure\Classes\ErrorHandler;
use VerboseAdventure\VerboseAdventure;
// Import all required auto loaders
require('ppm');
/** @noinspection PhpUnhandledExceptionInspection */
ppm::import('net.intellivoid.synical_bot');
VerboseAdventure::setStdout(true); // Enable stdout
ErrorHandler::registerHandlers(); // Register error handlers
// Load all configurations
/** @noinspection PhpUnhandledExceptionInspection */
$TelegramServiceConfiguration = SynicalBot::getTelegramConfiguration();
/** @noinspection PhpUnhandledExceptionInspection */
$BackgroundWorkerConfiguration = SynicalBot::getBackgroundWorkerConfiguration();
// Create the Telegram Bot instance (NO SQL)
define('TELEGRAM_BOT_NAME', $TelegramServiceConfiguration['BotName']);
SynicalBot::setLogHandler(new VerboseAdventure(TELEGRAM_BOT_NAME));
SynicalBot::getLogHandler()->log(EventType::INFO, 'Starting Service', 'Main');
try
{
if($TelegramServiceConfiguration['EnableCustomServer'])
{
Request::setCustomBotApiUri(
$TelegramServiceConfiguration['CustomEndpoint'],
$TelegramServiceConfiguration['CustomDownloadEndpoint']
);
define('TELEGRAM_ENDPOINT', $TelegramServiceConfiguration['CustomEndpoint']);
define('TELEGRAM_DOWNLOAD_ENDPOINT',
str_ireplace('{API_KEY}', $TelegramServiceConfiguration['BotToken'], $TelegramServiceConfiguration['CustomDownloadEndpoint']));
}
else
{
define('TELEGRAM_ENDPOINT', 'https://api.telegram.org');
define('TELEGRAM_DOWNLOAD_ENDPOINT', '/file/bot' . $TelegramServiceConfiguration['BotToken']);
}
$telegram = new Longman\TelegramBot\Telegram(
$TelegramServiceConfiguration['BotToken'],
$TelegramServiceConfiguration['BotName'],
$TelegramServiceConfiguration['UseTestServers']
);
$telegram->setVerboseLogging($TelegramServiceConfiguration['VerboseLogging']);
}
catch (Longman\TelegramBot\Exception\TelegramException $e)
{
SynicalBot::getLogHandler()->logException($e, 'Main');
exit(255);
}
$telegram->useGetUpdatesWithoutDatabase();
// Start the workers using the supervisor
SynicalBot::getLogHandler()->log(EventType::INFO, 'Starting Supervisor', 'Main');
try
{
SynicalBot::$BackgroundWorker = new BackgroundWorker();
SynicalBot::$BackgroundWorker->getSupervisor()->setDisplayOutput(TELEGRAM_BOT_NAME, true);
SynicalBot::getBackgroundWorker()->getClient()->addServer($BackgroundWorkerConfiguration['Host'], (int)$BackgroundWorkerConfiguration['Port']);
SynicalBot::getBackgroundWorker()->getSupervisor()->startWorkers(
getcwd() . DIRECTORY_SEPARATOR . 'worker', TELEGRAM_BOT_NAME,
(int)$BackgroundWorkerConfiguration['MaxWorkers']
);
}
catch(Exception $e)
{
SynicalBot::getLogHandler()->logException($e, 'Main');
exit(255);
}
$next_event_update = time() + 60;
$total_update_count = 0;
// Start listening to updates
while(true)
{
/** @noinspection PhpUnhandledExceptionInspection */
SynicalBot::$BackgroundWorker->getSupervisor()->monitor(TELEGRAM_BOT_NAME);
try
{
$server_response = $telegram->handleBackgroundUpdates(SynicalBot::getBackgroundWorker());
if ($server_response->isOk())
{
$update_count = count($server_response->getResult());
if($update_count > 0)
{
$total_update_count += $update_count;
if(time() >= $next_event_update)
{
SynicalBot::getLogHandler()->log(EventType::INFO, 'Processed ' . $total_update_count . ' update(s)', 'Main');
$total_update_count = 0;
$next_event_update = time() + 60;
}
}
}
else
{
SynicalBot::getLogHandler()->log(EventType::ERROR, 'Failed to fetch updates: ' . $server_response->printError(true), 'Main');
}
}
catch (TelegramException $e)
{
SynicalBot::getLogHandler()->logException($e, 'Main');
}
}