optim1zer/monolog-telegram

用于通过Telegram发送日志的Monolog处理器

v1.0.0 2018-09-23 10:00 UTC

This package is auto-updated.

Last update: 2024-09-24 04:54:47 UTC


README

通过HTML格式发送日志的Monolog Telegram处理器

要求

  • PHP 5.6或更高版本
  • Guzzle 6+

使用composer安装

composer require optim1zer/monolog-telegram  

声明处理器对象

要声明此处理器,您需要知道用于发送日志的bot token和chat identifier(chat_id)。

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

示例

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

$handler = new \optim1zer\Monolog\TelegramHandler(
    '000000000:XXXXX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    123456789,
    \Monolog\Logger::DEBUG
);
$handler->setFormatter(new \Monolog\Formatter\LineFormatter("%message%", null, true));
$log->pushHandler($handler);

$log->debug('Test message');

上述示例使用来自Monolog包的标准LineFormatter。您可以为更好的日志格式编写并使用自己的消息格式化程序。

带有代理(适用于俄罗斯服务器)的示例

$handler = new \optim1zer\Monolog\TelegramHandler('<token>', <chat_id>, <log_level>);
$handler->setProxy('http://username:password@192.168.16.1:80'); // or simply 'http://192.168.16.1:80'
$handler->setFormatter(new \Monolog\Formatter\LineFormatter("%message%", null, true));

创建机器人

要使用此处理器,您需要在Telegram上创建您的机器人并接收Bot API访问令牌。为此,开始与 @BotFather 进行对话。

对话示例

以下示例中,我与 @BotFather 进行对话,以创建名为 "Logger Bot" 的机器人,用户名为 "@logger_bot"。

Me: /newbot
---
@BotFather: Alright, a new bot. How are we going to call it? Please choose a name for your bot.
---
Me: Logger 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: logger_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

获取聊天标识符

要检索发送日志的chat_id,收件用户首先需要与机器人进行对话。对话开始后,请执行以下请求以获取该对话的chat_id。

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

示例

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

Response
--------
{
  "ok": true,
  "result": [
    {
      "update_id": 121227832,
      "message": {
        "message_id": 1,
        "from": {
          "id": 1583498345345,
          "first_name": "*****",
          "last_name": "*****",
          "username": "Optim1zer"
        },
        "chat": {
          "id": 123456789,
          "first_name": "*****",
          "last_name": "*****",
          "username": "Optim1zer",
          "type": "private"
        },
        "date": 1510701612,
        "text": "test message"
      }
    }
  ]
}

在上面的请求中,chat_id由数字 "123456789" 表示(result.message.chat.id)。