Readme update and trailing slash fix.

This commit is contained in:
Alexander Pankratov 2020-01-15 01:44:03 +03:00
parent a8319368e6
commit 8fb85d77b4
4 changed files with 23 additions and 17 deletions

View File

@ -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 `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. * CombinedAPI (multiple sessions) support.
If running with multiple sessions use include 'session' in path, before method, to define which session to use for request: When running multiple sessions, need to define which session to use for request.
* `php server.php --session=session --session=bot --session=xtrime` Each session is stored in `sessions/{$session}.madeline`. Nested folders supported.
* `http://127.0.0.1:9503/api/xtrime/getSelf`
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/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. * 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: When using CombinedAPI (multiple accounts) 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. 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) PHP websocket client example: [websocket-events.php](https://github.com/xtrime-ru/TelegramApiServer/blob/master/examples/websocket-events.php)

View File

@ -28,16 +28,18 @@ $options = [
if ($options['help']) { if ($options['help']) {
$help = 'Fast, simple, async php telegram parser: MadelineProto + Swoole Server $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: Options:
--help Show this message --help Show this message
-a --address Server ip (optional) (example: 127.0.0.1) -a --address Server ip (optional) (example: 127.0.0.1)
-p --port Server port (optional) (example: 9503) -p --port Server port (optional) (example: 9503)
-s --session Prefix for session file (optional) (example: xtrime). -s --session Name for session file (optional) (example: xtrime).
Multiple sessions can be used via CombinedAPI. Example "--session=user --session=bot" Multiple sessions can be used (MadelineProto CombinedAPI).
If running multiple sessions, then "session" parameter must be provided with every request. Example: "--session=user --session=bot"
See README for example requests. 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) Also all options can be set in .env file (see .env.example)

View File

@ -43,6 +43,7 @@ class Client
if (!$session) { if (!$session) {
return null; return null;
} }
$session = rtrim(trim($session), '/');
$session = static::$sessionFolder . '/' . $session . static::$sessionExtension; $session = static::$sessionFolder . '/' . $session . static::$sessionExtension;
$session = str_replace('//', '/', $session); $session = str_replace('//', '/', $session);
return $session; return $session;
@ -129,11 +130,11 @@ class Client
} }
if (!$session) { 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])) { if (empty($this->MadelineProtoCombined->instances[$session])) {
throw new \InvalidArgumentException('Session not found'); throw new \InvalidArgumentException('Session not found.');
} }
return $this->MadelineProtoCombined->instances[$session]; return $this->MadelineProtoCombined->instances[$session];

View File

@ -41,11 +41,11 @@ class Router
foreach (['GET', 'POST'] as $method) { foreach (['GET', 'POST'] as $method) {
$this->router->addRoute($method, '/api/{method}[/]', $apiHandler); $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[/]', $eventsHandler);
$this->router->addRoute('GET', '/events/{session:.*?}[/]', $eventsHandler); $this->router->addRoute('GET', '/events/{session:.*?[^/]}[/]', $eventsHandler);
} }