SynicalBot/botsrc/worker

242 lines
9.0 KiB
Plaintext

<?php
/** @noinspection PhpDefineCanBeReplacedWithConstInspection */
/** @noinspection PhpUndefinedClassInspection */
/** @noinspection DuplicatedCode */
/**
* worker is the code that the worker will execute whenever a job passed on from the main
* bot. Starting the CLI will restart the workers that are already running in the background
*/
use BackgroundWorker\BackgroundWorker;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\TelegramLog;
use Longman\TelegramBot\Request;
use ppm\ppm;
use Synical\Synical;
use TelegramClientManager\TelegramClientManager;
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');
$current_directory = getcwd();
VerboseAdventure::setStdout(true); // Enable stdout
VerboseAdventure::setSimpleStdout(true); // Simplified output
ErrorHandler::registerHandlers(); // Register error handlers
if(class_exists('SynicalBot') == false)
{
if(file_exists($current_directory . DIRECTORY_SEPARATOR . 'SynicalBot.php'))
{
require_once($current_directory . DIRECTORY_SEPARATOR . 'SynicalBot.php');
}
elseif(file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'SynicalBot.php'))
{
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'SynicalBot.php');
}
else
{
throw new RuntimeException('Cannot locate bot class');
}
}
// Load all required configurations
/** @noinspection PhpUnhandledExceptionInspection */
$TelegramServiceConfiguration = SynicalBot::getTelegramConfiguration();
/** @noinspection PhpUnhandledExceptionInspection */
$DatabaseConfiguration = SynicalBot::getDatabaseConfiguration();
/** @noinspection PhpUnhandledExceptionInspection */
$BackgroundWorkerConfiguration = SynicalBot::getBackgroundWorkerConfiguration();
/** @noinspection PhpUnhandledExceptionInspection */
$RedisConfiguration = SynicalBot::getRedisConfiguration();
// Define and create the Telegram Bot instance (SQL)
define('TELEGRAM_BOT_NAME', $TelegramServiceConfiguration['BotName']);
define('LOG_CHANNEL', $TelegramServiceConfiguration['LoggingChannel']);
define('MAIN_OPERATOR_IDS', $TelegramServiceConfiguration['MainOperators']);
SynicalBot::setLogHandler(new VerboseAdventure(TELEGRAM_BOT_NAME));
SynicalBot::setLastWorkerActivity(time());
SynicalBot::setIsSleeping(false);
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']);
if(file_exists($current_directory . DIRECTORY_SEPARATOR . 'SynicalBot.php'))
{
$telegram->addCommandsPaths([$current_directory . DIRECTORY_SEPARATOR . 'commands']);
$telegram->addCommandsPaths([$current_directory . DIRECTORY_SEPARATOR . 'AdminCommands']);
}
elseif(file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'SynicalBot.php'))
{
$telegram->addCommandsPaths([__DIR__ . DIRECTORY_SEPARATOR . 'commands']);
$telegram->addCommandsPaths([__DIR__ . DIRECTORY_SEPARATOR . 'AdminCommands']);
}
else
{
print('Cannot locate commands path');
exit(1);
}
TelegramLog::initialize();
}
catch (Longman\TelegramBot\Exception\TelegramException $e)
{
SynicalBot::getLogHandler()->logException($e, 'Worker');
exit(255);
}
try
{
$telegram->enableMySql(array(
'host' => $DatabaseConfiguration['Host'],
'port' => $DatabaseConfiguration['Port'],
'user' => $DatabaseConfiguration['Username'],
'password' => $DatabaseConfiguration['Password'],
'database' => $DatabaseConfiguration['Database'],
));
$telegram->enableRedis(
$RedisConfiguration['Host'],
(int)$RedisConfiguration['Port'],
(int)$RedisConfiguration['Database'],
empty($RedisConfiguration['Username']) ? null : $RedisConfiguration['Username'],
empty($RedisConfiguration['Password']) ? null : $RedisConfiguration['Password']
);
}
catch(Exception $e)
{
SynicalBot::getLogHandler()->logException($e, 'Worker');
exit(255);
}
// Create the database connections
SynicalBot::$TelegramClientManager = new TelegramClientManager();
if(SynicalBot::$TelegramClientManager->getDatabase()->connect_error)
{
SynicalBot::getLogHandler()->log(EventType::ERROR, 'Failed to initialize TelegramClientManager, ' . SynicalBot::$TelegramClientManager->getDatabase()->connect_error, 'Worker');
exit(255);
}
SynicalBot::$SynicalEngine = new Synical();
if(SynicalBot::$SynicalEngine->getDatabase()->connect_error)
{
SynicalBot::getLogHandler()->log(EventType::ERROR, 'Failed to initialize SynicalEngine, ' . SynicalBot::$SynicalEngine->getDatabase()->connect_error, 'Worker');
exit(255);
}
try
{
$BackgroundWorker = new BackgroundWorker();
$BackgroundWorker->getWorker()->addServer(
$BackgroundWorkerConfiguration['Host'],
(int)$BackgroundWorkerConfiguration['Port']
);
}
catch(Exception $e)
{
SynicalBot::getLogHandler()->logException($e, 'Worker');
exit(255);
}
// Define the function 'process_batch' to process a batch of Updates from Telegram in the background
$BackgroundWorker->getWorker()->addFunction($telegram->getBotUsername() . '_updates', function(GearmanJob $job) use ($telegram)
{
try
{
/** @noinspection PhpCastIsUnnecessaryInspection */
SynicalBot::setLastWorkerActivity((int)time()); // Set the last activity timestamp
SynicalBot::processSleepCycle(); // Wake worker if it's sleeping
$ServerResponse = new ServerResponse(json_decode($job->workload(), true), TELEGRAM_BOT_NAME);
if(is_null($ServerResponse->getResult()) == false)
{
$UpdateCount = count($ServerResponse->getResult());
if($UpdateCount > 0)
{
SynicalBot::getLogHandler()->log(EventType::INFO, 'Processing ' . $UpdateCount . ' update(s)', 'Worker');
/** @var Update $result */
foreach ($ServerResponse->getResult() as $result)
{
try
{
if(TelegramLog::isVerboseLogging())
SynicalBot::getLogHandler()->log(EventType::INFO, 'Processing update ID ' . $result->getUpdateId(), 'Worker');
$telegram->processUpdate($result);
}
catch(Exception $e)
{
SynicalBot::getLogHandler()->logException($e, 'Worker');
}
}
}
}
}
catch(Exception $e)
{
SynicalBot::getLogHandler()->logException($e, 'Worker');
}
});
// Set the timeout to 5 seconds
$BackgroundWorker->getWorker()->getGearmanWorker()->setTimeout(500);
while(true)
{
try
{
$BackgroundWorker->getWorker()->work(false);
if($BackgroundWorker->getWorker()->getGearmanWorker()->returnCode() == GEARMAN_TIMEOUT)
{
SynicalBot::processSleepCycle();
}
}
catch(Exception $e)
{
SynicalBot::getLogHandler()->logException($e, 'Worker');
exit(255);
}
}