gram-js / gramjs

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

Unable to receive phone code #546

Closed b10c77 closed 1 year ago

b10c77 commented 1 year ago

I tried the code on the main project page, changing the apiId and apiHash to my app settings.

I tried to enter the phone number without the + sign, with double 00's (same as the + sign) and with only the country code. I can't seem to ever receive the code on telegram. Tried already two different accounts. Also tried setting the DC to client.session.setDC(2, "149.154.167.50", 443);

I never receive any code (via SMS or via telegram) to enter on the console to then gain access to the session string.

Any idea what I might be doing wrong?

painor commented 1 year ago

Can you show the full code you tested (hide api_id and hash)

Also make sure this is an already existing account and you are logged in with it in your mobile or desktop client.

b10c77 commented 1 year ago

Can you show the full code you tested (hide api_id and hash)

* don't set the DC. the library will do that automatically

Also make sure this is an already existing account and you are logged in with it in your mobile or desktop client.

const telegram = require ("telegram");
const StringSession = require("telegram/sessions").StringSession;
const input = require("input");

const apiId = 123456;
const apiHash = "123456abcdfg";
const stringSession = new StringSession(""); // fill this later with the value from session.save()

(async () => {
  console.log("Loading interactive example...");
  const client = new TelegramClient(stringSession, apiId, apiHash, {
    connectionRetries: 5,
  });
  await client.start({
    phoneNumber: async () => await input.text("Please enter your number: "),
    password: async () => await input.text("Please enter your password: "),
    phoneCode: async () =>
      await input.text("Please enter the code you received: "),
    onError: (err) => console.log(err),
  });
  console.log("You should now be connected.");
  console.log(client.session.save()); // Save this string to avoid logging in again
})();
Loading interactive example...
[2023-06-02T15:10:35.913] [INFO] - [Running gramJS version 2.17.0]
[2023-06-02T15:10:35.915] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2023-06-02T15:10:36.897] [INFO] - [Connection to 149.154.167.91:443/TCPFull complete!]
[2023-06-02T15:10:36.897] [INFO] - [Using LAYER 158 for initial connect]
? Please enter your number:  XXX000000000 <- XXX = country code, 000000000 phone number
? Please enter the code you received:  

Never receive any code. I'm logged in with telegram lite on MacOS and was able to receive the code to login to my.telegram.org. Also tried with the +XXX country code, same result.

b10c77 commented 1 year ago

Adding baseLogger: new Logger("debug") to new telegram.TelegramClient

