Skip to content

Commit

Permalink
Readme update and trailing slash fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Jan 14, 2020
1 parent a831936 commit 8fb85d7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions server.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@
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)
Expand Down
5 changes: 3 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static function getSessionFile(?string $session): ?string
if (!$session) {
return null;
}
$session = rtrim(trim($session), '/');
$session = static::$sessionFolder . '/' . $session . static::$sessionExtension;
$session = str_replace('//', '/', $session);
return $session;
Expand Down Expand Up @@ -129,11 +130,11 @@ public function getInstance(?string $session = null): MadelineProto\API
}

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];
Expand Down
4 changes: 2 additions & 2 deletions src/Server/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ private function setRoutes($client): void

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);
}


Expand Down

0 comments on commit 8fb85d7

Please sign in to comment.