alik0211 / mtproto-core

Telegram API JS (MTProto) client library for Node.js and browser
https://mtproto-core.js.org
GNU General Public License v3.0
632 stars 116 forks source link

changing dcid issue #41

Closed somgupta13 closed 4 years ago

somgupta13 commented 4 years ago

if i have migrated to dcid=5 then while signing in my crendiatals are on dcid 5 so when i am using method exportAuthorization and passing dcid=5 it throws an error dcid invalid

alik0211 commented 4 years ago

@somgupta13

  1. Show your code
  2. Are you using the test server?
  3. What version of the library do you use?
somgupta13 commented 4 years ago
const prompt = require('prompt');
const fs = require('fs');
var data={};
const mtproto = new MTProto({

    api_id:111XXX,
    api_hash:'a115cc7ccbcc6e44XXXXXX',
    test: false,
});
const phone = '+917024XXXX';
const code = '2222';

function sendCode(phone) {
return mtproto.call('auth.sendCode', {
        api_id:111XXXX,
        api_hash: 'a115cc7ccbcc6e445XXXXX',
        // current_number: false,
        phone_number: phone,
        settings: {
        _: 'codeSettings',
        },
    })
}

sendCode(phone)
.catch(error => {
    if (error.error_message.includes('_MIGRATE_')) {
        const [type, nextDcId] = error.error_message.split('_MIGRATE_');
        mtproto.setDefaultDc(+nextDcId);
        return sendCode(phone)
    }
})
.then( result => {
f1(result.phone_code_hash);

})

async function f1(phone_code_hash){
    prompt.start();
    await prompt.get(['code'], async function (err, result) {
        if (err) { console.log(err); }
        else{
            const { user } = await mtproto.call('auth.signIn', {
                phone_number   : phone,
                phone_code_hash: phone_code_hash,
                phone_code     : result.code
            },{syncAuth:false}).then(result => {

            console.log('Logged:', result);
            mtproto.call('help.getConfig').then(result=>{console.log(result.this_dc);});
                mtproto.call('auth.exportAuthorization',{dc_id: 5},)
                    .catch(err=>{
                console.log(err);
            })

        return result;
    }).catch(error => {
            console.log(error);
            process.exit();
    });

 }
});
}
  1. I have passed test:false
  2. version @mtproto/core 3.0.1
alik0211 commented 4 years ago

@somgupta13

  1. Please, format code - https://guides.github.com/features/mastering-markdown/
  2. Log and show all results from .then blocks
  3. Log and show all errors from .catch blocks
somgupta13 commented 4 years ago
const { MTProto } = require('@mtproto/core');
const prompt = require('prompt');
const fs = require('fs');
var data={};
const mtproto = new MTProto({

    api_id:111XXX,
    api_hash:'a115cc7ccbcc6e44XXXXXX',
    test: false,
});
const phone = '+917024XXXX';
const code = '2222';

function sendCode(phone) {
return mtproto.call('auth.sendCode', {

        api_id:111XXXX,
        api_hash: 'a115cc7ccbcc6e445XXXXX',
        // current_number: false,
        phone_number: phone,
        settings: {
        _: 'codeSettings',
        },
    })
}

sendCode(phone)
.catch(error => {
    if (error.error_message.includes('_MIGRATE_')) {
        const [type, nextDcId] = error.error_message.split('_MIGRATE_');
        mtproto.setDefaultDc(+nextDcId);
        return sendCode(phone)
    }
})
.then( result => {
f1(result.phone_code_hash);

})

async function f1(phone_code_hash){
    prompt.start();
    await prompt.get(['code'], async function (err, result) {
        if (err) { console.log(err); }
        else{
            const { user } = await mtproto.call('auth.signIn', {
                phone_number   : phone,
                phone_code_hash: phone_code_hash,
                phone_code     : result.code
            },{syncAuth:false}).then(result => {

            console.log('Logged:', result);
            mtproto.call('help.getConfig').then(result=>{console.log('result2',result.this_dc);});
                mtproto.call('auth.exportAuthorization',{dc_id: 5},)
                    .catch(err=>{
                console.log('error2',err);
            })

        return result;
    }).catch(error => {
            console.log(error);
            process.exit();
    });

 }
});
}

results:

prompt: code:  44216
Logged: {
  _: 'auth.authorization',
  pFlags: {},
  flags: 0,
  user: {
    _: 'user',
    pFlags: { self: true },
    flags: 1107,
    id: 12331161,
    access_hash: '202675752045680',
    first_name: 'Sandhya',
    phone: '91702464',
    status: { _: 'userStatusOnline', expires: 1588600686 }
  }
}
result2 5
error2 { _: 'rpc_error', error_code: 400, error_message: 'DC_ID_INVALID' }
alik0211 commented 4 years ago

@somgupta13

  1. If you want to work with all the DC, the library will automatically copy the authorization. Delete:
    { syncAuth: false }
  2. In auth.exportAuthorization the dc_id parameter must not be equal to the this_dc. You are currently logged in to this_dc
somgupta13 commented 4 years ago

actually if I delete the syncAuth:false it does not respond anything and the screen is holded more than 20 minutes due to copy of authorisation to all dc and i did not get any response back because it takes too long

somgupta13 commented 4 years ago

if i want to make a call to any method it gives error migrate to 5th dc so i want the id and bytes to login to 5th dc so that i dont need to type phone code again and again

alik0211 commented 4 years ago

@somgupta13 you mean you need to keep your authorization?

alik0211 commented 4 years ago

@somgupta13 update to version 3.1.0. This may be fix you issue

somgupta13 commented 4 years ago

can you give the example of how to get values from local storage so that we need to signIn again again via otp

alik0211 commented 4 years ago

You don't need to work with the storage. You log in once and that's it

somgupta13 commented 4 years ago

@alik0211 by first time logging in it terminates the seesion and then again i need to login

alik0211 commented 4 years ago

@somgupta13

  1. Remove folder with auth data
  2. Use this code - https://github.com/alik0211/mtproto-core/blob/master/src/__playground/node.js
somgupta13 commented 4 years ago

1."Remove folder with auth data" where is this present? 2.https://github.com/alik0211/mtproto-core/blob/master/src/__playground/node.js this code is not working for test =true it gives error that dc 5 not found and for test:false it does not give any ouput id does even executes sendcode

alik0211 commented 4 years ago

@somgupta13

  1. When you run code in console print Auth data is located in <path>
  2. There are only three DC on test servers
somgupta13 commented 4 years ago
mtproto.call('messages.sendMessage',{
    peer:{
     _ :'inputPeerUser',
     user_id:12707XXXX,
     access_hash:93874605735XXX
    },
    message:"hiii",
    random_id:6789

   })

error { _: 'rpc_error', error_code: 400, error_message: 'PEER_ID_INVALID' } sendMessages does not work with 3.1.1 but it works with 3.0.0

alik0211 commented 4 years ago

@somgupta13 the user_id and access_hash parameters are inserted in plain? You should get access_hash value from the user constructor (it works for me) - https://core.telegram.org/constructor/inputPeerUser image

somgupta13 commented 4 years ago

yes i used that only it works in 3.0.0 but it does not work here

alik0211 commented 4 years ago

I don't understand you. Show the code!