zerobias / telegram-mtproto

Telegram client api (MTProto) library
MIT License
620 stars 136 forks source link

RangeError: Wrong length! Error. or Error: 400 PHONE_CODE_EXPIRED CODE#400 PHONE_CODE_EXPIRED #42

Closed sajadghobadi closed 5 years ago

sajadghobadi commented 7 years ago

here is sample when i receive activation code and use it to sign in telegram by below sample code get this error. RangeError: Wrong length! use babel for transcompile to es5. code :

const login = async () => {
  try {

    const phone = '+98........................'

    const info = JSON.parse(fs.readFileSync('code.json'));
    const code = info.phoneCode;
    const phone_code_hash = info.phone_code_hash;

    const res = await telegram('auth.signIn', {
      phone_number: phone,
      phone_code_hash,
      phone_code: code
    })

    const { user } = res
    const {
      first_name = '',
      username = ''
    } = user

    console.log('signIn', first_name, username, user.phone)
    return first_name
  } catch (error) {
    console.error(error)
  }
}

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): RangeError: Wrong length!

wfjsw commented 7 years ago

You should check every constants to make sure they are valid first.

always surround all your codes with three `

zerobias commented 7 years ago

@sajadghobadi How did you receive a phone code hash from a file? Looks like it does not exist at all

Hash is what you must inquire from the server

Example

wfjsw commented 7 years ago

@zerobias Probably he split the login process into two parts: getCode and signIn.

sajadghobadi commented 7 years ago

@zerobias and @wfjsw thanks lot for response this error occurre for cheep!(sorry) mistake. i must pass phone_code_hash to signIn method but in truly i send undefined!!!

change and correct phone hash code to const res = await telegram('auth.signIn', { phone_number: phone, phone_code_hash:phone_code_hash, phone_code: code })

My final target is this : read messages from channel list(example:['channel1','channel2','chanell3'])

for do this i run below steps

  1. call auth.sendCode method and write _phone_code_hash_ to file named code.json
  2. write manually activation code that i received in my phone to file named code.json
  3. then i trying to login by those values in code.json by call auth.signIn (@wfjsw you right exactly in split send code and login in to step)
  4. but i get this error always : Error: 400 PHONE_CODE_EXPIRED CODE#400 PHONE_CODE_EXPIRED, i exactly enter code that received.

please help me if you can. thnaks. sorry for my bad en

sajadghobadi commented 7 years ago

@zerobias you can't help?

zerobias commented 7 years ago

@sajadghobadi Have you used the code as string or number type? Phone, code and hash fields must be string, not number.

I think this code should fails

const code = 12345

telegram('auth.signIn', { phone_number, phone_code_hash, phone_code: code })

So better to try this

const code = "12345"

telegram('auth.signIn', { phone_number, phone_code_hash, phone_code: code })
wfjsw commented 7 years ago

@sajadghobadi Hmm...Expired? You should try to understand it. The main problem is that you are too slow to get you authenticated. Try to be quick next time.

However, depend on what you are doing, there is a rare case. According to https://t.me/MadelineProto/23

#UndocumentedAPIChangeOfTheDay 
Telegram automatically invalidates your login code if it is sent to any chat on telegram before the login, this problem can be avoided simply by appending or pretending some chars to the code.
Thanks to @usage_tg for finding that out.

Time to debug on your own.

FragsterAt commented 7 years ago

@sajadghobadi I also had such a error until I request code and sent the result within one connection

wfjsw commented 7 years ago

(also make sure that you are using the same auth keypair for auth)

javeedrahman commented 7 years ago

@FragsterAt how did you grab the code from telegram chat to the code automatically? , i am getting response only for phone_code_hash and phone_code sent from telegram official to my telegram chat.

I copied the login_code from app, try pasted in

client("auth.signIn", {
    phone_number:   number,
    phone_code_hash: result.phone_code_hash,
    phone_code:      "39480",
}).then((result) => {
    console.log("User:", result);
});

But always getting PHONE_CODE_EXPIRED.

@wfjsw tried this but no luck appending or pretending return PHONE_CODE_INVALID Some one please help.

#UndocumentedAPIChangeOfTheDay 
Telegram automatically invalidates your login code if it is sent to any chat on telegram before the login, this problem can be avoided simply by appending or pretending some chars to the code.
Thanks to @usage_tg for finding that out.
goodmind commented 7 years ago

@javeedrahman please use three backticks for code blocks

javeedrahman commented 7 years ago

@goodmind okay sure got it.

javeedrahman commented 7 years ago

Here is the Sample code trying::

const phone = {
  num : '+9199999999999',
  code: '22222'
}
const config = {
  // NOTE: if you FORK the project you MUST use your APP ID.
  // Otherwise YOUR APPLICATION WILL BE BLOCKED BY TELEGRAM
  // You can obtain your own APP ID for your application here: https://my.telegram.org
  id  : 49631,
  hash: 'fb050b8f6771e15bfda5df2409931569'
}

var telegram = MTProto({
    layer: 57,
    api_id: config.id,
});

  const run = async ()  => {
      try {
        const { phone_code_hash } = await telegram('auth.sendCode', {
              phone_number  : phone.num,
              current_number: false,
              api_id        : config.id,
              api_hash      : config.hash
        })
        console.log('phone_code_hash', phone_code_hash)
        res = await telegram('auth.signIn', {
          phone_number: phone.num,
          phone_code_hash,
          phone_code  : "83118"
        })
        console.log('signIn', res)
        console.log('\n Logined as user')
        console.dir(res.user, { colors: true })
      } catch (err) {
        console.log('err', err)
      }
  }
  run()
dundas commented 6 years ago

@javeedrahman @sajadghobadi Were you able to figure this out?

onezens commented 6 years ago

@dundas https://github.com/zerobias/telegram-mtproto/issues/124#issuecomment-367972932

terrasoff commented 6 years ago