yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.45k stars 1.53k forks source link

Getting firstName of quoted user. #920

Closed allenhe77 closed 2 years ago

allenhe77 commented 2 years ago

Question

I have a bot that parses the message when a user '@' another user, I was wondering if the library has a feature to get the firstName of the quoted user.

danielperez9430 commented 2 years ago
bot.on('message', (msg) => {
    console.dir(msg)
    getUserMention(msg)
})

const getUserMention = (msg) => {
    if (msg.entities && msg.entities.length > 0) {
        // Users with username @
        let mentions = msg.entities.filter(e => e.type === 'mention')
        // Users without username @
        let text_mentions =  msg.entities.filter(e => e.type === 'text_mention')

        if (mentions) {
            mentions.forEach(mention => {
                let mentionUsername = msg.text.substring(mention.offset, mention.offset + mention.length)
                // in the mentions of the users with alias the "user" object is not present. 
                // So the better way is use a userbot for resolve the alias or save in the database the alias and the user id
                // when the users talk with the bot on in the chart
                console.dir(mentionUsername)
            })
        }

        if(text_mentions) {
            text_mentions.forEach(mention => {
                // User data
                console.dir(text_mentions.user)
            })
        }
    }
}