-
Notifications
You must be signed in to change notification settings - Fork 48
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
--modeloption is optional if we want to create our own model for thetelegram_sessionmigration table. This option will generatingTelegramSessionmodel for the migration table. -
The
--forceoption is optional. We may use it for ignoring the existing migration file and generatedTelegramSessionmodel.
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 generatedTelegramSessionmodel, the generatedTelegramSessionrecord'sid, any Model which hassession_filefield, or integer which will be considered asidfield for defined table fromtelegram.phpconfig file (and of course, the table must havesession_filefield too 😄). -
If both
get()andmake()second argument ($config) is not passed, then thesettingsarray from the thetelegram.phpconfig file will be used.