Skip to content

Anti Link

KourvA edited this page Aug 28, 2024 · 6 revisions

What is anti link feature in bot?

The "anti-link" feature in a Telegram bot serves to manage large groups (supergroups) by automatically handling incoming messages containing links. Given the challenge administrators face in overseeing numerous messages, especially with various inappropriate links such as those promoting explicit content, advertisements, or potentially malicious links, this feature aims to address these concerns.

# Regular expression for Anti-Link
LINK_REGIX: str = r"(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?"

The bot employs a regular expression to detect and subsequently delete incoming messages that contain links. The regular expression, defined as LINK_REGIX, scans messages to identify links in the following formats:

  • Links with http:// or https://
  • Links with or without www
  • Valid domain extensions (e.g., .com, .org) followed by potential paths

By implementing this regular expression within the message handler, the bot can identify messages containing any variation of these links. The intention is to filter out these links, excluding administrators and VIP members from the link deletion process.

Handler path: source_path/Handlers/3-AntiLink/anti-link

# Anti-Link handler using regular expression
# ─────────────────────────────────────────────────────────────
@bot.message_handler(regexp=setting.LINK_REGIX)
def handle_message_with_url(message: ClassVar[Any]) -> NoReturn:
    # Additional code to process or delete messages containing URLs can be added here

Previous page: About bot | Next page: Database

Clone this wiki locally