diff --git a/src/Factories/MadelineProtoFactory.php b/src/Factories/MadelineProtoFactory.php new file mode 100644 index 0000000..f48f635 --- /dev/null +++ b/src/Factories/MadelineProtoFactory.php @@ -0,0 +1,80 @@ +database = $manager->connection(); + $this->table = $table; + } + + /** + * Get the MadelineProto (session) instance from session table. + * + * @param int|Model $session can be either id or model instance of TelegramSession which + * generated from madeline-proto:multi-session --model command + * @param array|null $config if this parameter is null, then the config from telegram.php + * file will be used + * @return MadelineProto + */ + public function get($session, array $config = null) + { + if (is_null($config)) { + $config = config('telegram.settings'); + } + + if (is_int($session)) { + $session = $this->database->table($this->table)->find($session); + + $sessionFile = $session->session_file; + } else { + $sessionFile = $session->session_file; + } + + return $this->make($sessionFile, $config); + } + + /** + * Generating MadelineProto (session) instance. + * + * @param string $sessionFile + * @param array $config + * @return MadelineProto + */ + public function make(string $sessionFile, array $config) + { + if (!file_exists(storage_path("app/telegram/"))) { + mkdir(storage_path("app/telegram"), 0755); + } + + $client = new API(storage_path("app/telegram/$sessionFile"), $config); + + return new MadelineProto($client); + } +}