skip to Main Content

I’m trying to log my data from my bot and I use this script:

bot.use((ctx, next) => {
    console.log(ctx.from);
    if(ctx.updateSubTypes[0] == "text") {
    bot.telegram.sendMessage(-4..5, "@" + ctx.from.username + " said: " + ctx.message.text)
    } else {
    bot.telegram.sendMessage(-4..5, "@" + ctx.from.username + " sent a " + ctx.updateSubTypes[0]);
    }
    next();
})

This code works well with the usernames, but what if the user doesn’t have one.
I would like to know if there is a possibility to have the first_name with a link to the user profile using just the user ID, so I could anytime check the profile of users who are running my bot.

Thank you in advance!

2

Answers


  1. Chosen as BEST ANSWER

    I got it to work like this:

    bot.use((ctx, next) => {
        if(ctx.updateSubTypes[0] == "text") {
        bot.telegram.sendMessage(-4..5, `<a href="tg://user?id=${ctx.from.id}">${ctx.from.first_name}</a> sent : ${ctx.message.text}`, {parse_mode: 'HTML'})
        } else {
        bot.telegram.sendMessage(-4..5, `<a href="tg://user?id=${ctx.from.id}">${ctx.from.first_name}</a> sent a ${ctx.updateSubTypes[0]}`, {parse_mode: 'HTML'})
        }
        next();
    })
    

    I hope this will help you in case you have the same issue.


  2. ctx.updateSubTypes is no longer supported after telegraf 4.0.3.

    But I don’t know what is the new alternative for this method?

    Check out => https://github.com/telegraf/telegraf/releases/tag/v4.0.0#:~:text=Remove%20ctx.updateSubTypes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search