178 lines
6.6 KiB
Plaintext
178 lines
6.6 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 ProcLib\Abstracts\Types\StdType;
|
|
use ProcLib\Process;
|
|
use ProcLib\Utilities\PhpExecutableFinder;
|
|
use VerboseAdventure\Abstracts\EventType;
|
|
use VerboseAdventure\Classes\ErrorHandler;
|
|
use VerboseAdventure\VerboseAdventure;
|
|
|
|
// Import all required auto loaders
|
|
/** @noinspection PhpIncludeInspection */
|
|
require('ppm');
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
ppm::import('net.intellivoid.spam_protection_bot');
|
|
|
|
VerboseAdventure::setStdout(true); // Enable stdout
|
|
ErrorHandler::registerHandlers(); // Register error handlers
|
|
|
|
// Load all configurations
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
$TelegramServiceConfiguration = SynicalBot::getTelegramConfiguration();
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
$DatabaseConfiguration = SynicalBot::getDatabaseConfiguration();
|
|
|
|
/** @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);
|
|
}
|
|
|
|
// Start the verdict thread
|
|
$phpExecutableFinder = new PhpExecutableFinder();
|
|
$phpBinLocation = $phpExecutableFinder->find();
|
|
$VerdictThread = new Process([$phpBinLocation, getcwd() . DIRECTORY_SEPARATOR . 'verdict_worker']);
|
|
$VerdictCallbackThread = function ($type, $buffer)
|
|
{
|
|
$buffer_split = implode("\n", explode("\r\n", $buffer));
|
|
$buffer_split = explode("\n", $buffer_split);
|
|
foreach ($buffer_split as $item) {
|
|
if (strlen($item) == 0)
|
|
{
|
|
continue;
|
|
}
|
|
if (stripos($item, 'flush(gearman_could_not_connect)'))
|
|
{
|
|
continue;
|
|
}
|
|
switch (strtolower($type)) {
|
|
case "out":
|
|
case StdType::STDOUT:
|
|
SynicalBot::getLogHandler()->log(EventType::INFO, $item, 'Verdict Thread');
|
|
break;
|
|
case "err":
|
|
case StdType::STDERR:
|
|
SynicalBot::getLogHandler()->log(EventType::ERROR, $item, 'Verdict Thread');
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
$VerdictThread->start($VerdictCallbackThread);
|
|
|
|
$next_event_update = time() + 60;
|
|
$total_update_count = 0;
|
|
|
|
// Start listening to updates
|
|
while(true)
|
|
{
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
SynicalBot::$BackgroundWorker->getSupervisor()->monitor(TELEGRAM_BOT_NAME);
|
|
if($VerdictThread->isRunning() == false)
|
|
{
|
|
$VerdictThread->restart($VerdictCallbackThread);
|
|
}
|
|
|
|
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');
|
|
}
|
|
}
|