gram-js / gramjs

NodeJS/Browser MTProto API Telegram client library,
MIT License
1.28k stars 179 forks source link

Error when starting client #162

Closed KidA001 closed 3 years ago

KidA001 commented 3 years ago

I'm getting an error Cannot send requests while disconnected. You need to call .connect() when I call start() on my gramJs client (see code below). This error seems out of the blue as I'm not sure what if anything has changed in my code. I've logged out any existing sessions to this client.

My code:

// `sessionToken` is an empty string in this situation
const session = new StringSession(sessionToken);
const client = new TelegramClient(session, creds.apiId, creds.apiHash, { connectionRetries: 0 });
await client.start({
  phoneNumber: creds.phone,
  phoneCode: async () => authCode(),
  password: async () => '',
  onError: (err) => handleConnectionErr(err),
});
Error: Cannot send requests while disconnected. You need to call .connect()
    at MTProtoSender.send (path-to-app/node_modules/telegram/network/MTProtoSender.js:201:19)
    at TelegramClient.<anonymous> (path-to-app/node_modules/telegram/client/TelegramClient.js:940:32)
    at Generator.next (<anonymous>)
    at fulfilled (path-to-app/node_modules/telegram/client/TelegramClient.js:24:58)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

Any insight appreciated

KidA001 commented 3 years ago

For some reason connectionRetries needs to be at least 1.

painor commented 3 years ago

yeah sometimes telegram would disconnect you randomly and you'll need to recreate the connection. It's usually safer to put it something like 5 since the disconnect can happen twice.