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

Telegram 403: Forbidden: bot was blocked by the user — how to handle it

The user blocked the bot, and Telegram refuses every message the bot sends them. Nothing is broken and a retry will not help: catch the 403, mark that user as inactive in your database, skip them in broadcasts, and mark them active again when they write to the bot.

By Livops team, the people who build and run LivopsUpdated

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

Forbidden: bot was blocked by the user

Each library wraps it in its own words:

Library What you see
aiogram 3 TelegramForbiddenError: Telegram server says - Forbidden: bot was blocked by the user[5][6]
python-telegram-bot telegram.error.Forbidden: Forbidden: bot was blocked by the user[10]
grammY GrammyError: Call to 'sendMessage' failed! (403: Forbidden: bot was blocked by the user)[12]
Telegraf TelegramError: 403: Forbidden: bot was blocked by the user[13]

This page explains what the error means, why it shows up in bulk during broadcasts, how to catch it in each of the four libraries, and how to keep a list of active users so the bot stops writing to people who left.

What does the error mean?

The user blocked the bot. Telegram turns its internal USER_IS_BLOCKED into this 403 for every message the bot tries to send that user[2]. It is not a bug in your code, not a token problem and not a rate limit; the message was simply not delivered, and the next one will not be either, until the user unblocks the bot.

Bots cannot start conversations: a user must write to the bot or add it to a group first[3]. So the bot can only ever reach people who chose to talk to it, and blocking is how they take that back.

The same situation has a few relatives, all 403s, all meaning “stop sending here”[2][7]:

Description Meaning
Forbidden: bot was blocked by the user The user blocked the bot
Forbidden: user is deactivated The user deleted their account
Forbidden: bot was kicked from the group chat (or supergroup chat, channel chat) The bot was removed from that chat
Forbidden: bot is not a member of the channel chat (or supergroup chat, group chat) The bot is not in that chat
bot can't initiate conversation with a user The user never started a chat with the bot

Telegram notes that error_code may change in the future[1], so decide on the HTTP status your library already mapped (403) together with the description, not on the number alone.

Why it appears in broadcasts

In normal use a blocked user stops sending messages, so the bot has nothing to reply to and never sees the error. A broadcast — a newsletter, a reminder, an announcement to everyone in the database — is where the bot writes first, and every user who blocked it since they last spoke returns a 403. In a large audience that can be a sizeable share of the list, and the share grows with every broadcast if nothing is ever marked.

The python-telegram-bot wiki puts it plainly: even with every user’s ID, you cannot know whether someone has blocked the bot in the meantime, so wrap each send in a try/except for Forbidden[11].

Catch it in each library

The pattern is the same everywhere: try to send; on 403, mark the chat inactive and move on; on anything else, raise. Use your own mark_inactive(), for example an UPDATE in your database.

aiogram 3

A 403 from Telegram becomes TelegramForbiddenError[4][5]; its message holds Telegram’s description[6].

from aiogram import Bot
from aiogram.exceptions import TelegramForbiddenError


async def send(bot: Bot, chat_id: int, text: str) -> bool:
    try:
        await bot.send_message(chat_id, text)
        return True
    except TelegramForbiddenError as e:
        await mark_inactive(chat_id, reason=e.message)
        return False

python-telegram-bot

A 403 becomes telegram.error.Forbidden[9][10], which before version 20.0 was called Unauthorized[9].

from telegram.error import Forbidden


async def send(bot, chat_id: int, text: str) -> bool:
    try:
        await bot.send_message(chat_id, text)
        return True
    except Forbidden as e:
        await mark_inactive(chat_id, reason=e.message)
        return False

grammY

Every Bot API error is a GrammyError carrying error_code and description[12]:

import { GrammyError, type Bot } from "grammy";

async function send(bot: Bot, chatId: number, text: string): Promise<boolean> {
  try {
    await bot.api.sendMessage(chatId, text);
    return true;
  } catch (err) {
    if (err instanceof GrammyError && err.error_code === 403) {
      await markInactive(chatId, err.description);
      return false;
    }
    throw err;
  }
}

Telegraf

TelegramError exposes the error code as code and Telegram’s text as description[13]:

const { TelegramError } = require('telegraf');

async function send(telegram, chatId, text) {
  try {
    await telegram.sendMessage(chatId, text);
    return true;
  } catch (err) {
    if (err instanceof TelegramError && err.code === 403) {
      await markInactive(chatId, err.description);
      return false;
    }
    throw err;
  }
}

aiogram 2 (older code)

aiogram 2 raised BotBlocked for this description, alongside UserDeactivated, BotKicked and CantInitiateConversation, all subclasses of Unauthorized[7]. Catch Unauthorized to handle them together.

Mark users inactive, and active again

Do not wait for a broadcast to discover who blocked the bot. Telegram sends a my_chat_member update when the bot’s status in a chat changes, and in a private chat it comes only when the user blocks or unblocks the bot[1]. The python-telegram-bot wiki calls my_chat_member the most reliable way to keep track of your users[11].

In aiogram, filter the transition from member to not-member[8]:

from aiogram import F, Router
from aiogram.filters import IS_MEMBER, IS_NOT_MEMBER, ChatMemberUpdatedFilter
from aiogram.types import ChatMemberUpdated

router = Router()


