From 8a11ca752408fda9962583d6b9e44c2d1233d293 Mon Sep 17 00:00:00 2001 From: Alexander Pankratov Date: Mon, 20 Jan 2020 04:04:39 +0300 Subject: [PATCH] Sessions fixes --- server.php | 10 +++++- src/Client.php | 33 ++++++++++++------- .../SystemApiExtensions.php | 2 +- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/server.php b/server.php index b1e2042..f7da320 100644 --- a/server.php +++ b/server.php @@ -67,7 +67,15 @@ foreach ($options['session'] as $session) { } $session = TelegramApiServer\Client::getSessionFile($session); - foreach (glob($session) as $file) { + + if (preg_match('~['.preg_quote('*?[]!', '~').']~',$session)) { + $sessions = glob($session); + } else { + $sessions[] = $session; + } + + $sessions = array_filter($sessions); + foreach ($sessions as $file) { TelegramApiServer\Client::checkOrCreateSessionFolder($file, __DIR__); $sessionFiles[$file] = null; } diff --git a/src/Client.php b/src/Client.php index ff37318..e25b3e9 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,6 +4,9 @@ namespace TelegramApiServer; use Amp\Loop; use danog\MadelineProto; +use danog\MadelineProto\MTProto; +use InvalidArgumentException; +use RuntimeException; use TelegramApiServer\EventObservers\EventHandler; class Client @@ -61,12 +64,12 @@ class Client if ($directory && $directory !== '.' && !is_dir($directory)) { $parentDirectoryPermissions = fileperms($rootDir); if (!mkdir($directory, $parentDirectoryPermissions, true) && !is_dir($directory)) { - throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory)); + throw new RuntimeException(sprintf('Directory "%s" was not created', $directory)); } } } - public function connect(): void + public function connect() { //При каждой инициализации настройки обновляются из массива $config echo PHP_EOL . 'Starting MadelineProto...' . PHP_EOL; @@ -74,7 +77,7 @@ class Client foreach ($this->sessionsFiles as $file) { $session = static::getSessionName($file); - $this->addSession($session); + $this->addSession($session, true); } $time = round(microtime(true) - $time, 3); @@ -87,22 +90,28 @@ class Client ; } - public function addSession($session): void + public function addSession(string $session, bool $startSession = false) { $settings = (array) Config::getInstance()->get('telegram'); $file = static::getSessionFile($session); $instance = new MadelineProto\API($file, $settings); $instance->async(true); - $instance->setEventHandler(EventHandler::class); $this->instances[$session] = $instance; - Loop::defer(static function() use($instance) { - $instance->loop(['async' => true]); - }); + if ($instance->API->authorized === MTProto::LOGGED_IN) { + $instance->setEventHandler(EventHandler::class); + Loop::defer(static function() use($instance) { + $instance->loop(['async' => true]); + }); + } else { + $instance->loop(function() use($instance) { + yield $instance->start(); + }); + } } public function removeSession($session) { if (empty($this->instances[$session])) { - throw new \InvalidArgumentException('Instance not found'); + throw new InvalidArgumentException('Instance not found'); } $this->instances[$session]->stop(); @@ -117,19 +126,19 @@ class Client public function getInstance(?string $session = null): MadelineProto\API { if (!$this->instances) { - throw new \RuntimeException('No sessions available. Use combinedApi or restart server with --session option'); + throw new RuntimeException('No sessions available. Use combinedApi or restart server with --session option'); } if (!$session) { if (count($this->instances) === 1) { $session = (string) array_key_first($this->instances); } else { - throw new \InvalidArgumentException('Multiple sessions detected. Specify which session to use. See README for examples.'); + throw new InvalidArgumentException('Multiple sessions detected. Specify which session to use. See README for examples.'); } } if (empty($this->instances[$session])) { - throw new \InvalidArgumentException('Session not found.'); + throw new InvalidArgumentException('Session not found.'); } return $this->instances[$session]; diff --git a/src/MadelineProtoExtensions/SystemApiExtensions.php b/src/MadelineProtoExtensions/SystemApiExtensions.php index fe07c62..a5e3a0b 100644 --- a/src/MadelineProtoExtensions/SystemApiExtensions.php +++ b/src/MadelineProtoExtensions/SystemApiExtensions.php @@ -18,7 +18,7 @@ class SystemApiExtensions public function addSession(string $session) { - $this->client->addSession($session); + yield $this->client->addSession($session); return $this->getSessionList(); }