Skip to content

Multiple Telegram Account

Setiawan edited this page Dec 10, 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:

  1. The --model option is optional if we want to create our own model for the telegram_session migration table. This option will generating TelegramSession model for the migration table.

  2. The --force option is optional. We may use it for ignoring the existing migration file and generated TelegramSession 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 one of the followings as first argument:

    • The generated TelegramSession Model instance,
    • The generated TelegramSession record's id,
    • Any Model instance which has session_file field, or
    • Integer which will be considered as id field for defined table from telegram.php config file (and of course, the table must have session_file field too 😄).
  • If both get() and make() second argument ($config) is not passed, then the settings array from the the telegram.php config file will be used.

Clone this wiki locally