简体   繁体   English

如何使用用户 ID 创建指向 Telegram 个人资料的链接 - Node.js

[英]How to create a link to Telegram profile with user ID - Node.js

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.我想知道是否有可能只使用用户 ID 来让 first_name 带有指向用户配置文件的链接,这样我就可以随时检查运行我的机器人的用户的配置文件。

Thank you in advance!先感谢您!

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.如果您遇到同样的问题,我希望这会对您有所帮助。

ctx.updateSubTypes is no longer supported after telegraf 4.0.3 . ctx.updateSubTypes后不再支持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查看 => https://github.com/telegraf/telegraf/releases/tag/v4.0.0#:~:text=Remove%20ctx.updateSubTypes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM