我们正在为 Telegram Bot 使用 python API,并且需要能够识别用户。
每个连接机器人的用户的 chat_id 是唯一的吗?
我们可以相信 chat_id 是一致的吗?例如,相同的 chat_id 会告诉我们这是同一个用户,并且每个与机器人连接的用户都会有一个在 session 之间保持一致的 chat_id?
谢谢
最佳答案
Is the chat_id unique for each user connecting the bot?
是的
对于连接到您的机器人的每个用户,
chat_id
将始终是唯一的。如果同一用户向不同的机器人发送消息,他们将始终使用其唯一的
id
来“识别”自己。请记住,
getUpdates
显示用户 ID 和聊天中的 ID。{
"ok": true,
"result": [
{
"update_id": 1234567,
"message": {
"message_id": 751,
"from": {
"id": 12122121, <-- user.id
"is_bot": false,
"first_name": "Me",
"last_name": "&",
"username": "&&&&",
"language_code": "en"
},
"chat": {
"id": -104235244275, <-- chat_id
"title": "Some group",
"type": "supergroup"
},
"date": 1579999999,
"text": "Hi!"
}
}
]
}
根据 this post ,即使该组转换为超组,该
chat.id
也不会改变基于评论;私有(private)/群聊示例的小概述
user_1 ---> bot_a in private chat
{
"message": {
"from": {
"id": 12345678 <-- id from user_1
},
"chat": {
"id": 12345678, <-- send from private chat, so chat is equals to user_id
}
}
}
user_2 ---> bot_a in private chat
{
"message": {
"from": {
"id": 9876543 <-- id from user_2
},
"chat": {
"id": 9876543, <-- send from private chat, so chat is equals to user_id
}
}
}
user_1 ---> bot_a in group chat
{
"message": {
"from": {
"id": 12345678 <-- id from user_1
},
"chat": {
"id": 5646464, <-- send from group chat, so id is from groupchat
}
}
}
user_2 ---> bot_a in group chat
{
"message": {
"from": {
"id": 9876543 <-- id from user_2
},
"chat": {
"id": 5646464, <-- send from group chat, so id is from groupchat
}
}
}
关于python - Telegram bot API 对于每个联系该机器人的用户来说,chat_id 是唯一的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59748008/