2019-01-07 13:24:29 +01:00
|
|
|
<?php
|
|
|
|
|
2020-01-19 04:17:14 +01:00
|
|
|
use TelegramApiServer\Migrations\SessionsMigration;
|
|
|
|
use TelegramApiServer\Migrations\SwooleToAmpMigration;
|
2020-01-14 22:39:41 +01:00
|
|
|
|
2020-01-11 21:53:58 +01:00
|
|
|
chdir(__DIR__);
|
|
|
|
|
2019-01-07 13:24:29 +01:00
|
|
|
require_once __DIR__ . '/bootstrap.php';
|
|
|
|
|
|
|
|
if (PHP_SAPI !== 'cli') {
|
2020-01-14 22:39:41 +01:00
|
|
|
throw new RuntimeException('Start in CLI');
|
2019-01-07 13:24:29 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 00:28:30 +01:00
|
|
|
$shortopts = 'a::p::s::';
|
2019-03-19 00:36:20 +01:00
|
|
|
$longopts = [
|
2019-01-07 13:24:29 +01:00
|
|
|
'address::', // ip адресс сервера, необязательное значение
|
|
|
|
'port::', // порт сервера, необязательное значение
|
2019-03-09 00:28:30 +01:00
|
|
|
'session::', //префикс session файла
|
2019-01-07 13:24:29 +01:00
|
|
|
'help', //нужна ли справка?
|
|
|
|
];
|
|
|
|
$options = getopt($shortopts, $longopts);
|
|
|
|
$options = [
|
2019-03-19 00:36:20 +01:00
|
|
|
'address' => $options['address'] ?? $options['a'] ?? '',
|
|
|
|
'port' => $options['port'] ?? $options['p'] ?? '',
|
2020-01-19 04:17:14 +01:00
|
|
|
'session' => (array) ($options['session'] ?? $options['s'] ?? []),
|
2019-03-19 00:36:20 +01:00
|
|
|
'help' => isset($options['help']),
|
2019-01-07 13:24:29 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($options['help']) {
|
|
|
|
$help = 'Fast, simple, async php telegram parser: MadelineProto + Swoole Server
|
|
|
|
|
2020-01-19 04:17:14 +01:00
|
|
|
usage: php server.php [--help] [-a=|--address=127.0.0.1] [-p=|--port=9503] [-s=|--session=]
|
2019-01-07 13:24:29 +01:00
|
|
|
|
|
|
|
Options:
|
|
|
|
--help Show this message
|
2020-01-15 00:00:58 +01:00
|
|
|
|
|
|
|
-a --address Server ip (optional) (default: 127.0.0.1)
|
|
|
|
To listen external connections use 0.0.0.0 and fill IP_WHITELIST in .env
|
|
|
|
|
|
|
|
-p --port Server port (optional) (default: 9503)
|
|
|
|
|
2020-01-19 04:17:14 +01:00
|
|
|
-s --session Name for session file (optional)
|
2020-01-15 00:00:58 +01:00
|
|
|
Multiple sessions can be specified: "--session=user --session=bot"
|
|
|
|
|
2020-01-14 23:44:03 +01:00
|
|
|
Each session is stored in `sessions/{$session}.madeline`.
|
|
|
|
Nested folders supported.
|
|
|
|
See README for more examples.
|
2019-01-07 13:24:29 +01:00
|
|
|
|
|
|
|
Also all options can be set in .env file (see .env.example)
|
|
|
|
|
|
|
|
Example:
|
|
|
|
php server.php
|
|
|
|
|
|
|
|
';
|
|
|
|
echo $help;
|
|
|
|
exit;
|
|
|
|
}
|
2020-01-11 21:53:58 +01:00
|
|
|
|
2020-01-19 04:17:14 +01:00
|
|
|
SessionsMigration::move(__DIR__);
|
|
|
|
SwooleToAmpMigration::check();
|
2020-01-14 22:39:41 +01:00
|
|
|
|
2020-01-11 21:53:58 +01:00
|
|
|
$sessionFiles = [];
|
|
|
|
foreach ($options['session'] as $session) {
|
2020-01-14 18:55:31 +01:00
|
|
|
$session = trim($session);
|
|
|
|
if (mb_substr($session, -1) === '/') {
|
|
|
|
throw new InvalidArgumentException('Session name specified as directory');
|
|
|
|
}
|
|
|
|
|
2020-01-12 20:25:12 +01:00
|
|
|
$session = TelegramApiServer\Client::getSessionFile($session);
|
2020-01-20 02:04:39 +01:00
|
|
|
|
|
|
|
if (preg_match('~['.preg_quote('*?[]!', '~').']~',$session)) {
|
|
|
|
$sessions = glob($session);
|
|
|
|
} else {
|
|
|
|
$sessions[] = $session;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sessions = array_filter($sessions);
|
|
|
|
foreach ($sessions as $file) {
|
2020-01-18 15:25:53 +01:00
|
|
|
TelegramApiServer\Client::checkOrCreateSessionFolder($file, __DIR__);
|
|
|
|
$sessionFiles[$file] = null;
|
|
|
|
}
|
2019-03-09 00:28:30 +01:00
|
|
|
}
|
2019-01-07 13:24:29 +01:00
|
|
|
|
2020-01-20 02:38:14 +01:00
|
|
|
new TelegramApiServer\Server\Server(
|
|
|
|
new TelegramApiServer\Client(),
|
|
|
|
$options,
|
|
|
|
array_keys($sessionFiles)
|
|
|
|
);
|