defstudio/laravel-telegram-log

Telegram log channel for Laravel

dev-master 2024-07-12 11:36 UTC

This package is auto-updated.

Last update: 2024-11-12 12:26:03 UTC


README

This is a monolog telegram handler for laravel applications

Installation

You can install the package via composer:

composer require defstudio/laravel-telegram-log

You can publish the config file with:

php artisan vendor:publish --provider="Defstudio\LaravelTelegramLog\LaravelTelegramLogServiceProvider" --tag="config"

This is the contents of the published config file:

return [
    'bot_token' => env('TELEGRAM_BOT_TOKEN'),
    'chat_id' => env('TELEGRAM_CHAT_ID'),
];

Usage

Add the new Log Channel in config/logging.php:

'telegram' => [
    'driver' => 'custom',
    'via'    => LaravelTelegramLog::class,
    'level'  => 'error',
],

If you use the stack channel as default logger, you can just the telegram channel to your stack:

'stack' => [
    'driver' => 'stack',
    'channels' => ['single', 'telegram'],
]

Or you can simply change the default logging channel in the .env file.

LOG_CHANNEL=telegram

Great! Your Laravel project can now send logs to your Telegram chat.

You can use Laravel Log Facade to send logs to your chat:

// Use the Laravel Log Facade
use Illuminate\Support\Facades\Log;
...

// All Laravel log leves are avaiable
Log::channel('telegram')->emergency($message);
Log::channel('telegram')->alert($message);
Log::channel('telegram')->critical($message);
Log::channel('telegram')->error($message);
Log::channel('telegram')->warning($message);
Log::channel('telegram')->notice($message);
Log::channel('telegram')->info($message);
Log::channel('telegram')->debug($message);

Telegram Instructions

Instructions for creating a new Telegram Bot and getting chat_id from a particular group or chat.

Creating a Bot

  1. Go to @BotFather on Telegram.

  2. Send /newbot, to start creating a new Bot.

    message

  3. Set the bot's username and username.

    defines-a-bot-name

  4. Now you need to allow your Bot to send direct messages, so send /setjoingroups to @BotFather, select your Bot and click Enable:

    set-join-group

  5. Get the Bot token and add it to your .env file.

    get-bot-token

    Bot Token in .env:

    token-in-env-file

Getting a Telegram Chat ID

  • If you want to send messages to a group:

    1. Add your Bot to a Telegram group.
    2. Send any message from another user to this group.
  • If you want send direct messages to a user:

    1. Search for your bot name, and select the chat.
    2. Send /start to your Bot.
  1. Visit the following link to get updates about your bot and get chat_id:

    https://api.telegram.org/botXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/getUpdates

Replace all X in the URL with your Bot Token.

  1. Search for the chat that you want to send messages, and get the chat->id:

    get-a-chat-id

  2. Add it to your .env file:

    env

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.