Running it

How to find a Telegram chat ID or user ID

By the Botable team

Every Telegram chat has a numeric ID: positive for private chats, negative for groups, and negative starting -100 for supergroups and channels. The simplest way to get one is a bot command that reports the ID of the chat it was sent in.

The short version

Telegram identifies every chat by a numeric id, and its sign tells you what kind of chat it is: private chats are positive, basic groups are negative, and supergroups and channels are negative beginning with -100. That last case surprises people, because a group's id changes when Telegram upgrades it to a supergroup — a stored id from before the upgrade stops working and the bot appears to have been removed from a chat it is still in. User ids are positive, permanent, and the only safe key for anything user-scoped: an @username can be changed by its owner at any moment and reused by someone else afterwards, so storing usernames as identifiers eventually attributes one person's data to another. The practical way to obtain any of these is a small command on your own bot that replies with the ids of the current chat and sender.

Step by step

How do you get the ID of a chat?

Add a command to your bot that replies with the chat id of wherever it was sent. Run it in the private chat, the group or the discussion group and read the number back. It works everywhere and needs no third-party bot holding your data.

How do you get a private channel's ID?

Forward any message from the channel into a chat with your bot, and have the bot report the forward's origin chat id. That is the reliable route, since a private channel has no username to address it by.

Why are some IDs negative?

The sign encodes the chat type. Negative means a group; the -100 prefix means a supergroup or channel. Storing these in a field that cannot hold negative numbers is a real and quietly common bug — every group row silently fails to match.

Why store the ID instead of the username?

Because usernames are mutable and reusable. A user who changes theirs breaks every record keyed on the old one, and if someone else claims that username your data now points at a stranger. The numeric id never changes.

What catches people out

  • A group's ID changes when it becomes a supergroup — old stored ids stop resolving
  • Group and channel ids are negative; unsigned columns break them invisibly
  • @usernames are mutable and reusable and must never be used as a primary key

Questions

Can a bot find a user's ID from their @username?

Not directly — there is no lookup endpoint for arbitrary usernames. The bot learns a user's id when that user interacts with it or appears in an update it receives.

Are user IDs the same across every bot?

Yes. A user's id is stable and identical for every bot that sees them, which is what makes cross-referencing your own records straightforward.

Want a bot that does this?

Describe it in plain language and Botable writes the code, gives the bot its own database, deploys it and keeps it running. Everything on this page is handled for you — the token, the webhook, the storage, the hosting.

Build a bot

Keep reading

Bots that use this

All twenty-five guides