Skip to content

banChatSenderChat

Returns: TrueOfficial docs ↗

Use this method to ban a channel chat in a supergroup or a channel. Until the chat is unbanned, the owner of the banned chat won't be able to send messages on behalf of any of their channels. The bot must be an administrator in the supergroup or channel for this to work and must have the appropriate administrator rights. Returns True on success.

Parameters

chat_idIntegerStringRequired
Unique identifier for the target chat or username of the target channel (in the format @channelusername)
sender_chat_idIntegerRequired
Unique identifier of the target sender chat

Returns

On success, True is returned.

GramIO Usage

typescript
import { Bot } from "gramio";

const bot = new Bot(process.env.BOT_TOKEN as string);

// Ban a sender chat when a channel posts in your supergroup
bot.on("message", async (ctx) => {
  const senderChat = ctx.message.sender_chat;
  if (!senderChat) return; // Message was sent by a regular user, not a channel

  // Ban the channel from posting
  await bot.api.banChatSenderChat({
    chat_id: ctx.chat.id,
    sender_chat_id: senderChat.id,
  });

  await ctx.send(`Channel @${senderChat.username ?? senderChat.id} has been banned.`);
});

// Command to ban a specific sender chat by replying to its message
bot.command("banchannel", async (ctx) => {
  const senderChatId = ctx.message.reply_to_message?.sender_chat?.id;
  if (!senderChatId) {
    return ctx.reply("Reply to a channel message to ban that channel.");
  }

  await bot.api.banChatSenderChat({
    chat_id: ctx.chat.id,
    sender_chat_id: senderChatId,
  });

  await ctx.reply("The channel has been banned from this chat.");
});

// Ban a known channel from a supergroup by ID
async function banChannelFromSupergroup(
  bot: Bot,
  supergroupId: number,
  channelId: number
): Promise<void> {
  await bot.api.banChatSenderChat({
    chat_id: supergroupId,
    sender_chat_id: channelId,
  });
}

Errors

CodeErrorCause
400Bad Request: chat not foundchat_id is invalid or the bot is not a member of the target supergroup/channel
400Bad Request: PEER_ID_INVALIDsender_chat_id does not refer to a valid channel or chat — verify the ID is a channel, not a user
400Bad Request: can't remove chat ownerAttempting to ban the chat that owns the supergroup — a chat cannot ban itself
400Bad Request: method is available for supergroup and channel chats onlychat_id refers to a basic group — this method only works for supergroups and channels
403Forbidden: bot is not an administratorThe bot is not an admin in the target supergroup/channel — promote the bot first
403Forbidden: not enough rights to restrict/ban chat memberThe bot is an admin but lacks can_restrict_members — grant it in admin settings
429Too Many Requests: retry after NRate limit hit — use the auto-retry plugin

Tips & Gotchas

  • This bans the channel, not an individual user. However, the ban affects the channel owner's ability to send messages as any of their channels in that chat. The owner can still post as themselves (their personal account) unless separately banned with banChatMember.
  • Only works in supergroups and channels. Basic groups do not support sender chat bans. If your bot manages basic groups, check the chat type before calling this method.
  • sender_chat_id comes from message.sender_chat. When a channel or anonymous group admin sends a message in a supergroup, Telegram populates message.sender_chat with the channel's info. Use this field to get the ID for banning.
  • The ban is permanent until lifted. Unlike banChatMember, there is no until_date parameter. Use unbanChatSenderChat to lift the ban.
  • To ban the channel's human owner too, combine with banChatMember. If you want to completely block both the channel and the owner's personal account, call both methods.
  • Check message.is_automatic_forward for linked channels. Posts automatically forwarded from a linked channel arrive with is_automatic_forward: true and sender_chat set. Don't accidentally ban your own linked channel.

See Also