@router.my_chat_member(F.chat.type == "private", ChatMemberUpdatedFilter(IS_MEMBER >> IS_NOT_MEMBER))
async def on_blocked(event: ChatMemberUpdated) -> None:
    await mark_inactive(event.chat.id, reason="blocked")


@router.my_chat_member(F.chat.type == "private", ChatMemberUpdatedFilter(IS_NOT_MEMBER >> IS_MEMBER))
async def on_unblocked(event: ChatMemberUpdated) -> None:
    await mark_active(event.chat.id)

Two details make this work:

  • Receive the update. my_chat_member is in the default set of updates. If your bot passes its own allowed_updates list to getUpdates or setWebhook, include "my_chat_member" in it. A list you set once stays in force: when the parameter is not specified, the previous setting is used[1].

  • Keep the flag in a database, not in memory, so a restart does not forget it. A column is enough:

    ALTER TABLE users ADD COLUMN blocked_at timestamptz;
    -- on 403 or a my_chat_member block:
    UPDATE users SET blocked_at = now() WHERE chat_id = $1;
    -- on unblock, or any new message from the user:
    UPDATE users SET blocked_at = NULL WHERE chat_id = $1;
    -- the broadcast list:
    SELECT chat_id FROM users WHERE blocked_at IS NULL;

To message everyone you need their chat ids stored somewhere you can read them slowly[14]; the same table carries the flag.

Do not retry

A 403 is final for that user until they act. Retrying it, with or without a delay, sends the same request and gets the same answer. Keep retries for the errors that ask for them: 429 Too Many Requests means wait retry_after seconds and send again, while 403 means skip. A broadcast loop handles both — see Telegram 429: Too Many Requests for the waiting part.

On Livops

Livops does not filter your sends or retry them; the handling above lives in your code. What Livops gives you is the place to run it:

  • The bot as a Bot. A bot runs as the bot type: always on, no public URL needed. Its exceptions and your mark_inactive log lines are on the project’s Logs page and in the terminal with livops logs <service> -f.

  • The users table in PostgreSQL. Add PostgreSQL under Databases, and DATABASE_URL is written into every service in the environment, including ones added later. Keep the blocked_at flag there; a file next to the code is gone after the next deploy.

  • The broadcast as a worker. Run the sender as a worker service in the same project: an always-on background process with no public URL, with the same database variables. It must only send and never call getUpdates, or it conflicts with the bot — see Conflict: terminated by other getUpdates request.

    {
      "kind": "worker",
      "start": ".venv/bin/python sender.py"
    }
  • A broadcast at a fixed time. A cron service can queue tomorrow’s broadcast on a schedule read in UTC: 0 4 * * * is 09:00 in Tashkent. The syntax is in Cron and workers.

A bot, worker or cron job costs 19 credits a month (19,000 so’m). One 500 MB database per team is free; see Pricing.

Questions

Can I unblock myself or make the user unblock the bot?

No. Only the user can unblock the bot, from the chat with it. Until they do, every message to them returns this 403. Your bot is told through a my_chat_member update when they block it, and again when they unblock.

Should I retry the message later?

No. The error does not go away with time, and retries only add load and log noise. Mark the user as inactive and skip them. When they unblock the bot and write to it again, mark them active.

Does this error crash my bot?

Only if the exception escapes your code. Give your library an error handler for exceptions raised in handlers, and wrap each send of a broadcast in a try block. Without one, a single blocked user can stop the loop for everyone after them.

How do I know how many users blocked my bot?

Count them yourself. Record a my_chat_member update in a private chat, or a 403 on send, as blocked, with the time, and count the rows where that time is set.

Is user is deactivated the same thing?

Almost. Forbidden: user is deactivated means the account was deleted. Handle it the same way, but that user will not come back, so you can delete them instead of marking them inactive.

Is a blocked user a problem for my bot's rate limits?

No, the 403 is not flood control. A 429 Too Many Requests is a different error that asks you to wait and send again. A broadcast needs both: wait on 429, skip on 403.

Sources

  1. Telegram Bot API — Making requests, Update, getUpdates (core.telegram.org). Checked .
  2. Telegram Bot API server source — telegram-bot-api/Client.cpp (raw.githubusercontent.com). Checked .
  3. Telegram — Bots, an introduction for developers (core.telegram.org). Checked .
  4. aiogram — Errors (docs.aiogram.dev). Checked .
  5. aiogram source — aiogram/client/session/base.py (raw.githubusercontent.com). Checked .
  6. aiogram source — aiogram/exceptions.py (raw.githubusercontent.com). Checked .
  7. aiogram 2.x source — aiogram/utils/exceptions.py (raw.githubusercontent.com). Checked .
  8. aiogram — ChatMemberUpdated filter (docs.aiogram.dev). Checked .
  9. python-telegram-bot — telegram.error (docs.python-telegram-bot.org). Checked .
  10. python-telegram-bot source — telegram/request/_baserequest.py (raw.githubusercontent.com). Checked .
  11. python-telegram-bot wiki — Frequently requested design patterns (github.com). Checked .
  12. grammY source — src/core/error.ts (raw.githubusercontent.com). Checked .
  13. Telegraf source — src/core/network/error.ts (raw.githubusercontent.com). Checked .
  14. grammY — Scaling up IV, flood limits (grammy.dev). 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