Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 322
thebeancounter Avatar asked Jan 15 '20 09:01

thebeancounter


1 Answers

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
        }
    }
}
like image 164
0stone0 Avatar answered Nov 11 '22 13:11

0stone0