danog / MadelineProto

Async PHP client API for the telegram MTProto protocol
https://docs.madelineproto.xyz
GNU Affero General Public License v3.0
2.81k stars 640 forks source link

How can I check if my session is valid? #1339

Closed PavelSachenko closed 1 year ago

PavelSachenko commented 1 year ago
  1. I logged in through Telegram and saved the session.
  2. The system is working correctly (sending and receiving messages).
  3. I decided to clear all sessions on my Telegram account.

How can I check and delete the files of the old session that are no longer valid?

I use: "danog/madelineproto": "7.0.152", php 8.2.3 laravel 10.8.0

I try this way (recommended by chatGPT):

                $MadelineProto = new API($session, [
                    'app_info' => [
                        'api_id' => $tgAccount->api_id,
                        'api_hash' => $tgAccount->app_hash,
                    ],
                    'on_dc_update' => function ($dcId) {
                        throw new Exception('some errors');
                    },
                    'async' => false, // Set async to false
                    'pwr_chat_upload' => false,
                    'connection_timeout' => 10,
                    'on_error' => function ($e) {
                        throw new \Exception('MadelineProto failed to start: ' . $e->getMessage());
                    }
                ]);

But I had a cycle: Exception: stream_socket_client(): Unable to connect to unix:///var/www/storage/app/session.flbIhU8jyp.ipc (Connection refused) in functions.php:43 Serialization: stream_socket_client(): Unable to connect to unix:///var/www/storage/app/session.flbIhU8jyp.ipc (Connection refused) while connecting to IPC socket

danog commented 1 year ago

The example provided by chatgpt is completely incorrect, I don't recommend using chatgpt to write MadelineProto code, because it doesn't have enough information about MadelineProto to generate working code.

Simply use the getSelf function to check if the session is still active: if it's still active, it will return flase. To avoid the stream socket client error make sure to use v8.