locky42 / telegram-logger
A PSR-4 compatible logger for sending messages to Telegram via Bot API.
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/locky42/telegram-logger
Requires
- php: >=7.4
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9.0
README
Simple and reliable service for logging messages to Telegram with PSR-4 autoloading support.
Installation
composer require locky42/telegram-logger
Usage
Basic Usage
<?php use Locky42\TelegramLogger\TelegramLogger; // Initialize the logger (default bot token is used if not provided) $logger = new TelegramLogger('YOUR_CHAT_ID'); // or $logger = new TelegramLogger('YOUR_CHAT_ID', 'YOUR_BOT_TOKEN'); // Send a message $logger->log('Test message'); // Send a message with log level $logger->info('Information message'); $logger->warning('Warning'); $logger->error('Error');
Configuration
<?php use Locky42\TelegramLogger\TelegramLogger; use Locky42\TelegramLogger\Config\TelegramConfig; $config = new TelegramConfig([ 'bot_token' => 'YOUR_BOT_TOKEN', 'chat_id' => 'YOUR_CHAT_ID', 'parse_mode' => 'HTML', // or 'Markdown' 'timeout' => 30, 'thread_id' => 123456789 // optional: message thread ID for group discussions ]); $logger = new TelegramLogger($config); // You can omit 'bot_token' to use the default public bot: // 'bot_token' => '8256677096:AAGaIGto_l9fuARk6LUDcUodb0ks6iZkZD8',
Getting Bot Token
- Message @BotFather in Telegram
- Use the
/newbot
command - Give your bot a name
- Get the bot token
Default Bot Token
If you do not provide your own bot token, the default public bot will be used:
8256677096:AAGaIGto_l9fuARk6LUDcUodb0ks6iZkZD8
You can use this for quick tests or demos, but for production use, create your own bot for privacy and security.
Default Bot
By default, the package uses the public bot @tg_multilogger_bot.
You can use this bot for quick tests or demos. For production and privacy, it is recommended to create your own bot via @BotFather.
Getting Chat ID
- Add the bot to the chat
- Send any message to the chat
- Go to:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
- Find
chat.id
in the response
Getting Thread ID
For group discussions with threads:
- Enable topics in your group (Group Settings > Topics)
- Create a new topic/thread in the group
- Send a message in that thread
- Go to:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
- Find
message_thread_id
in the response
License
MIT License