mero/telegram-handler

Monolog handler to send log by Telegram

0.3.2 2021-01-16 21:45 UTC

This package is auto-updated.

Last update: 2024-11-17 06:17:16 UTC


README

SensioLabsInsight Build Status Coverage Status Latest Stable Version Total Downloads License

Monolog handler to send log by Telegram.

Requirements

  • PHP 5.6 or above
  • cURL extension

Instalation with composer

  1. Open your project directory;
  2. Run composer require mero/telegram-handler to add TelegramHandler in your project vendor;
  3. Add symfony/yaml dependency if you need use the \Mero\Monolog\Formatter\HtmlFormatter.

Declaring handler object

To declare this handler, you need to know the bot token and the chat identifier(chat_id) to which the log will be sent.

// ...
$handler = new \Mero\Monolog\Handler\TelegramHandler('<token>', <chat_id>, <log_level>);
// ...

Example:

<?php

$log = new \Monolog\Logger('telegram_channel');

$handler = new \Mero\Monolog\Handler\TelegramHandler(
    '000000000:XXXXX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    111111111,
    \Monolog\Logger::DEBUG
);
$handler->setFormatter(new \Mero\Monolog\Formatter\HtmlFormatter());
$handler->setTimeout(30);
$log->pushHandler($handler);

$log->debug('Message log');

The above example is using HtmlFormatter for Telegram API. This feature is added on 0.3.0 release and you can use declaring handler formatter to use \Mero\Monolog\Formatter\HtmlFormatter class.

You can set the timeout for Telegram request using setTimeout method, implemented on TelegramHandler. This feature is implemented on 0.4.0 release and this use is not required.

Creating a bot

To use this handler, you need to create your bot on telegram and receive the Bot API access token. To do this, start a conversation with @BotFather.

Conversation example:

In the example below, I'm talking to @BotFather. to create a bot named "Cronus Bot" with user "@cronus_bot".

Me: /newbot
---
@BotFather: Alright, a new bot. How are we going to call it? Please choose a name for your bot.
---
Me: Cronus Bot
---
@BotFather: Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: 
TetrisBot or tetris_bot.
---
Me: cronus_bot
---
@BotFather: Done! Congratulations on your new bot. You will find it at telegram.me/cronus_bot. You can now add a 
description, about section and profile picture for your bot, see /help for a list of commands. By the way, when 
you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure 
the bot is fully operational before you do this.

Use this token to access the HTTP API:
000000000:XXXXX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

For a description of the Bot API, see this page: https://core.telegram.org/bots/api

Give a chat identifier

To retrieve the chat_id in which the log will be sent, the recipient user will first need a conversation with the bot. After the conversation has started, make the request below to know the chat_id of that conversation.

URL: https://api.telegram.org/bot_token_/getUpdates

Example:

Request
-------
POST https://api.telegram.org/bot000000000:XXXXX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx/getUpdates

Response
--------
{
  "ok": true,
  "result": [
    {
      "update_id": 141444845,
      "message": {
        "message_id": 111,
        "from": {
          "id": 111111111,
          "first_name": "Rafael",
          "last_name": "Mello",
          "username": "merorafael"
        },
        "chat": {
          "id": 111111111,
          "first_name": "Rafael",
          "last_name": "Mello",
          "username": "merorafael",
          "type": "private"
        },
        "date": 1480701504,
        "text": "test"
      }
    }
  ]
}

In the above request, the chat_id is represented by the number "111111111".