如何从 Telegram 中的 UserID 获取用户名? [英] How to obtain username from UserID in Telegram?

查看:647
本文介绍了如何从 Telegram 中的 UserID 获取用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python-Telegram-Bot API 来创建一个机器人.当人们启动机器人时,我会得到他们的用户名和用户名.
用户 ID 永远不会改变,但用户可以随时更改用户名.
所以我有用户名和用户 ID,我收到了另一个用户名 - 我想弄清楚该用户名是否属于现有列表中的某个人.
基本上需要从用户 ID 中找出用户名,或者我是否有任何其他方式可以使用它.

I am using Python-Telegram-Bot API to create a bot. I get people's userID and username when they start the bot.
The userID doesn't change ever but the username can be changed by the user any time.
So I have usernames and UserIDs, and I receive another username - I wish to figure out whether that username is of someone already in the existing list or not.
Basically need to find out username from userID or if there's any other way I'm open to it.

推荐答案

这就是你总是使用用户 ID 处理消息的原因

thats why you always process messages using the users' IDs

但是要回答您的问题,您可以直接使用 user 对象获取用户的用户名,例如 update.message.from_user.usernameupdate.effective_user.username或使用 bot.getchat(chat_id) 获取某个聊天的用户对象(可能是用户的私人聊天

but to answer your question, you can either get the user's username directly using the user object, for example update.message.from_user.username or update.effective_user.username or use bot.getchat(chat_id) to get an user object of a certain chat (which could be an user's private chat

这篇关于如何从 Telegram 中的 UserID 获取用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