gram-js / gramjs

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

TypeError: builder.resolve is not a function #423

Closed danielemaddaluno closed 1 year ago

danielemaddaluno commented 1 year ago

I was trying to listen for a telegram channel with this code:

const { TelegramClient } = require("telegram");
const { StringSession } = require("telegram/sessions");
const input = require("input"); // npm i input

const apiId = getProp("telegram", "apiId")
const apiHash = getProp("telegram", "apiHash")
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("Please enter your number: "),
        password: async () => await input.text("Please enter your password: "),
        phoneCode: async () =>
            await input.text("Please enter the code you received: "),
        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

    async function eventPrint(event) {
        const message = event.message;

        // Checks if it's a private message (from user or bot)
        if (event.isPrivate){
            // prints sender id
            console.log(message.senderId);
            // read message
            if (message.text == "hello"){
                const sender = await message.getSender();
                console.log("sender is",sender);
                await client.sendMessage(sender,{
                    message:`hi your id is ${message.senderId}`
                });
            }
        }
    }
    client.addEventHandler(eventPrint, {});
})();

As soon as I get an update the app crashes with this error:

/Users/madx/Documents/chatlistener/node_modules/telegram/client/updates.js:110
            await builder.resolve(client);
                          ^

TypeError: builder.resolve is not a function
    at _dispatchUpdate (/Users/madx/Documents/chatlistener/node_modules/telegram/client/updates.js:110:27)
    at _processUpdate (/Users/madx/Documents/chatlistener/node_modules/telegram/client/updates.js:100:5)
    at TelegramClient._handleUpdate (/Users/madx/Documents/chatlistener/node_modules/telegram/client/updates.js:89:9)
    at MTProtoSender._handleUpdate (/Users/madx/Documents/chatlistener/node_modules/telegram/network/MTProtoSender.js:575:18)
    at MTProtoSender._processMessage (/Users/madx/Documents/chatlistener/node_modules/telegram/network/MTProtoSender.js:456:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async MTProtoSender._recvLoop (/Users/madx/Documents/chatlistener/node_modules/telegram/network/MTProtoSender.js:432:17)

Process finished with exit code 1

Any advice? Thank you in advance

painor commented 1 year ago

that's not how you add an event. not sure where you got that code from.

client.addEventHandler(eventPrint, {});client.addEventHandler(eventPrint, new NewMessage({}));

danielemaddaluno commented 1 year ago

Yeah it was that. Thank you very much @painor !