diff --git a/README.md b/README.md index 8e2ef25..69774fa 100644 --- a/README.md +++ b/README.md @@ -67,18 +67,21 @@ Fast, simple, async php telegram api server: `http://127.0.0.1:9503/api/getInfo/?id=@xtrime` or `http://127.0.0.1:9503/api/getInfo/?abcd=@xtrime` works the same * CombinedAPI (multiple sessions) support. - If running with multiple sessions use include 'session' in path, before method, to define which session to use for request: - * `php server.php --session=session --session=bot --session=xtrime` - * `http://127.0.0.1:9503/api/xtrime/getSelf` + When running multiple sessions, need to define which session to use for request. + Each session is stored in `sessions/{$session}.madeline`. Nested folders supported. + + Examples: + * `php server.php --session=bot --session=users/xtrime --session=users/user1` * `http://127.0.0.1:9503/api/bot/getSelf` - * `http://127.0.0.1:9503/api/session/getSelf` + * `http://127.0.0.1:9503/api/users/xtrime/getSelf` + * `http://127.0.0.1:9503/api/users/user1/getSelf` + * sessions file paths are: `sessions/bot.madeline`, `sessions/users/xtrime.madeline` and `sessions/users/user1.madeline` - Each session is store in `{$session}.madeline` file in root folder of library. * EventHandler updates via websocket. Connect to `ws://127.0.0.1:9503/events`. You will get all events in json. - Each event stored inside object, where key is name of session which created event. + Each event is json object. Key is name of session, which created event. - When using CombinedAPI (multiple account) name of session can be added to path of websocket endpoint: - `ws://127.0.0.1:9503/events/session_name`. This endpoint will send events only from given session. + When using CombinedAPI (multiple accounts) name of session can be added to path of websocket endpoint: + This endpoint will send events only from `users/xtrime` session: `ws://127.0.0.1:9503/events/users/xtrime` PHP websocket client example: [websocket-events.php](https://github.com/xtrime-ru/TelegramApiServer/blob/master/examples/websocket-events.php) diff --git a/server.php b/server.php index b49dd19..ed5fa51 100644 --- a/server.php +++ b/server.php @@ -28,16 +28,18 @@ $options = [ if ($options['help']) { $help = 'Fast, simple, async php telegram parser: MadelineProto + Swoole Server -usage: php server.php [--help] [-a=|--address=127.0.0.1] [-p=|--port=9503] [-s=|--session=] +usage: php server.php [--help] [-a=|--address=127.0.0.1] [-p=|--port=9503] [-s=|--session=session] Options: --help Show this message -a --address Server ip (optional) (example: 127.0.0.1) -p --port Server port (optional) (example: 9503) - -s --session Prefix for session file (optional) (example: xtrime). - Multiple sessions can be used via CombinedAPI. Example "--session=user --session=bot" - If running multiple sessions, then "session" parameter must be provided with every request. - See README for example requests. + -s --session Name for session file (optional) (example: xtrime). + Multiple sessions can be used (MadelineProto CombinedAPI). + Example: "--session=user --session=bot" + Each session is stored in `sessions/{$session}.madeline`. + Nested folders supported. + See README for more examples. Also all options can be set in .env file (see .env.example) diff --git a/src/Client.php b/src/Client.php index d483a69..903445e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -43,6 +43,7 @@ class Client if (!$session) { return null; } + $session = rtrim(trim($session), '/'); $session = static::$sessionFolder . '/' . $session . static::$sessionExtension; $session = str_replace('//', '/', $session); return $session; @@ -129,11 +130,11 @@ class Client } if (!$session) { - throw new \InvalidArgumentException('Multiple sessions detected. You need to specify which session to use'); + throw new \InvalidArgumentException('Multiple sessions detected. Specify which session to use. See README for examples.'); } if (empty($this->MadelineProtoCombined->instances[$session])) { - throw new \InvalidArgumentException('Session not found'); + throw new \InvalidArgumentException('Session not found.'); } return $this->MadelineProtoCombined->instances[$session]; diff --git a/src/Server/Router.php b/src/Server/Router.php index 6727eb2..6801a6d 100644 --- a/src/Server/Router.php +++ b/src/Server/Router.php @@ -41,11 +41,11 @@ class Router foreach (['GET', 'POST'] as $method) { $this->router->addRoute($method, '/api/{method}[/]', $apiHandler); - $this->router->addRoute($method, '/api/{session:.*?}/{method}[/]', $apiHandler); + $this->router->addRoute($method, '/api/{session:.*?[^/]}/{method}[/]', $apiHandler); } $this->router->addRoute('GET', '/events[/]', $eventsHandler); - $this->router->addRoute('GET', '/events/{session:.*?}[/]', $eventsHandler); + $this->router->addRoute('GET', '/events/{session:.*?[^/]}[/]', $eventsHandler); }