-
Notifications
You must be signed in to change notification settings - Fork 50
Multiple Telegram Account
For multiple telegram accounts (and dynamically), we need to store the all session file name to determine who's the owner of the session file.
Run the following artisan command:
php artisan madeline-proto:multi-session [--model --force]
This command will generate the telegram_session
migration table.
Note:
-
The
--model
option is optional if we want to create our own model for thetelegram_session
migration table. This option will generatingTelegramSession
model for the migration table. -
The
--force
option is optional. We may use it for ignoring the existing migration file and generatedTelegramSession
model.
Then set the relation to the "telegram account owner model", it can be either HasOne
or HasMany
relation. Refer to the example below:
//...
use App\TelegramSession;
class User extends Model {
//...
public function telegramSession(){
return $this->hasOne(TelegramSession::class);
}
//or
public function telegramSessions(){
return $this->hasMany(TelegramSession::class);
}
}
Insert the session data, we can refer to this following example:
//...
use Hu\MadelineProto\Factories\MadelineProtoFactory;
use Hu\MadelineProto\Facades\Factory;
class YourController extends Controller {
/**
* @var MadelineProtoFactory
*/
private $factory;
public function __construct(MadelineProtoFactory $factory)
{
$this->factory = $factory;
}
/**
* Handle new telegram account session.
*
* @param Request $request
* @return JsonResponse
*/
public function newSession(Request $request) {
//Insert the session into the telegram_sessions table
$user = User::find($request->get('user_id'));
$telegramSession = $user->telegramSession()->create([
'session_file' => "{$user->name}.madeline"
]);
//You can either use one of this following method
$madelineProto = $this->factory->get($telegramSession);
//or
$madelineProto = Factory::get($telegramSession);
$madelineProto->phoneLogin($request->get('phone_number'));
return response()->json([
'message' => 'Phone code sent!'
]);
}
/**
* Handle submit telegram account login code.
*
* @param Request $request
* @return JsonResponse
*/
public function confirmCode(Request $request) {
$user = User::find($request->get('user_id'));
$telegramSession = $user->telegramSession;
//You can either use one of this following method
$madelineProto = $this->factory->get($telegramSession);
//or
$madelineProto = Factory::get($telegramSession);
$madelineProto->completePhoneLogin($request->get('code'));
return response()->json([
'message' => 'Account logged in!'
]);
}
}
If we know the session file name, we can use make
method:
//YourController.php
//...
use Hu\MadelineProto\Factories\MadelineProtoFactory;
use Hu\MadelineProto\Facades\Factory;
//...
Factory::make($filename, $config);
//or
$this->factory->make($filename);
//...
Notes:
-
The factory's
get()
method can accept generatedTelegramSession
model, the generatedTelegramSession
record'sid
, any Model which hassession_file
field, or integer which will be considered asid
field for defined table fromtelegram.php
config file (and of course, the table must havesession_file
field too 😄). -
If both
get()
andmake()
second argument ($config
) is not passed, then thesettings
array from the thetelegram.php
config file will be used.