TelegramBotHandler.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use RuntimeException;
  12. use Monolog\Logger;
  13. /**
  14. * Handler send logs to Telegram using Telegram Bot API.
  15. *
  16. * How to use:
  17. * 1) Create telegram bot with https://telegram.me/BotFather
  18. * 2) Create a telegram channel where logs will be recorded.
  19. * 3) Add created bot from step 1 to the created channel from step 2.
  20. *
  21. * Use telegram bot API key from step 1 and channel name with '@' prefix from step 2 to create instance of TelegramBotHandler
  22. *
  23. * @link https://core.telegram.org/bots/api
  24. *
  25. * @author Mazur Alexandr <alexandrmazur96@gmail.com>
  26. */
  27. class TelegramBotHandler extends AbstractProcessingHandler
  28. {
  29. private const BOT_API = 'https://api.telegram.org/bot';
  30. /**
  31. * Telegram bot access token provided by BotFather.
  32. * Create telegram bot with https://telegram.me/BotFather and use access token from it.
  33. * @var string
  34. */
  35. private $apiKey;
  36. /**
  37. * Telegram channel name.
  38. * Since to start with '@' symbol as prefix.
  39. * @var string
  40. */
  41. private $channel;
  42. /**
  43. * @param string $apiKey Telegram bot access token provided by BotFather
  44. * @param string $channel Telegram channel name
  45. * @inheritDoc
  46. */
  47. public function __construct(
  48. string $apiKey,
  49. string $channel,
  50. $level = Logger::DEBUG,
  51. bool $bubble = true
  52. ) {
  53. parent::__construct($level, $bubble);
  54. $this->apiKey = $apiKey;
  55. $this->channel = $channel;
  56. $this->level = $level;
  57. $this->bubble = $bubble;
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. protected function write(array $record): void
  63. {
  64. $this->send($record['formatted']);
  65. }
  66. /**
  67. * Send request to @link https://api.telegram.org/bot on SendMessage action.
  68. * @param string $message
  69. */
  70. protected function send(string $message): void
  71. {
  72. $ch = curl_init();
  73. $url = self::BOT_API . $this->apiKey . '/SendMessage';
  74. curl_setopt($ch, CURLOPT_URL, $url);
  75. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  76. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  77. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
  78. 'text' => $message,
  79. 'chat_id' => $this->channel,
  80. ]));
  81. $result = Curl\Util::execute($ch);
  82. $result = json_decode($result, true);
  83. if ($result['ok'] === false) {
  84. throw new RuntimeException('Telegram API error. Description: ' . $result['description']);
  85. }
  86. }
  87. }