Telegram bot API is the chat_id unique for each user contacting the bot?

回眸只為那壹抹淺笑 提交于 2020-05-29 07:43:52

问题


We are using python API for telegram bots and need to be able to identify the user.

Is the chat_id unique for each user connecting the bot?

Can we trust the chat_id to be consistent? e.g same chat_id will tell us that this is the same user, and each user connecting with the bot will have one chat_id that is consistent between sessions?

Thanks


回答1:


Is the chat_id unique for each user connecting the bot?

Yes

chat_id will always be unique for each user connecting to your bot. If the same user sends messages to different bots, they will always 'identify' themselves with their unique id.

Keep in mind that getUpdates shows the users id, and the id from the chat.

{
    "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!"
            }
        }
    ]
}

According to this post, that chat.id will not change, even if the group is converted to a supergroup

Based on comment; small overvieuw of private/group chat example

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
        }
    }
}


来源:https://stackoverflow.com/questions/59748008/telegram-bot-api-is-the-chat-id-unique-for-each-user-contacting-the-bot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!