Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

Telegram 400: Bad Request: chat not found — how to fix it

The bot asked Telegram for a chat it does not know. Either the chat_id is wrong (a typo, a channel id without its -100 prefix, a user's @username, an id cut to 32 bits), or it is right but this bot has never been in that chat: the user never pressed Start, the bot was never added to the group or channel, or the token belongs to a different bot.

By Livops team, the people who build and run LivopsUpdated

The error, as the Telegram Bot API returns it with error code 400[2]:

Bad Request: chat not found

Each library wraps it in its own words:

Library What you see
aiogram 3 TelegramBadRequest: Telegram server says - Bad Request: chat not found[6][7]
python-telegram-bot telegram.error.BadRequest: Chat not found[9][10]
grammY GrammyError: Call to 'sendMessage' failed! (400: Bad Request: chat not found)[11]
Telegraf TelegramError: 400: Bad Request: chat not found[12]

python-telegram-bot drops the Bad Request: prefix and capitalizes the rest[9], which is why its message looks different. This page explains what the error means, lists the causes from the most common, shows how to find the right chat_id for a user, a group and a channel, and what to check when the bot runs on Livops.

What does the error mean?

Every method that sends to a chat takes a chat_id: an integer id, or a username in the form @username for a bot, supergroup or channel[1]. Before it sends anything, the Bot API server looks the chat up for this bot. If the id is unknown to the bot, or the @username does not resolve to a chat that may be addressed by name, it answers Bad Request: chat not found[2].

So there are two families of cause: the chat_id is not the id you meant, or it is the right id and this bot has no access to that chat. A chat the server can find but the bot is not in gives a different, 403 error instead — Forbidden: bot was kicked from the channel chat, Forbidden: bot is not a member of the supergroup chat and so on[2] — covered in Forbidden: bot was blocked by the user.

Causes, most common first

1. The user never started the bot

Bots cannot start conversations with users: a user must add the bot to a group or send it a message first[4]. A user id you got from somewhere else — a form, a CRM, another bot, a group the user is in — is a real id, but the bot has no private chat with that person yet. Send them the bot’s link, https://t.me/<bot_username>[5], and store their chat id when they write. A related 403, bot can't initiate conversation with a user[8], means the same thing for a user the bot already knows.

2. A channel or supergroup id without -100

The Bot API turns a channel’s or supergroup’s internal id into -(1000000000000 + id), so every such id is negative and begins with -100; basic group ids are just negative; user ids are positive[3]. Tools and web clients that show the internal number give you 1234567890, while the bot must send to:

-1001234567890

Writing 1234567890 or -1234567890 points at another chat, or at none.

3. A @username where only an id works

@username is accepted for channels, supergroups and bots[1]. For a private chat with a person, the Bot API server allows a username only when the target is a bot, and answers chat not found otherwise[2]. Send to the user’s numeric id. A channel username also needs the @: @mychannel, not mychannel or t.me/mychannel.

4. The bot is not in the group or channel

A bot that was never added to a private group or channel does not know it, so its id gives chat not found. A public channel or supergroup can be found by its @username, and then the answer is a 403 instead: Forbidden: bot is not a member of the channel chat; a bot that was removed gets Forbidden: bot was kicked from the channel chat[2]. Either way, add the bot; for a channel, make it an administrator that can post messages[1].

5. A different bot’s token

Chats are per bot. User A started @shop_bot, and the code sends with the token of @shop_test_bot: the id is A’s real id, but the test bot has never talked to A. This is common when staging and production use different bots, or after the token in the environment was swapped. Check which bot the token belongs to:

curl -s "https://api.telegram.org/bot$BOT_TOKEN/getMe"

getMe returns the bot’s username; it must be the bot the user started.

6. The id was cut or mangled

Chat ids may have more than 32 significant bits but at most 52, so a signed 64-bit integer or a double is safe for them[1]. A 32-bit INT column or a 32-bit integer type silently breaks a large id. In SQL, use BIGINT. And print the value you actually send, quotes included: a CHAT_ID set with quotes or a trailing space in the environment is not the number you think it is.

7. A group became a supergroup

When a basic group is upgraded, it gets a new id; the old one fails with a different error, Bad Request: group chat was upgraded to a supergroup chat, and the response’s parameters.migrate_to_chat_id carries the new id[2][1]. Save that id and use it from then on.

How to find the right chat_id

A user. Have them press Start or write to the bot, then read message.chat.id from the update. Store it when it arrives — in your /start handler — rather than looking it up later. Telegram also sends a my_chat_member update in a private chat when the user blocks or unblocks the bot[1].

A group. Add the bot, send a message in the group, and read message.chat.id. It is negative.

A channel. Add the bot as an administrator. If the channel is public, ask for its id by username — getChat accepts @username for a supergroup or channel[1]:

curl -s "https://api.telegram.org/bot$BOT_TOKEN/getChat?chat_id=@mychannel"

The result.id starts with -100. For a private channel, post something in it and read channel_post.chat.id from the update[1].

Reading updates by hand with getUpdates works only while nothing else receives them: it fails while a webhook is set, and it conflicts with a running bot that polls with the same token — see Conflict: terminated by other getUpdates request. The simplest way on a running bot is to log chat.id in a handler.

How to confirm the fix

Send one message with the exact id and token the app uses:

curl -s "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
  --data-urlencode "chat_id=$CHAT_ID" \
  --data-urlencode "text=test"

"ok":true means the pair works and the problem is in how the code builds the id. chat not found again means the id or the bot is wrong: run getMe and the lookup above, and compare.

On Livops

  • The id and the token are variables. Put BOT_TOKEN and, for a fixed target such as an admin chat or a channel, CHAT_ID under Variables, without quotes. New values take effect on the next deploy; restart the service to apply them now. Mark the token as a secret, and its value is never shown again, only its name.
  • Staging copies the variables. A new environment such as staging gets copies of production’s services and variables. Give it its own test bot’s token, or the two copies poll with one token and conflict. Once it has a test bot, its users and chats are that bot’s, and a chat id from production gives chat not found there.
  • A worker can use the bot’s variables. A worker’s variable can take its value from another service’s variable by reference, so a sender in the same project uses the same token without a copy — see Variables.
  • Chat ids in PostgreSQL. Add PostgreSQL under Databases, and DATABASE_URL is written into every service in the environment. Store chat ids as BIGINT. A file next to the code is gone after the next deploy.
  • Logs. Log the chat_id and the bot’s username next to each failed send; the project’s Logs page and livops logs <service> -f show them. The assistant sees a failed deploy’s log and variable names, never variable values, so it can point at a missing CHAT_ID but not at a wrong one.

A bot, worker or cron job costs 19 credits a month (19,000 so’m); see Pricing.

Questions

Can a bot send the first message to a user?

No. Bots cannot start conversations: a user must write to the bot, press Start, or add it to a group first. Until then, sending to that user's id fails. Give users the bot's t.me link and store their chat id when they write.

Why does my channel id need -100 in front?

The Bot API numbers channels and supergroups as minus one trillion minus the channel's internal id, so every channel id starts with -100. An id copied without that prefix, such as 1234567890, points at a different chat or none.

Can I send a message to a user by their @username?

No. The @username form works for channels, supergroups and other bots. For a person you need their numeric chat id, which the bot gets from their first message or their Start.

Is my chat_id too big for an integer?

It can be too big for a 32-bit one. Chat ids can have more than 32 significant bits but at most 52, so store them as a 64-bit integer, BIGINT in SQL. A 32-bit column cuts the id and the bot then sends to a chat that does not exist.

It worked yesterday and now says chat not found. What changed?

Usually the bot or the chat. A token of a different bot, for example a test bot in staging, reaches only that bot's chats. A bot removed from a group or channel loses it. A group upgraded to a supergroup gets a new id, and the old one returns its own error with the new id in migrate_to_chat_id.

How do I find the chat id of a channel?

Add the bot to the channel as an administrator, then call getChat with chat_id set to @yourchannel if it is public, or post in the channel and read chat.id from the channel_post update. The id starts with -100.

Sources

  1. Telegram Bot API — sendMessage, getChat, getUpdates, Chat, Update, ResponseParameters (core.telegram.org). Checked .
  2. Telegram Bot API server source — telegram-bot-api/Client.cpp (raw.githubusercontent.com). Checked .
  3. Telegram — Bot API dialog IDs (core.telegram.org). Checked .
  4. Telegram — Bots, an introduction for developers (core.telegram.org). Checked .
  5. Telegram — Bot features (deep linking) (core.telegram.org). Checked .
  6. aiogram source — aiogram/client/session/base.py (raw.githubusercontent.com). Checked .
  7. aiogram source — aiogram/exceptions.py (raw.githubusercontent.com). Checked .
  8. aiogram 2.x source — aiogram/utils/exceptions.py (raw.githubusercontent.com). Checked .
  9. python-telegram-bot source — telegram/error.py (raw.githubusercontent.com). Checked .
  10. python-telegram-bot source — telegram/request/_baserequest.py (raw.githubusercontent.com). Checked .
  11. grammY source — src/core/error.ts (raw.githubusercontent.com). Checked .
  12. Telegraf source — src/core/network/error.ts (raw.githubusercontent.com). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your bot

Got a question? — @livops_uz