laravel-simple-telegram maintained by devaspid
Laravel Simple Telegram
A lightweight and clean one-way Telegram notification package for Laravel. Designed to easily send notifications to Telegram groups, channels, or specific message topics.
Developed and maintained by Devaspid.
Requirements
- PHP
^8.2or^8.3 - Laravel
^11.0,^12.0, or^13.0
Installation
You can install the package via Composer:
composer require devaspid/laravel-simple-telegram
The service provider and facade will automatically register themselves.
Configuration & Publishing Publish the configuration file using the vendor:publish Artisan command:
php artisan vendor:publish --tag="telegram-config"
This will create a config/telegram.php file in your project:
return [
'enable' => env('TELEGRAM_ENABLE', true),
'bot_token' => env('TELEGRAM_BOT_TOKEN', ''),
'chat_id' => env('TELEGRAM_CHAT_ID', ''),
'topics' => [
'transaction' => env('TELEGRAM_TOPIC_TRANSACTION', null),
],
];
Environment Variables
Add the following environment variables to your .env file:
TELEGRAM_ENABLE=true
TELEGRAM_BOT_TOKEN="your-bot-token-here"
TELEGRAM_CHAT_ID="-100xxxxxxxxx"
TELEGRAM_TOPIC_TRANSACTION=482
Usage
You can use the Telegram facade to send notifications anywhere in your Laravel application.
1. Controller / Livewire Component
Send basic HTML-formatted messages or target specific Telegram topic threads:
namespace App\Http\Controllers;
use Devaspid\Telegram\Facades\Telegram;
use App\Models\Order;
class OrderController extends Controller
{
public function checkout(Order $order)
{
// Simple HTML message
$message = "📦 <b>Test!</b>\n\n"
. "Order Number: <b>#{$order->order_number}</b>\n";
// Send to general group chat
Telegram::send($message);
// Or send to a specific Telegram topic ID
Telegram::send($message, config('telegram.topics.transaction'));
}
}
2. Model Events (e.g., Boot Method)
Trigger Telegram notifications directly inside Eloquent model lifecycle hooks:
namespace App\Models;
use Devaspid\Telegram\Facades\Telegram;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
protected static function booted(): void
{
static::created(function (Order $order) {$message = "🚨 <b>Alert: New Order Created</b>\n"
. "Order ID: #{$order->id}\n"
. "Total: Rp " . number_format($order->total_amount);
Telegram::send($message, config('telegram.topics.transaction'));
});
static::updated(function (Order $order) {
if ($order->isDirty('status') &&$order->status === 'paid') {
Telegram::send("💰 <b>Order #{$order->order_number} has been PAID!</b>");
}
});
}
}
Testing
Run the test suite using PHPUnit:
./vendor/bin/phpunit
License
This package is open-sourced software licensed under the MIT license.
Support the Project
If you find this project useful, consider giving it a ⭐ on GitHub.
Maintained with ❤️ by devASPId.