mirror of
https://github.com/danog/laravel-madeline-proto.git
synced 2024-11-26 11:54:40 +01:00
Added MadelineProtoFactory class
This factory class is used for generating MadelineProto instance from given TelegramSession model instance, TelegramSession record id, or session file path Note: all the danog/MadelineProto session file will be stored in `storage/app/telegram/` directory.
This commit is contained in:
parent
e007b1c592
commit
328eeebb47
80
src/Factories/MadelineProtoFactory.php
Normal file
80
src/Factories/MadelineProtoFactory.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Hu\MadelineProto\Factories;
|
||||
|
||||
use danog\MadelineProto\API;
|
||||
use Hu\MadelineProto\MadelineProto;
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class MadelineProtoFactory
|
||||
{
|
||||
/**
|
||||
* @var Connection
|
||||
*/
|
||||
private $database;
|
||||
|
||||
/**
|
||||
* Table name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $table;
|
||||
|
||||
/**
|
||||
* SessionFactory constructor.
|
||||
*
|
||||
* @param DatabaseManager $manager
|
||||
* @param string $table
|
||||
*/
|
||||
public function __construct(DatabaseManager $manager, string $table)
|
||||
{
|
||||
$this->database = $manager->connection();
|
||||
$this->table = $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the MadelineProto (session) instance from session table.
|
||||
*
|
||||
* @param int|Model $session can be either <b>id</b> or model instance of <b>TelegramSession</b> which
|
||||
* generated from <u>madeline-proto:multi-session --model</u> command
|
||||
* @param array|null $config if this parameter is null, then the config from <b>telegram.php</b>
|
||||
* 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user