6

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

thebeancounter
  • 4,261
  • 8
  • 61
  • 109

1 Answers1

7

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
        }
    }
}
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • 1
    So if I have two bots, and the same user will talk to both of them, He/She will have two chat_id's or one? – thebeancounter Jan 15 '20 at 11:29
  • 3
    Both the `chat_id` and `user_id` will be the same since you're sending the chat from the same user, and so the same (private) chat. If the first user would send a private message to the bot, and the second user will target the bot though a groupchat, then the (from) `user_id` will be the same. Except this time the (from) `chat_id` will be different, one id of the private chat, and the other id from the groupchat – 0stone0 Jan 15 '20 at 11:50
  • 1
    I've added a small example @thebeancounter – 0stone0 Jan 15 '20 at 12:02
  • can anyone help me in a similar question here: https://stackoverflow.com/q/68815736/10302693 – Dinesh Suthar Aug 19 '21 at 05:14
  • @0stone0 what if it's two different bots? will `from.id` be the same for the same user account in all groups and bot-direct messages (for bot_a and bot_b)? – Mihail Malostanidis Feb 18 '22 at 17:05