TelegramBotHandler
extends AbstractProcessingHandler
in package
Handler send logs to Telegram using Telegram Bot API.
How to use:
- Create telegram bot with https://telegram.me/BotFather
- Create a telegram channel where logs will be recorded.
- Add created bot from step 1 to the created channel from step 2.
Use telegram bot API key from step 1 and channel name with '@' prefix from step 2 to create instance of TelegramBotHandler
Tags
Table of Contents
- AVAILABLE_PARSE_MODES = ['HTML', 'MarkdownV2', 'Markdown']
- The available values of parseMode according to the Telegram api documentation
- BOT_API = 'https://api.telegram.org/bot'
- MAX_MESSAGE_LENGTH = 4096
- The maximum number of characters allowed in a message according to the Telegram api documentation
- $bubble : bool
- $formatter : FormatterInterface|null
- $level : int
- $processors : array<string|int, callable>
- $apiKey : string
- Telegram bot access token provided by BotFather.
- $channel : string
- Telegram channel name.
- $delayBetweenMessages : bool
- Adds 1-second delay between sending a split message (according to Telegram API to avoid 429 Too Many Requests).
- $disableNotification : bool|null
- Sends the message silently. Users will receive a notification with no sound.
- $disableWebPagePreview : bool|null
- Disables link previews for links in the message.
- $parseMode : string|null
- The kind of formatting that is used for the message.
- $splitLongMessages : bool
- True - split a message longer than MAX_MESSAGE_LENGTH into parts and send in multiple messages.
- __construct() : mixed
- __destruct() : mixed
- __sleep() : mixed
- close() : void
- Closes the handler.
- delayBetweenMessages() : $this
- Adds 1-second delay between sending a split message (according to Telegram API to avoid 429 Too Many Requests).
- disableNotification() : self
- disableWebPagePreview() : self
- getBubble() : bool
- Gets the bubbling behavior.
- getFormatter() : FormatterInterface
- {@inheritDoc}
- getLevel() : int
- Gets minimum logging level at which this handler will be triggered.
- handle() : bool
- {@inheritDoc}
- handleBatch() : void
- {@inheritDoc}
- isHandling() : bool
- {@inheritDoc}
- popProcessor() : callable
- {@inheritDoc}
- pushProcessor() : HandlerInterface
- {@inheritDoc}
- reset() : void
- setBubble() : self
- Sets the bubbling behavior.
- setFormatter() : HandlerInterface
- {@inheritDoc}
- setLevel() : self
- Sets minimum logging level at which this handler will be triggered.
- setParseMode() : self
- splitLongMessages() : $this
- True - split a message longer than MAX_MESSAGE_LENGTH into parts and send in multiple messages.
- getDefaultFormatter() : FormatterInterface
- Gets the default formatter.
- processRecord() : array<string|int, mixed>
- Processes a record.
- resetProcessors() : void
- send() : void
- Send request to @link https://api.telegram.org/bot on SendMessage action.
- sendCurl() : void
- write() : void
- Writes the record down to the log of the implementing handler
- handleMessageLength() : array<string|int, string>
- Handle a message that is too long: truncates or splits into several
Constants
AVAILABLE_PARSE_MODES
The available values of parseMode according to the Telegram api documentation
private
mixed
AVAILABLE_PARSE_MODES
= ['HTML', 'MarkdownV2', 'Markdown']
BOT_API
private
mixed
BOT_API
= 'https://api.telegram.org/bot'
MAX_MESSAGE_LENGTH
The maximum number of characters allowed in a message according to the Telegram api documentation
private
mixed
MAX_MESSAGE_LENGTH
= 4096
Properties
$bubble
protected
bool
$bubble
= true
$formatter
protected
FormatterInterface|null
$formatter
$level
protected
int
$level
= MonologLogger::DEBUG
Tags
$processors
protected
array<string|int, callable>
$processors
= []
Tags
$apiKey
Telegram bot access token provided by BotFather.
private
string
$apiKey
Create telegram bot with https://telegram.me/BotFather and use access token from it.
$channel
Telegram channel name.
private
string
$channel
Since to start with '@' symbol as prefix.
$delayBetweenMessages
Adds 1-second delay between sending a split message (according to Telegram API to avoid 429 Too Many Requests).
private
bool
$delayBetweenMessages
$disableNotification
Sends the message silently. Users will receive a notification with no sound.
private
bool|null
$disableNotification
$disableWebPagePreview
Disables link previews for links in the message.
private
bool|null
$disableWebPagePreview
$parseMode
The kind of formatting that is used for the message.
private
string|null
$parseMode
See available options at https://core.telegram.org/bots/api#formatting-options or in AVAILABLE_PARSE_MODES
$splitLongMessages
True - split a message longer than MAX_MESSAGE_LENGTH into parts and send in multiple messages.
private
bool
$splitLongMessages
False - truncates a message that is too long.
Methods
__construct()
public
__construct(string $apiKey, string $channel[, mixed $level = Logger::DEBUG ][, bool $bubble = true ][, string|null $parseMode = null ][, bool|null $disableWebPagePreview = null ][, bool|null $disableNotification = null ][, bool $splitLongMessages = false ][, bool $delayBetweenMessages = false ]) : mixed
Parameters
- $apiKey : string
-
Telegram bot access token provided by BotFather
- $channel : string
-
Telegram channel name
- $level : mixed = Logger::DEBUG
- $bubble : bool = true
- $parseMode : string|null = null
- $disableWebPagePreview : bool|null = null
- $disableNotification : bool|null = null
- $splitLongMessages : bool = false
-
Split a message longer than MAX_MESSAGE_LENGTH into parts and send in multiple messages
- $delayBetweenMessages : bool = false
-
Adds delay between sending a split message according to Telegram API
Tags
Return values
mixed —__destruct()
public
__destruct() : mixed
Return values
mixed —__sleep()
public
__sleep() : mixed
Return values
mixed —close()
Closes the handler.
public
close() : void
Return values
void —delayBetweenMessages()
Adds 1-second delay between sending a split message (according to Telegram API to avoid 429 Too Many Requests).
public
delayBetweenMessages([bool $delayBetweenMessages = false ]) : $this
Parameters
- $delayBetweenMessages : bool = false
Return values
$this —disableNotification()
public
disableNotification([bool|null $disableNotification = null ]) : self
Parameters
- $disableNotification : bool|null = null
Return values
self —disableWebPagePreview()
public
disableWebPagePreview([bool|null $disableWebPagePreview = null ]) : self
Parameters
- $disableWebPagePreview : bool|null = null
Return values
self —getBubble()
Gets the bubbling behavior.
public
getBubble() : bool
Return values
bool —true means that this handler allows bubbling. false means that bubbling is not permitted.
getFormatter()
{@inheritDoc}
public
getFormatter() : FormatterInterface
Return values
FormatterInterface —getLevel()
Gets minimum logging level at which this handler will be triggered.
public
getLevel() : int
Tags
Return values
int —handle()
{@inheritDoc}
public
handle(array<string|int, mixed> $record) : bool
Parameters
- $record : array<string|int, mixed>
Return values
bool —handleBatch()
{@inheritDoc}
public
handleBatch(array<string|int, mixed> $records) : void
Parameters
- $records : array<string|int, mixed>
Return values
void —isHandling()
{@inheritDoc}
public
isHandling(array<string|int, mixed> $record) : bool
Parameters
- $record : array<string|int, mixed>
Return values
bool —popProcessor()
{@inheritDoc}
public
popProcessor() : callable
Return values
callable —pushProcessor()
{@inheritDoc}
public
pushProcessor(callable $callback) : HandlerInterface
Parameters
- $callback : callable
Return values
HandlerInterface —reset()
public
reset() : void
Return values
void —setBubble()
Sets the bubbling behavior.
public
setBubble(bool $bubble) : self
Parameters
- $bubble : bool
-
true means that this handler allows bubbling. false means that bubbling is not permitted.
Return values
self —setFormatter()
{@inheritDoc}
public
setFormatter(FormatterInterface $formatter) : HandlerInterface
Parameters
- $formatter : FormatterInterface
Return values
HandlerInterface —setLevel()
Sets minimum logging level at which this handler will be triggered.
public
setLevel(Level|LevelName|\Psr\Log\LogLevel::* $level) : self
Parameters
- $level : Level|LevelName|\Psr\Log\LogLevel::*
-
Level or level name
Return values
self —setParseMode()
public
setParseMode([string|null $parseMode = null ]) : self
Parameters
- $parseMode : string|null = null
Return values
self —splitLongMessages()
True - split a message longer than MAX_MESSAGE_LENGTH into parts and send in multiple messages.
public
splitLongMessages([bool $splitLongMessages = false ]) : $this
False - truncates a message that is too long.
Parameters
- $splitLongMessages : bool = false
Return values
$this —getDefaultFormatter()
Gets the default formatter.
protected
getDefaultFormatter() : FormatterInterface
Overwrite this if the LineFormatter is not a good default for your handler.
Return values
FormatterInterface —processRecord()
Processes a record.
protected
processRecord(array<string|int, mixed> $record) : array<string|int, mixed>
Parameters
- $record : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —resetProcessors()
protected
resetProcessors() : void
Return values
void —send()
Send request to @link https://api.telegram.org/bot on SendMessage action.
protected
send(string $message) : void
Parameters
- $message : string
Return values
void —sendCurl()
protected
sendCurl(string $message) : void
Parameters
- $message : string
Return values
void —write()
Writes the record down to the log of the implementing handler
protected
write(array<string|int, mixed> $record) : void
Parameters
- $record : array<string|int, mixed>
Tags
Return values
void —handleMessageLength()
Handle a message that is too long: truncates or splits into several
private
handleMessageLength(string $message) : array<string|int, string>
Parameters
- $message : string