gram-js / gramjs

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

How to handle registration when logging in? #144

Closed trizau closed 3 years ago

trizau commented 3 years ago

I use these codes:

import { TelegramClient } from 'telegram'
import { StringSession }  from 'telegram/sessions'
import input from 'input'; // npm i 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('number ?'),
        password: async () => await input.text('password?'),
        phoneCode: async () => await input.text('Code ?'),
        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
    await client.sendMessage('me', { message: 'Hello!' });
})()

If the mobile phone number has not been registered, the following problems will be reported:

api.js:402 CastError: Found wrong type for lastName. expected string but received undefined.If you think this is a mistake please report it.
    at VirtualClass.assertType (api.js:403)
    at VirtualClass.validate (api.js:345)
    at VirtualClass.getBytes (api.js:412)
    at new RequestState (RequestState.ts:20)
    at MTProtoSender.send (MTProtoSender.ts:278)
    at Object.<anonymous> (users.ts:35)
    at Generator.next (<anonymous>)
    at fulfilled (VM3103 users.js:5)

At this time, I must use client.disconnect(), otherwise it will continue to loop and cause memory leaks

painor commented 3 years ago

you can pass firstName and lastName params to start and it will use that to register a new account.

trizau commented 3 years ago

you can pass firstName and lastName params to start and it will use that to register a new account.

Thanks, it was my fault

pythonjsgo commented 2 years ago

Hello, I have same promblem, please show how you passed name parameters. My code:

    await client.start({
        phoneNumber: '+'+resp.number.toString(),
        //phoneCode: async () => await sms.getCode(resp.id),
        phoneCode: async () => await input.text('code:'),
        firstName: 'Testname',
        lastName: 'Testlastname',
        onError: (err) => console.log(err),

    });

After entering code facing error


CastError: Found wrong type for lastName. expected string but received undefined.If you think this is a mistake please report it.
    at VirtualClass.assertType (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/tl/api.js:353:43)
    at VirtualClass.validate (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/tl/api.js:299:30)
    at VirtualClass.getBytes (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/tl/api.js:360:26)
    at new RequestState (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/network/RequestState.js:9:29)
    at MTProtoSender.send (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/network/MTProtoSender.js:197:23)
    at Object.invoke (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/client/users.js:27:44)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.signInUser (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/client/auth.js:150:35)
    at async _authFlow (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/client/auth.js:338:11)
    at async Object.start (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/client/auth.js:41:5)
TypeError: Cannot read properties of undefined (reading 'constructor')
    at serializeBytes (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/tl/generationHelpers.js:238:60)
    at argToBytes (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/tl/api.js:105:20)
    at VirtualClass.getBytes (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/tl/api.js:412:42)
    at new RequestState (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/network/RequestState.js:9:29)
    at MTProtoSender.send (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/network/MTProtoSender.js:197:23)
    at Object.invoke (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/client/users.js:27:44)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.signInUser (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/client/auth.js:150:35)
    at async _authFlow (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/client/auth.js:338:11)
    at async Object.start (/home/navalny/PROJ/node/telegram_autoreger/node_modules/telegram/client/auth.js:41:5)
CastError: Found wrong type for lastName. expected string but received undefined.If you think this is a mistake please report it.
trizau commented 2 years ago

I used firstAndLastNames to set the username like this

await client.start({
     phoneNumber: this.phone,
     password: async () => {
         // ...
     },
     phoneCode: () => {// if need verify code
         // ...
     },
     onError: (err: Error) => {
         // ...
     },
     firstAndLastNames: async () => {
         return ['firstName','lastName']
     }
});