-
Notifications
You must be signed in to change notification settings - Fork 48
Multiple Telegram Account
Setiawan edited this page Aug 12, 2020
·
7 revisions
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!'
]);
}
}