gram-js / gramjs

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

PHONE_CODE_INVALID when trying to login using test DCs #70

Closed hit6 closed 3 years ago

hit6 commented 3 years ago

I'm trying to login using the test DC, as stated in telegram documentation: https://core.telegram.org/api/auth#test-phone-numbers

This is my code:

import { Api, TelegramClient } from "telegram";
import { StringSession } from "telegram/sessions";
import { Logger } from "telegram/extensions";

import {
  TELEGRAM_API_ID as apiId,
  TELEGRAM_API_HASH as apiHash,
} from "./config";

const phoneNumber = "9996627308";
const phoneCode = "22222";

const createClient = async (stringSession?: string) => {
  const session = new StringSession(stringSession);
  const options = { connectionRetries: 5, baseLogger: new Logger("debug") };
  const client = new TelegramClient(session, apiId, apiHash, options);
  client.session.setDC(2, "149.154.167.50", 443);
  await client.connect();
  return client;
};

const getLoginCodeCommand = (phoneNumber: string) => {
  const settings = new Api.CodeSettings();
  const args = { phoneNumber, apiId, apiHash, settings };
  return new Api.auth.SendCode(args);
};

(async () => {
  const client = await createClient();
  const response = await client.invoke(getLoginCodeCommand(phoneNumber));
  const { phoneCodeHash } = response;
  const args = { phoneNumber, phoneCode, phoneCodeHash };
  const result = await client.invoke(new Api.auth.SignIn(args));
  console.log("RESULT", result);
})();

But I keep receiving RPCError: PHONE_CODE_INVALID again and again. Any ideas on why it's not woring?

Thanks.

painor commented 3 years ago

I think phone code is 6 numbers not 5

hit6 commented 3 years ago

Nah, I'm pretty sure it's 5. But anyway I've tried with multiple phoneCode lengths ranging from 1 to 10 and it's all the same. I get that error every single time.

painor commented 3 years ago

well anyway, the issue in your case is that the code should be an integer, not a string so. const phoneCode = 22222;

hit6 commented 3 years ago

Typescript will only accept an string there. Furthermore, if I force it to be an integer, it'll crash immediately:

...
...
  const args = { phoneNumber, phoneCode: 22222, phoneCodeHash };
  const result = await client.invoke(new (Api as any).auth.SignIn(args));
...
...

will result in:

/home/hit/Jobs/telegramApp/tgApp/node_modules/telegram/tl/generationHelpers.js:239
            throw Error(`Bytes or str expected, not ${data.constructor.name}`);
                  ^

Error: Bytes or str expected, not Number
    at serializeBytes (/home/hit/Jobs/telegramApp/tgApp/node_modules/telegram/tl/generationHelpers.js:239:19)
    at argToBytes (/home/hit/Jobs/telegramApp/tgApp/node_modules/telegram/tl/api.js:110:20)
    at VirtualClass.getBytes (/home/hit/Jobs/telegramApp/tgApp/node_modules/telegram/tl/api.js:294:42)
    at new RequestState (/home/hit/Jobs/telegramApp/tgApp/node_modules/telegram/network/RequestState.js:9:29)
    at MTProtoSender.send (/home/hit/Jobs/telegramApp/tgApp/node_modules/telegram/network/MTProtoSender.js:203:23)
    at Object.<anonymous> (/home/hit/Jobs/telegramApp/tgApp/node_modules/telegram/client/users.js:41:48)
    at Generator.next (<anonymous>)
    at fulfilled (/home/hit/Jobs/telegramApp/tgApp/node_modules/telegram/client/users.js:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
painor commented 3 years ago

well just found the issue. it appears that you have the wrong IP somehow. this code worked and logged me in. const phoneCode = "222222"; // yes it's 6 numbers

const createClient = async (stringSession?: string) => {
    const session = new StringSession(stringSession);
    const options = { connectionRetries: 5, baseLogger: new Logger("debug") };
    const client = new TelegramClient(session, apiId, apiHash, options);
    client.session.setDC(2, "149.154.167.40", 443); // notice how it's 40 not 50
    await client.connect();
    return client;
};
hit6 commented 3 years ago

Yep, you are totally right I was using the production IP 🤦.

Also the 6 digit code works, the telegram documentation is wrong where they say:

https://core.telegram.org/api/auth#test-phone-numbers

A user like this would always get XXXXX as the login confirmation code (the DC number, repeated five times)

Thank you very much for your help :)