zerobias / telegram-mtproto

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

How to save login session of telegram in nodejs #200

Closed rambozindia closed 5 years ago

rambozindia commented 6 years ago

I am logged in successfully. but after restart the nodejs app. it still not logged in. how to stay logged in. How to save the authorization code locally?? I new in this module.

ismenc commented 6 years ago

My workaround on this is saving the storage data to a json file. For this, i am using a custom Storage class, but firstly i recomend you to check this example as i think it is already implemented. https://github.com/zerobias/telegram-mtproto/blob/25634da78dd9dfa26a14deb571e997016b6e3b49/examples/node-storage.js#L12-L14

Let me know if you have problems with that

rambozindia commented 6 years ago

Yes. I am Stored the data. But how to attach or reconnect with this auth_key_id. Showing AUTH_KEY_UNREGISTERED While requesting another method on another session.

ismenc commented 6 years ago

I have done so like this:

const cookie = fs.existsSync('data/session.json') ? JSON.parse(fs.readFileSync('data/session.json')) : {}
console.log("Cookie file found")
telegram = new Telegram(app_id, hash, cookie)

Basically if session.json exists then cookie takes its value, else it will be { }. Once telegram object is created with cookie, you can perform any op. Where session.json has the following structure (may vary in your case):

{
  "dc2_auth_key":
    "longHash",
  "dc2_server_salt": "shortHash",
  "dc": 4,
  "dc4_auth_key":
    "anotherLongHash",
  "dc4_server_salt": "anotherShortHash"
}

Inside telegram object you should have your constructor with its own storage and mtproto initilizer:

export default class Telegram {
    constructor(app_id, hash, data) {

        this.__storage = new Storage(data)
        this.__app_id = app_id
        this.__hash = hash

        const server = { webogram: true, dev: false }
        const api = {
            invokeWithLayer: 0xda9b0d0d,
            layer: 57,
            initConnection: 0x69796de9,
            api_id: app_id,
            app_version: '1.0.1',
            lang_code: 'en'
        }
        this.__connector = MTProto({ server, api, app: { storage: this.__storage } })

        this.__phone = null
        this.__phone_code_hash = null

    }

// Class methods and so...
async requestCode(phone){
  const config = {
            phone_number: phone,
            current_number: false,
            api_id: this.__app_id,
            api_hash: this.__hash
    }

   const result = await this.__connector('auth.sendCode', config)

  this.__phone_code_hash = res.phone_code_hash
  this.__phone = phone
  return result
}
rambozindia commented 6 years ago

Showing this error when attaching session

config errors C:\Users\india\OneDrive\Desktop\project\node_modules\telegram-mtproto\lib\service\main\config-val…:117 ObjectdataPath: ".api"keyword: "required"message: "should have required property 'api_id'"params: ObjectschemaPath: "#/properties/api/required"proto: Object C:\Users\india\OneDrive\Desktop\project\node_modules\telegram-mtproto\lib\service\main\config-val…:112 Uncaught Error: wrong config fields

ismenc commented 6 years ago

Can you check your mtproto version at package.json? Also, are you sure you are initializing MTProto object like in examples?

rambozindia commented 6 years ago

"telegram-mtproto": "^2.2.2",

yes. i m code like https://github.com/zerobias/telegram-mtproto/blob/25634da78dd9dfa26a14deb571e997016b6e3b49/examples/node-storage.js#L12-L14

given example