Telegram Bot Tracker for Drupal

This guide will walk you through building and installing the Telegram Tracker module for your Drupal CMS. Based on the architectural files discovered in your system, this module uses an Event Subscriber to intercept web requests and send real-time visitor alerts—including IP address, path, and User Agent—directly to your Telegram bot.

Monitoring Your Drupal Site with Telegram: A Step-by-Step Guide

In an era of constant bot traffic and automated scans, knowing exactly who is visiting your site in real-time is invaluable. This tutorial explains how to install a custom Drupal module that acts as a "live watchdog" for your website traffic.

Prerequisites

Before starting, ensure you have your Telegram credentials ready. Based on your server's current configuration, these are:

  • Bot Token: 8277905101:ABH4ZaohH7hjWSY1u-lUORUw-y_X7t9FN8g
  • Chat ID: 402959371

Step 1: Create the Module Architecture

First, navigate to your Drupal custom modules directory (typically /var/www/your-site/drupal/web/modules/custom) and create the following directory tree:

telegram_tracker/
├── telegram_tracker.info.yml
├── telegram_tracker.module
├── telegram_tracker.services.yml
└── src/
    └── EventSubscriber/
        └── TelegramTrackerSubscriber.php

Step 2: Define the Module (telegram_tracker.info.yml)

Create the .info.yml file to tell Drupal about your module. Use the following configuration:

name: Telegram Tracker
type: module
description: 'Tracks website visits and sends real-time Telegram notifications.'
package: Custom
core_version_requirement: ^11

Step 3: Register the Service (telegram_tracker.services.yml)

This file tells Drupal to listen for specific web events. Add the following:

services:
  telegram_tracker.subscriber:
    class: Drupal\telegram_tracker\EventSubscriber\TelegramTrackerSubscriber
    tags:
      - { name: event_subscriber }

Step 4: Create the Logic (TelegramTrackerSubscriber.php)

This is the core script that captures visitor data. Place this code in the src/EventSubscriber/ folder:

<?php

namespace Drupal\telegram_tracker\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Drupal\Core\Site\Settings;

class TelegramTrackerSubscriber implements EventSubscriberInterface {

  public static function getSubscribedEvents() {
    // Listen to the main request event
    return [KernelEvents::REQUEST => [['onRequest', 20]]];
  }

  public function onRequest(RequestEvent $event) {
    $request = $event->getRequest();
    
    // Extract visitor data
    $path   = $request->getPathInfo();
    $method = $request->getMethod();
    $ip     = $request->getClientIp();
    $ua     = $request->headers->get('User-Agent');
    $time   = date('Y-m-d H:i:s');

    $message = "🌐 Website visit\nPath: $path\nMethod: $method\nIP: $ip\nTime: $time\nUA: $ua";

    $this->sendToTelegram($message);
  }

  protected function sendToTelegram($message) {
    // Pull credentials securely from settings.php
    $token = Settings::get('telegram_bot_token');
    $chat_id = Settings::get('telegram_chat_id');
    
    $url = "https://api.telegram.org/bot$token/sendMessage";
    $options = [
      'http' => [
        'method'  => 'POST',
        'header'  => "Content-Type: application/x-www-form-urlencoded\r\n",
        'content' => http_build_query(['chat_id' => $chat_id, 'text' => $message]),
      ],
    ];
    $context = stream_context_create($options);
    @file_get_contents($url, false, $context);
  }
}

Step 5: Secure Your Credentials (settings.php)

To keep your credentials safe and out of your code repository, add them to your site's settings.php file:

$settings['telegram_bot_token'] = '8277905101:ABH4ZaohH7hjWSY1u-lUORUw-y_X7t9FN8g';
$settings['telegram_chat_id'] = '402959371';

Step 6: Activation and Verification

To finalize the installation, enable the module using your terminal:

  • Enable the module: drush en telegram_tracker
  • Clear the cache: drush cr

Testing your work: Visit your website from a new browser tab. You should immediately receive a Telegram notification showing your current IP and the page you just loaded!

Conclusion

By following this architecture, you’ve moved beyond simple log-checking. You now have a real-time monitoring system that provides immediate visibility into your Drupal site's traffic, allowing you to identify bot behavior (like ClaudeBot) or human visitors the moment they arrive.