TelegramBotHandler
extends AbstractProcessingHandler
in package
Handler sends logs to Telegram using Telegram Bot API.
How to use:
- Create a Telegram bot with https://telegram.me/BotFather;
- Create a Telegram channel or a group where logs will be recorded;
- Add the created bot from step 1 to the created channel/group from step 2.
In order to create an instance of TelegramBotHandler use
- The Telegram bot API key from step 1
- The channel name with the
@prefix if you created a public channel (e.g.@my_public_channel), or the channel ID with the-100prefix if you created a private channel (e.g.-1001234567890), or the group ID from step 2 (e.g.-1234567890).
Tags
Table of Contents
Constants
- 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
Properties
- $bubble : bool
- $formatter : FormatterInterface|null
- $level : Level
- $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.
- $topic : int|null
- Telegram message thread id, unique identifier for the target message thread (topic) of the forum; for forum supergroups only See how to get the `message_thread_id` https://stackoverflow.com/a/75178418
Methods
- __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() : $this
- disableWebPagePreview() : $this
- getBubble() : bool
- Gets the bubbling behavior.
- getFormatter() : FormatterInterface
- getLevel() : Level
- Gets minimum logging level at which this handler will be triggered.
- handle() : bool
- Handles a record.
- handleBatch() : void
- Handles a set of records at once.
- isHandling() : bool
- Checks whether the given record will be handled by this handler.
- popProcessor() : callable
- pushProcessor() : HandlerInterface
- reset() : void
- setBubble() : $this
- Sets the bubbling behavior.
- setFormatter() : HandlerInterface
- setLevel() : $this
- Sets minimum logging level at which this handler will be triggered.
- setParseMode() : $this
- setTopic() : $this
- 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() : LogRecord
- resetProcessors() : void
- send() : void
- Send request to @link https://api.telegram.org/bot on SendMessage action.
- sendCurl() : void
- write() : void
- Writes the (already formatted) 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
= null
$level
protected
Level
$level
= \Monolog\Level::Debug
$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.
$topic
Telegram message thread id, unique identifier for the target message thread (topic) of the forum; for forum supergroups only See how to get the `message_thread_id` https://stackoverflow.com/a/75178418
private
int|null
$topic
Methods
__construct()
public
__construct(string $apiKey, string $channel[, mixed $level = Level::Debug ][, bool $bubble = true ][, string|null $parseMode = null ][, bool|null $disableWebPagePreview = null ][, bool|null $disableNotification = null ][, bool $splitLongMessages = false ][, bool $delayBetweenMessages = false ][, int $topic = null ]) : mixed
Parameters
- $apiKey : string
-
Telegram bot access token provided by BotFather
- $channel : string
-
Telegram channel name
- $level : mixed = Level::Debug
-
The minimum logging level at which this handler will be triggered
- $bubble : bool = true
-
Whether the messages that are handled can bubble up the stack or not
- $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
- $topic : int = null
-
Telegram message thread id, unique identifier for the target message thread (topic) of the forum
Tags
__destruct()
public
__destruct() : mixed
__sleep()
public
__sleep() : mixed
close()
Closes the handler.
public
close() : void
Tags
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
$thisdisableNotification()
public
disableNotification([bool|null $disableNotification = null ]) : $this
Parameters
- $disableNotification : bool|null = null
Return values
$thisdisableWebPagePreview()
public
disableWebPagePreview([bool|null $disableWebPagePreview = null ]) : $this
Parameters
- $disableWebPagePreview : bool|null = null
Return values
$thisgetBubble()
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()
public
getFormatter() : FormatterInterface
Tags
Return values
FormatterInterfacegetLevel()
Gets minimum logging level at which this handler will be triggered.
public
getLevel() : Level
Return values
Levelhandle()
Handles a record.
public
handle(LogRecord $record) : bool
Parameters
- $record : LogRecord
-
The record to handle
Tags
Return values
bool —true means that this handler handled the record, and that bubbling is not permitted. false means the record was either not processed or that this handler allows bubbling.
handleBatch()
Handles a set of records at once.
public
handleBatch(array<string|int, mixed> $records) : void
Parameters
- $records : array<string|int, mixed>
-
The records to handle
Tags
isHandling()
Checks whether the given record will be handled by this handler.
public
isHandling(LogRecord $record) : bool
Parameters
- $record : LogRecord
-
Partial log record having only a level initialized
Tags
Return values
boolpopProcessor()
public
popProcessor() : callable
Tags
Return values
callablepushProcessor()
public
pushProcessor(callable $callback) : HandlerInterface
Parameters
- $callback : callable
Tags
Return values
HandlerInterfacereset()
public
reset() : void
setBubble()
Sets the bubbling behavior.
public
setBubble(bool $bubble) : $this
Parameters
- $bubble : bool
-
true means that this handler allows bubbling. false means that bubbling is not permitted.
Return values
$thissetFormatter()
public
setFormatter(FormatterInterface $formatter) : HandlerInterface
Parameters
- $formatter : FormatterInterface
Tags
Return values
HandlerInterfacesetLevel()
Sets minimum logging level at which this handler will be triggered.
public
setLevel(Level|LogLevel::* $level) : $this
Parameters
- $level : Level|LogLevel::*
-
Level or level name
Tags
Return values
$thissetParseMode()
public
setParseMode([string|null $parseMode = null ]) : $this
Parameters
- $parseMode : string|null = null
Return values
$thissetTopic()
public
setTopic([int $topic = null ]) : $this
Parameters
- $topic : int = null
Return values
$thissplitLongMessages()
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
$thisgetDefaultFormatter()
Gets the default formatter.
protected
getDefaultFormatter() : FormatterInterface
Overwrite this if the LineFormatter is not a good default for your handler.
Return values
FormatterInterfaceprocessRecord()
protected
processRecord(LogRecord $record) : LogRecord
Parameters
- $record : LogRecord
Return values
LogRecordresetProcessors()
protected
resetProcessors() : void
send()
Send request to @link https://api.telegram.org/bot on SendMessage action.
protected
send(string $message) : void
Parameters
- $message : string
sendCurl()
protected
sendCurl(string $message) : void
Parameters
- $message : string
write()
Writes the (already formatted) record down to the log of the implementing handler
protected
write(LogRecord $record) : void
Parameters
- $record : LogRecord
Tags
handleMessageLength()
Handle a message that is too long: truncates or splits into several
private
handleMessageLength(string $message) : array<string|int, string>
Parameters
- $message : string