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:
Setiawan Hu 2020-08-13 01:29:39 +07:00
parent e007b1c592
commit 328eeebb47

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