Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example for event "NewMessage" #74

Closed
aotaotaot99 opened this issue May 7, 2021 · 2 comments
Closed

example for event "NewMessage" #74

aotaotaot99 opened this issue May 7, 2021 · 2 comments

Comments

@aotaotaot99
Copy link

aotaotaot99 commented May 7, 2021

hi, I cannot find any example to handle event update "NewMessage"
As I googled found this https://painor.gitbook.io/gramjs/getting-started/updates-events

this worked fine.

client.addEventHandler((update) => {
    console.log("Received new Update");
    console.log(update);
});

this one not working.
do you have any example working code for this ?

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}`
            });
        }
    }
}

Thank you.

@painor
Copy link
Member

painor commented May 7, 2021

hey, you can use them like so

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, new events.NewMessage({}));

@aotaotaot99
Copy link
Author

hey, you can use them like so

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, new events.NewMessage({}));

It's worked.

anyway it was hard to handle the update. so i will use telegram bot api get update instead and use some functions that cannot do with bot
eg. - get list of users in supergroup.
- invite users without send their link

Thank you so much for fast respond.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants