Search by

ozkanozcan / laravel-telegram

ozkanozcan

Laravel 11-13 Telegram Bot notification channel package

Package info

github.com/ozkanozcan/laravel-telegram

pkg:composer/ozkanozcan/laravel-telegram

Statistics

Installs: 17

Dependents: 0

Suggesters: 0

Stars: 3

Open Issues: 0

v1.1.0 2026-12-02 20:27 UTC

This package is auto-updated.

Last update: 2026-12-02 20:28:02 UTC


README

Tests Latest Version on Packagist PHP Version Laravel Version License: MIT

A clean, zero-dependency Telegram Bot notification channel for Laravel 11, 12, and 13.
Send messages, photos, and documents directly from your Laravel application via the Telegram Bot API, and receive updates through a verified webhook.

A community package, not an official Laravel package.

Table of Contents

Requirements

Dependency Version
PHP ^8.2
Laravel 11.x / 12.x / 13.x

Installation

Install the package via Composer:

composer require ozkanozcan/laravel-telegram

Laravel's auto-discovery will register the service provider and Telegram facade automatically.

Publish the configuration file:

php artisan vendor:publish --tag=telegram-config

Publish language files (optional — required only to customise messages):

php artisan vendor:publish --tag=telegram-lang

Step 1 — Create a Telegram Bot

  1. Open Telegram and search for @BotFather.
  2. Start a conversation and send the command:
    /newbot
    
  3. Follow the prompts:
    • Choose a display name for your bot (e.g. My Laravel App).
    • Choose a username — must end with bot (e.g. mylaravelapp_bot).
  4. BotFather will respond with your Bot Token:
    Done! Congratulations on your new bot.
    Use this token to access the HTTP API:
    1234567890:AAFxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    
  5. Copy the token and add it to your .env file:
    TELEGRAM_BOT_TOKEN=1234567890:AAFxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Keep your token secret. Anyone with this token can control your bot.

Step 2 — Get Your Chat ID

The chat ID is the unique identifier of the conversation your bot should send messages to.
It can be a personal chat, a group, or a channel.

Personal Chat ID

  1. Search for @userinfobot on Telegram.
  2. Start a conversation and send /start.
  3. The bot will reply with your user information including your Id field — that is your chat_id.

Alternatively:

  1. Send any message to your own bot (@yourbotname).
  2. Open this URL in your browser (replace YOUR_TOKEN):
    https://api.telegram.org/botYOUR_TOKEN/getUpdates
    
  3. Find the "chat" object in the JSON response:
    "chat": {
        "id": 123456789,
        "first_name": "John",
        "type": "private"
    }
  4. The "id" value is your chat_id.

Group Chat ID

  1. Add your bot to the group.
  2. Send a message in the group (mention the bot or just type anything).
  3. Open https://api.telegram.org/botYOUR_TOKEN/getUpdates.
  4. Look for "chat": {"id": -100xxxxxxxxxx} — group IDs start with -100.

Channel ID

For public channels use the channel username: @yourchannel
For private channels:

  1. Forward any message from your channel to @userinfobot.
  2. It will show the channel ID (e.g. -1001234567890).

Add the chat ID to your .env:

TELEGRAM_CHAT_ID=123456789

Step 3 — Configure the Package

Add these variables to your .env file:

TELEGRAM_BOT_TOKEN=1234567890:AAFxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TELEGRAM_CHAT_ID=123456789
TELEGRAM_PARSE_MODE=HTML
# Only needed to receive updates, see "Webhooks"
TELEGRAM_WEBHOOK_SECRET=

Verify your setup with the built-in Artisan command:

php artisan telegram:test

Basic Usage

Send a simple message

use OzkanOzcan\LaravelTelegram\TelegramBot;
use OzkanOzcan\LaravelTelegram\TelegramMessage;

$bot = app(TelegramBot::class);

$bot->sendMessage(
    config('telegram.chat_id'),
    TelegramMessage::create('Hello from Laravel! 🚀')->html()
);

Send a plain string

$bot->sendMessage(config('telegram.chat_id'), 'Hello World!');

Laravel Notifications

This is the recommended way to use the package in Laravel applications.

1. Add routing to your notifiable model

// app/Models/User.php

use OzkanOzcan\LaravelTelegram\TelegramChannel;
use OzkanOzcan\LaravelTelegram\TelegramMessage;

class User extends Authenticatable
{
    public function routeNotificationForTelegram(): string|int|null
    {
        // Return the user's personal Telegram chat_id stored in the database,
        // or fall back to the application-wide default.
        return $this->telegram_chat_id ?? config('telegram.chat_id');
    }
}

2. Create a notification class

php artisan make:notification OrderShipped
// app/Notifications/OrderShipped.php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use OzkanOzcan\LaravelTelegram\TelegramChannel;
use OzkanOzcan\LaravelTelegram\TelegramMessage;

class OrderShipped extends Notification
{
    public function __construct(private readonly Order $order) {}

    public function via(object $notifiable): array
    {
        return [TelegramChannel::class];
    }

    public function toTelegram(object $notifiable): TelegramMessage
    {
        return TelegramMessage::create()
            ->html()
            ->content(
                "📦 <b>Order Shipped!</b>\n\n" .
                "Order: <code>#{$this->order->id}</code>\n" .
                "Customer: {$this->order->customer_name}\n" .
                "Total: <b>\${$this->order->total}</b>"
            )
            ->button('Track Order', route('orders.track', $this->order))
            ->disablePreview();
    }
}

3. Dispatch the notification

// Send to a specific user
$user->notify(new OrderShipped($order));

// Send to multiple users
Notification::send(User::all(), new OrderShipped($order));

Facade Usage

The Telegram facade is available after auto-discovery:

use OzkanOzcan\LaravelTelegram\TelegramFacade as Telegram;
use OzkanOzcan\LaravelTelegram\TelegramMessage;

// Send a message
Telegram::sendMessage(
    config('telegram.chat_id'),
    TelegramMessage::create('🔔 New user registered!')->html()
);

// Send a photo
Telegram::sendPhoto(
    config('telegram.chat_id'),
    'https://example.com/photo.jpg',
    ['caption' => 'Check out this photo!']
);

// Send a document
Telegram::sendDocument(
    config('telegram.chat_id'),
    'https://example.com/report.pdf',
    ['caption' => 'Monthly Report']
);

// Raw API call
Telegram::request('sendMessage', [
    'chat_id' => config('telegram.chat_id'),
    'text'    => 'Hello!',
]);

// Check bot info
$me = Telegram::getMe();
echo $me['username']; // yourbot

// Webhooks (see "Webhooks")
Telegram::setWebhook('https://example.com/api/telegram/webhook'); // uses TELEGRAM_WEBHOOK_SECRET
Telegram::setWebhook('https://example.com/hook', 'my_secret', ['message', 'callback_query'], 40, true);
$info = Telegram::getWebhookInfo();   // array: url, pending_update_count, last_error_message, ...
Telegram::deleteWebhook(dropPendingUpdates: true);

// Answer an inline button press (stops the spinner on the button)
Telegram::answerCallbackQuery($callbackQueryId, 'Done', showAlert: false);

TelegramMessage Reference

use OzkanOzcan\LaravelTelegram\TelegramMessage;

$message = TelegramMessage::create('Your text here')

    // ── Content ─────────────────────────────────────
    ->text('Override text')           // Set message text
    ->content('Alias for text()')     // Alias

    // ── Parse mode ──────────────────────────────────
    ->html()                          // HTML parse mode
    ->markdown()                      // Markdown parse mode
    ->markdownV2()                    // MarkdownV2 parse mode
    ->parseMode('HTML')               // Custom parse mode string

    // ── Behaviour ───────────────────────────────────
    ->disablePreview()                // Disable link preview
    ->silent()                        // Send without notification sound

    // ── Recipient override ───────────────────────────
    ->to(123456789)                   // Override chat_id for this message

    // ── Inline keyboard ──────────────────────────────
    ->button('Label', 'https://...')  // URL button (new row)
    ->callbackButton('Label', 'data') // Callback data button (new row)
    ->buttonRow([                     // Multiple buttons in one row
        ['text' => 'A', 'url' => 'https://a.com'],
        ['text' => 'B', 'url' => 'https://b.com'],
    ]);

Inline Keyboard Buttons

TelegramMessage::create('Choose an option:')
    ->html()
    ->button('📖 Documentation', 'https://laravel.com/docs')
    ->button('🐙 GitHub', 'https://github.com')
    ->buttonRow([
        ['text' => '✅ Accept', 'callback_data' => 'accept'],
        ['text' => '❌ Decline', 'callback_data' => 'decline'],
    ]);

Message Formatting

HTML (recommended)

TelegramMessage::create(
    "<b>Bold</b>, <i>Italic</i>, <u>Underline</u>\n" .
    "<code>Inline code</code>\n" .
    "<pre>Block code</pre>\n" .
    '<a href="https://example.com">Link text</a>'
)->html();

Supported HTML tags: <b>, <i>, <u>, <s>, <code>, <pre>, <a href>, <tg-spoiler>

Markdown

TelegramMessage::create(
    "*Bold*, _Italic_\n`Inline code`\n```Block code```"
)->markdown();

Artisan Command

Test your bot configuration without writing any code:

# Basic test — uses token from config and sends to default chat_id
php artisan telegram:test

# Override chat_id
php artisan telegram:test --chat_id=123456789

# Send a custom message
php artisan telegram:test --message="Hello from artisan!"

Manage the webhook (see Webhooks):

php artisan telegram:webhook set --url=https://example.com/api/telegram/webhook
php artisan telegram:webhook set --allowed=message --allowed=callback_query --max-connections=20
php artisan telegram:webhook info
php artisan telegram:webhook delete --drop-pending

Webhooks

Receive messages, button presses and other updates from Telegram by webhook. Telegram posts each update to your URL, the package verifies it, turns it into typed objects and dispatches a Laravel event.

Telegram only posts to HTTPS URLs on ports 443, 80, 88 or 8443.

1. Generate a secret

The secret token is sent back by Telegram in the X-Telegram-Bot-Api-Secret-Token header of every update. It is required: without it the endpoint answers every request with 500 and never accepts an update.

openssl rand -hex 32
TELEGRAM_WEBHOOK_SECRET=<the generated value>

It must be 1 to 256 characters of A-Z a-z 0-9 _ -.

2. Register the route

The package never registers a route on its own. Add one where you want it, for example in routes/api.php, where the path becomes /api/telegram/webhook:

use Illuminate\Support\Facades\Route;

Route::telegramWebhook();                  // POST telegram/webhook, named telegram.webhook
Route::telegramWebhook('bots/ops/hook');   // custom URI

The macro returns the route, so you can chain more (->middleware('throttle:120,1')). The route is already excluded from CSRF protection, so it also works in routes/web.php.

3. Register the URL with Telegram

php artisan telegram:webhook set                       # uses the telegram.webhook route
php artisan telegram:webhook set --url=https://example.com/api/telegram/webhook
php artisan telegram:webhook set --allowed=message --allowed=callback_query --max-connections=20 --drop-pending
php artisan telegram:webhook info
php artisan telegram:webhook delete

or from code:

use OzkanOzcan\LaravelTelegram\TelegramFacade as Telegram;

Telegram::setWebhook(route('telegram.webhook'));   // secret from TELEGRAM_WEBHOOK_SECRET

set refuses to run when no secret is configured, and never prints it.

4. Handle updates

Every valid update dispatches OzkanOzcan\LaravelTelegram\Events\TelegramUpdateReceived with an Update in $event->update. Register a listener (Laravel discovers listeners in app/Listeners automatically):

namespace App\Listeners;

use OzkanOzcan\LaravelTelegram\Events\TelegramUpdateReceived;
use OzkanOzcan\LaravelTelegram\TelegramFacade as Telegram;
use OzkanOzcan\LaravelTelegram\Webhook\UpdateKind;

class HandleTelegramUpdate
{
    public function handle(TelegramUpdateReceived $event): void
    {
        $update = $event->update;

        match ($update->kind()) {
            UpdateKind::Message => Telegram::sendMessage(
                $update->chatId(),
                'You said: ' . ($update->message->text ?? '(no text)')
            ),
            UpdateKind::CallbackQuery => $this->handleCallback($update),
            default => null,
        };
    }

    private function handleCallback($update): void
    {
        $query = $update->callbackQuery;

        Telegram::answerCallbackQuery($query->id, 'Got it: ' . $query->data);

        if ($update->chatId() !== null) {
            Telegram::sendMessage($update->chatId(), 'Button pressed: ' . $query->data);
        }
    }
}

Update objects

All classes are in OzkanOzcan\LaravelTelegram\Webhook, readonly, with camelCase public properties. Fields Telegram does not send are null (or an empty array for lists).

Class Contents
Update updateId, message, editedMessage, channelPost, editedChannelPost, callbackQuery, myChatMember, raw (the full decoded update, for fields not modeled here); kind(), chatId()
UpdateKind Enum: Message, EditedMessage, ChannelPost, EditedChannelPost, CallbackQuery, MyChatMember, Unknown
IncomingMessage messageId, from, chat, date, text, caption, entities, photo, document, replyToMessage
User id, isBot, firstName, lastName, username, languageCode
Chat id, type, title, username, firstName, lastName
MessageEntity type, offset, length, url
PhotoSize fileId, fileUniqueId, width, height, fileSize
Document fileId, fileUniqueId, fileName, mimeType, fileSize
CallbackQuery id, from, message, data
ChatMemberUpdated chat, from, date, oldChatMember, newChatMember
ChatMember status, user
  • $update->kind() returns the UpdateKind of the field that is set, UpdateKind::Unknown for update types that are not modeled (read them from $update->raw).
  • $update->chatId() returns the chat id, or null when there is none (an unknown kind, or a callback from an inline message). Chat ids of groups and channels are negative and can exceed 32 bits.

Response codes

Code When
200 The update was valid and the event was dispatched
401 The secret token header is missing or wrong
500 TELEGRAM_WEBHOOK_SECRET is not configured: requests are never accepted without a secret
413 The body is larger than 1 MiB
400 The body is not a valid update (bad JSON, or no update_id)
500 A listener threw an exception

Only POST is routed; other methods get 405.

A listener exception is not caught, so Telegram gets a 500 and redelivers the update. Make listeners idempotent ($update->updateId is unique per update) or push the work onto a queue and return quickly.

Custom controller

The pieces work on their own if you prefer your own endpoint:

use OzkanOzcan\LaravelTelegram\Http\Middleware\VerifyTelegramWebhook;
use OzkanOzcan\LaravelTelegram\Webhook\Update;
use OzkanOzcan\LaravelTelegram\Webhook\WebhookVerifier;

// 1. The middleware: 401 for a wrong header, 500 when no secret is configured
Route::post('/my/hook', MyController::class)->middleware(VerifyTelegramWebhook::class);

// 2. Or verify by hand (throws TelegramWebhookException, never skips on an empty secret)
WebhookVerifier::verify(
    $request->header(WebhookVerifier::SECRET_TOKEN_HEADER),
    config('telegram.webhook.secret')
);

// 3. Parse the body (throws TelegramWebhookException for invalid input)
$update = Update::fromJson($request->getContent());

Language Files

The package ships with en and tr language files.

Publish them to customise or add new locales:

php artisan vendor:publish --tag=telegram-lang

Files will be placed at lang/vendor/telegram/{locale}/telegram.php.

To use translations in your own code:

trans('telegram::telegram.missing_token');
trans('telegram::telegram.api_error', ['code' => 429, 'description' => 'Too Many Requests']);

To add a new locale (e.g. German), create:

lang/vendor/telegram/de/telegram.php

and add the same keys as the en file.

Configuration Reference

After publishing the config (php artisan vendor:publish --tag=telegram-config), you can fine-tune config/telegram.php:

Key Default Description
token '' Bot token from BotFather
chat_id '' Default recipient chat ID
parse_mode HTML HTML, Markdown, or MarkdownV2
disable_web_page_preview false Disable link previews
disable_notification false Silent notifications
api_url https://api.telegram.org Bot API server root (TELEGRAM_API_URL). /bot<TOKEN>/<method> is appended automatically; a trailing / or /bot is accepted
timeout 30 HTTP read timeout (seconds)
connect_timeout 10 HTTP connect timeout (seconds)
retry.times 3 Retry count on HTTP 429
retry.sleep_ms 1000 Delay between retries (ms)
proxy null HTTP proxy URL
logging false Log every sent message
webhook.secret '' Webhook secret token (TELEGRAM_WEBHOOK_SECRET): given to setWebhook and checked on every incoming update; 1 to 256 characters of A-Z a-z 0-9 _ -. Required to serve the webhook

Error Handling

use OzkanOzcan\LaravelTelegram\Exceptions\TelegramApiException;
use OzkanOzcan\LaravelTelegram\Exceptions\TelegramChannelException;
use OzkanOzcan\LaravelTelegram\TelegramFacade as Telegram;
use OzkanOzcan\LaravelTelegram\TelegramMessage;

try {
    Telegram::sendMessage(
        config('telegram.chat_id'),
        TelegramMessage::create('Hello!')->html()
    );
} catch (TelegramChannelException $e) {
    // Configuration error (missing token, missing chat_id)
    logger()->error('Telegram config error: ' . $e->getMessage());

} catch (TelegramApiException $e) {
    if ($e->isRateLimit()) {
        // HTTP 429 — bot is sending too many messages
        logger()->warning('Telegram rate limit hit.');
    } elseif ($e->isBotBlocked()) {
        // HTTP 403 — user blocked the bot
        logger()->warning('Bot was blocked by user.');
    } elseif ($e->isBadRequest()) {
        // HTTP 400 — invalid parameters
        logger()->error('Bad Telegram API request: ' . $e->getTelegramDescription());
    } else {
        logger()->error("Telegram API [{$e->getTelegramErrorCode()}]: {$e->getTelegramDescription()}");
    }
}

Testing

composer install
vendor/bin/phpunit

The PHP and Rust versions share fixtures/telegram.json: request URLs, payloads, error and retry behavior, chat id resolution and message serialization are checked against the same file in both packages (tests/FixturesTest.php here).

In your own application tests, you can mock TelegramBot to avoid real API calls:

use OzkanOzcan\LaravelTelegram\TelegramBot;
use OzkanOzcan\LaravelTelegram\TelegramMessage;

$this->mock(TelegramBot::class)
    ->shouldReceive('sendMessage')
    ->once()
    ->with(123456789, Mockery::type(TelegramMessage::class))
    ->andReturn(['message_id' => 42]);

$user->notify(new OrderShipped($order));

Changelog

See CHANGELOG.md for a history of changes.

Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/my-feature
  3. Commit your changes following Conventional Commits
  4. Push and open a Pull Request against development

Please make sure all tests pass before submitting a PR.

License

MIT © Özkan Özcan — Özcan Teknoloji
See LICENSE for full details.