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

2

10

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

Peroration answered 15/1, 2020 at 9:3 Comment(0)
S
10

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
        }
    }
}
Schuller answered 15/1, 2020 at 11:9 Comment(5)
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?Peroration
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 groupchatSchuller
I've added a small example @PerorationSchuller
can anyone help me in a similar question here: https://mcmap.net/q/82566/-telegram-bot-webhook-returns-user-identifier-id-as-null/10302693Brant
@Schuller 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)?Ragland
D
0

To expand on the other answer (and addressing questions raised in the comments):

Yes, the user_id is the same across bots. Two bots communicating with the same user will receive the same user_id. However, chat_ids are (of course) different.

Source: I couldn't find clear information on the web and simply created two test bots.

Defilade answered 7/3 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.