2017 © Pedro Peláez
 

library telegram-handler

Monolog handler to send log by Telegram

image

mero/telegram-handler

Monolog handler to send log by Telegram

  • Thursday, April 26, 2018
  • by merorafael
  • Repository
  • 1 Watchers
  • 14 Stars
  • 6,168 Installations
  • PHP
  • 0 Dependents
  • 2 Suggesters
  • 4 Forks
  • 0 Open issues
  • 4 Versions
  • 21 % Grown

The README.md

TelegramHandler

SensioLabsInsight Build Status Coverage Status Latest Stable Version Total Downloads License, (*1)

Monolog handler to send log by Telegram., (*2)

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., (*3)

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

Example:, (*4)

<?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., (*5)

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., (*6)

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., (*7)

Conversation example:, (*8)

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

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., (*10)

URL: https://api.telegram.org/bot_token_/getUpdates, (*11)

Example:, (*12)

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"., (*13)

The Versions

26/04 2018

dev-master

9999999-dev

Monolog handler to send log by Telegram

  Sources   Download

MIT

The Requires

 

The Development Requires

log logging monolog telegram

26/04 2018

0.3.0

0.3.0.0

Monolog handler to send log by Telegram

  Sources   Download

MIT

The Requires

 

The Development Requires

log logging monolog telegram

02/12 2016

0.2.0

0.2.0.0

Monolog handler to send log by Telegram

  Sources   Download

MIT

The Requires

 

The Development Requires

log logging monolog telegram

18/10 2016

0.1.0

0.1.0.0

Monolog handler to send log by Telegram

  Sources   Download

MIT

The Requires

 

The Development Requires

log logging monolog telegram