abergasov/telegram_logger

Telegram php 日志记录器

1.0.6 2020-01-28 10:09 UTC

This package is auto-updated.

Last update: 2024-09-28 20:14:50 UTC


README

网站/应用 PHP 日志记录器

示例消息 telegram/slack

Repo_List

Repo_List

Repo_List

安装

composer require abergasov/telegram_logger

使用

const INFO_CHAT = 0;
const ERROR_CHAT = 1;
const CONTACT_CHAT = 2;

$logger = new TelegramLogger([
    'token' => SET_YOUR_TOKEN_HERE,
    'chats' => [
        INFO_CHAT => -199024103,
        ERROR_CHAT => -293646246,
        CONTACT_CHAT => -305488244,
        ... add more or less if need
    ],
    'trace_dir' => __DIR__ . '/trace/logs',
    'decorate_url' => 'https://example.com/trace/logs',
    'logs' => [
        'access_log' => '/home/admin/web/my_site/logs/access_log.log',
        'error_log' => '/home/admin/web/my_site/logs/error_log.log',
    ]
]);
$logger->addSlackConfig([
    'token' => SET_YOUR_SLACK_TOKEN_HERE,
    'channels' => [
        INFO_CHAT => 'GF4UTEHGB',
        ERROR_CHAT => 'GF4UTEHGB',
        CONTACT_CHAT => 'GF4UTEHGB',
    ],
]);

$result = $logger->sendMessage(INFO_CHAT, 'Hello, I need help', 'Additional info 1', 'Additional info 2', 'Additional info 3');
echo ($result ? 'Info message was send' : 'Troubles in send messages') . PHP_EOL;

try {
    throw new RuntimeException('Something went wrong in this world');
} catch (Throwable $t) {
    $logger->sendMessage(ERROR_CHAT, 'Exception in script', $t);
}

配置文档

创建 telegram 机器人教程

  • 找到 BotFather。
  • 发送 /newbot。
  • 为您的机器人设置名称和机器人名称。
  • 获取令牌并将其添加到您的 .env 文件中。
  • 找到您的机器人(BotFather 已经在其最后一条消息中生成链接)。
  • 向他发送一条或几条消息。
  • 打开以下网址 https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates 并找到 chat_id。
  • 找到 @myidbot 并向他发送 /getid 以获取个人聊天 ID
  • 找到 @myidbot,将其添加到群组,并向他发送 /getgroupid@myidbot 以获取聊天 ID

telegram_logger