[2023-06-03T10:03:46.792] [INFO] - [Running gramJS version 2.17.0]
[2023-06-03T10:03:46.794] [INFO] - [Connecting to 149.154.167.91:443/TCPFull using PromisedNetSocket]
[2023-06-03T10:03:46.794] [DEBUG] - [Connection attempt 0...]
[2023-06-03T10:03:46.794] [DEBUG] - [Connecting]
[2023-06-03T10:03:46.866] [DEBUG] - [Finished connecting]
[2023-06-03T10:03:46.866] [DEBUG] - [Connection success!]
[2023-06-03T10:03:46.866] [DEBUG] - [New auth_key attempt 0...]
[2023-06-03T10:03:46.866] [DEBUG] - [New auth_key attempt ...]
[2023-06-03T10:03:46.947] [DEBUG] - [Starting authKey generation step 1]
[2023-06-03T10:03:46.947] [DEBUG] - [Finished authKey generation step 1]
[2023-06-03T10:03:47.024] [DEBUG] - [Step 2 : Generated a secure aes encrypted data]
[2023-06-03T10:03:47.538] [DEBUG] - [Finished authKey generation step 2]
[2023-06-03T10:03:47.538] [DEBUG] - [Starting authKey generation step 3]
[2023-06-03T10:03:47.883] [DEBUG] - [Finished authKey generation step 3]
[2023-06-03T10:03:47.883] [DEBUG] - [Generated new auth_key successfully]
[2023-06-03T10:03:47.883] [DEBUG] - [auth_key generation success!]
[2023-06-03T10:03:47.883] [DEBUG] - [Starting receive loop]
[2023-06-03T10:03:47.884] [DEBUG] - [Receiving items from the network...]
[2023-06-03T10:03:47.884] [DEBUG] - [Starting send loop]
[2023-06-03T10:03:47.884] [DEBUG] - [Waiting for messages to send...false]
[2023-06-03T10:03:47.884] [INFO] - [Connection to 149.154.167.91:443/TCPFull complete!]
[2023-06-03T10:03:47.884] [INFO] - [Using LAYER 158 for initial connect]
[2023-06-03T10:03:47.886] [DEBUG] - [Assigned msgId = 7240382972656884952 to InvokeWithLayer]
[2023-06-03T10:03:47.886] [DEBUG] - [Encrypting 1 message(s) in 208 bytes for sending]
[2023-06-03T10:03:47.886] [DEBUG] - [Encrypted messages put in a queue to be sent]
[2023-06-03T10:03:47.886] [DEBUG] - [Waiting for messages to send...false]
[2023-06-03T10:03:47.960] [DEBUG] - [Handling bad salt for message 7240382972656884952]
[2023-06-03T10:03:47.960] [DEBUG] - [1 message(s) will be resent]
[2023-06-03T10:03:47.960] [DEBUG] - [Receiving items from the network...]
[2023-06-03T10:03:47.960] [DEBUG] - [Assigned msgId = 7240382972956885144 to InvokeWithLayer]
[2023-06-03T10:03:47.960] [DEBUG] - [Encrypting 1 message(s) in 208 bytes for sending]
[2023-06-03T10:03:47.961] [DEBUG] - [Encrypted messages put in a queue to be sent]
[2023-06-03T10:03:47.961] [DEBUG] - [Waiting for messages to send...false]
[2023-06-03T10:03:47.962] [DEBUG] - [Assigned msgId = 7240382972960884852 to MsgsAck]
[2023-06-03T10:03:47.962] [DEBUG] - [Encrypting 1 message(s) in 36 bytes for sending]
[2023-06-03T10:03:47.962] [DEBUG] - [Encrypted messages put in a queue to be sent]
[2023-06-03T10:03:47.962] [DEBUG] - [Waiting for messages to send...false]
[2023-06-03T10:03:48.036] [DEBUG] - [Handling container]
[2023-06-03T10:03:48.036] [DEBUG] - [Handling new session created]
[2023-06-03T10:03:48.036] [DEBUG] - [Handling acknowledge for 7240382972956885144]
[2023-06-03T10:03:48.037] [DEBUG] - [Receiving items from the network...]
[2023-06-03T10:03:48.103] [DEBUG] - [Handling RPC result for message 7240382972956885144]
[2023-06-03T10:03:48.104] [DEBUG] - [Receiving items from the network...]
[2023-06-03T10:03:48.105] [DEBUG] - [Assigned msgId = 7240382973831852364 to updates.GetState]
[2023-06-03T10:03:48.105] [DEBUG] - [Encrypting 1 message(s) in 20 bytes for sending]
[2023-06-03T10:03:48.105] [DEBUG] - [Encrypted messages put in a queue to be sent]
[2023-06-03T10:03:48.105] [DEBUG] - [Waiting for messages to send...false]
[2023-06-03T10:03:48.105] [DEBUG] - [Assigned msgId = 7240382973831852368 to MsgsAck]
[2023-06-03T10:03:48.105] [DEBUG] - [Encrypting 1 message(s) in 60 bytes for sending]
[2023-06-03T10:03:48.107] [DEBUG] - [Encrypted messages put in a queue to be sent]
[2023-06-03T10:03:48.107] [DEBUG] - [Waiting for messages to send...false]
[2023-06-03T10:03:48.309] [DEBUG] - [Handling RPC result for message 7240382973831852364]
[2023-06-03T10:03:48.312] [DEBUG] - [Receiving items from the network...]
[2023-06-03T10:03:48.312] [DEBUG] - [Assigned msgId = 7240382974659852432 to MsgsAck]
? Please enter your number:  [2023-06-03T10:03:48.326] [DEBUG] - [Encrypting 1 message(s) in 36 bytes for sending]
[2023-06-03T10:03:48.326] [DEBUG] - [Encrypted messages put in a queue to be sent]
[2023-06-03T10:03:48.326] [DEBUG] - [Waiting for messages to send...false]
[2023-06-03T10:03:48.327] [DEBUG] - [Assigned msgId = 7240382974719851896 to MsgsAck]
[2023-06-03T10:03:48.327] [DEBUG] - [Encrypting 1 message(s) in 36 bytes for sending]
[2023-06-03T10:03:48.327] [DEBUG] - [Encrypted messages put in a queue to be sent]
[2023-06-03T10:03:48.327] [DEBUG] - [Waiting for messages to send...false]
? Please enter your number:  XXX000000000
[2023-06-03T10:04:27.755] [DEBUG] - [Assigned msgId = 7240383143935577288 to auth.SendCode]
[2023-06-03T10:04:27.755] [DEBUG] - [Encrypting 1 message(s) in 84 bytes for sending]
[2023-06-03T10:04:27.756] [DEBUG] - [Encrypted messages put in a queue to be sent]
[2023-06-03T10:04:27.756] [DEBUG] - [Waiting for messages to send...false]
[2023-06-03T10:04:27.868] [DEBUG] - [Handling RPC result for message 7240383143935577288]
[2023-06-03T10:04:27.868] [DEBUG] - [Receiving items from the network...]
? Please enter the code you received:  [2023-06-03T10:04:48.107] [DEBUG] - [Assigned msgId = 7240383231533889832 to Ping]
[2023-06-03T10:04:48.107] [DEBUG] - [Encrypting 1 message(s) in 28 bytes for sending]
[2023-06-03T10:04:48.108] [DEBUG] - [Encrypted messages put in a queue to be sent]
[2023-06-03T10:04:48.108] [DEBUG] - [Waiting for messages to send...false]
[2023-06-03T10:04:48.108] [DEBUG] - [Assigned msgId = 7240383231541890208 to MsgsAck]
[2023-06-03T10:04:48.108] [DEBUG] - [Encrypting 1 message(s) in 36 bytes for sending]
[2023-06-03T10:04:48.109] [DEBUG] - [Encrypted messages put in a queue to be sent]
[2023-06-03T10:04:48.109] [DEBUG] - [Waiting for messages to send...false]
[2023-06-03T10:04:48.187] [DEBUG] - [Handling pong for message 7240383231533889832]
[2023-06-03T10:04:48.187] [DEBUG] - [Receiving items from the network...]
b10c77 commented 1 year ago

Ok, I found the possible culprit. I was connected via Mullvad VPN. As soon as I turned off the VPN I was able to receive the code. So they might be preventing logins from known VPN IPs or something similar.