\Monolog\HandlerTelegramBotHandler

Handler send logs to Telegram using Telegram Bot API.

How to use:

  1. Create telegram bot with https://telegram.me/BotFather
  2. Create a telegram channel where logs will be recorded.
  3. 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

Summary

Methods
Properties
Constants
handle()
reset()
pushProcessor()
popProcessor()
setFormatter()
getFormatter()
__construct()
isHandling()
setLevel()
getLevel()
setBubble()
getBubble()
handleBatch()
close()
__destruct()
__sleep()
setParseMode()
disableWebPagePreview()
disableNotification()
splitLongMessages()
delayBetweenMessages()
No public properties found
BOT_API
AVAILABLE_PARSE_MODES
MAX_MESSAGE_LENGTH
write()
processRecord()
resetProcessors()
getDefaultFormatter()
send()
sendCurl()
$processors
$formatter
$level
$bubble
N/A
handleMessageLength()
$apiKey
$channel
$parseMode
$disableWebPagePreview
$disableNotification
$splitLongMessages
$delayBetweenMessages
N/A

Constants

BOT_API

BOT_API = 'https://api.telegram.org/bot'

AVAILABLE_PARSE_MODES

AVAILABLE_PARSE_MODES = ['HTML', 'MarkdownV2', 'Markdown']

The available values of parseMode according to the Telegram api documentation

MAX_MESSAGE_LENGTH

MAX_MESSAGE_LENGTH = 4096

The maximum number of characters allowed in a message according to the Telegram api documentation

Properties

$processors

$processors : callable[]

Type

callable[]

$formatter

$formatter : ?\Monolog\Formatter\FormatterInterface

Type

FormatterInterface

$level

$level : int

Type

int

$bubble

$bubble : bool

Type

bool

$apiKey

$apiKey : string

Telegram bot access token provided by BotFather.

Create telegram bot with https://telegram.me/BotFather and use access token from it.

Type

string

$channel

$channel : string

Telegram channel name.

Since to start with '@' symbol as prefix.

Type

string

$parseMode

$parseMode : ?string

The kind of formatting that is used for the message.

See available options at https://core.telegram.org/bots/api#formatting-options or in AVAILABLE_PARSE_MODES

Type

?string

$disableWebPagePreview

$disableWebPagePreview : ?bool

Disables link previews for links in the message.

Type

?bool

$disableNotification

$disableNotification : ?bool

Sends the message silently. Users will receive a notification with no sound.

Type

?bool

$splitLongMessages

$splitLongMessages : bool

True - split a message longer than MAX_MESSAGE_LENGTH into parts and send in multiple messages.

False - truncates a message that is too long.

Type

bool

$delayBetweenMessages

$delayBetweenMessages : bool

Adds 1-second delay between sending a split message (according to Telegram API to avoid 429 Too Many Requests).

Type

bool

Methods

handle()

handle(array  $record) : bool

{@inheritDoc}

Parameters

array $record

Returns

bool —

reset()

reset() : void

pushProcessor()

pushProcessor(callable  $callback) : \Monolog\Handler\HandlerInterface

{@inheritDoc}

Parameters

callable $callback

Returns

\Monolog\Handler\HandlerInterface —

popProcessor()

popProcessor() : callable

{@inheritDoc}

Returns

callable —

setFormatter()

setFormatter(\Monolog\Formatter\FormatterInterface  $formatter) : \Monolog\Handler\HandlerInterface

{@inheritDoc}

Parameters

\Monolog\Formatter\FormatterInterface $formatter

Returns

\Monolog\Handler\HandlerInterface —

getFormatter()

getFormatter() : \Monolog\Formatter\FormatterInterface

{@inheritDoc}

Returns

\Monolog\Formatter\FormatterInterface —

__construct()

__construct(string  $apiKey, string  $channel, mixed  $level = Logger::DEBUG, bool  $bubble = true, string  $parseMode = null, bool  $disableWebPagePreview = null, bool  $disableNotification = null, bool  $splitLongMessages = false, bool  $delayBetweenMessages = false) : mixed

Parameters

string $apiKey

Telegram bot access token provided by BotFather

string $channel

Telegram channel name

mixed $level
bool $bubble
string $parseMode
bool $disableWebPagePreview
bool $disableNotification
bool $splitLongMessages

Split a message longer than MAX_MESSAGE_LENGTH into parts and send in multiple messages

bool $delayBetweenMessages

Adds delay between sending a split message according to Telegram API

Throws

\Monolog\Handler\MissingExtensionException

Returns

mixed —

isHandling()

isHandling(array  $record) : bool

{@inheritDoc}

Parameters

array $record

Returns

bool —

setLevel()

setLevel(mixed  $level) : self

Sets minimum logging level at which this handler will be triggered.

Parameters

mixed $level

Returns

self —

getLevel()

getLevel() : int

Gets minimum logging level at which this handler will be triggered.

Returns

int —

setBubble()

setBubble(bool  $bubble) : self

Sets the bubbling behavior.

Parameters

bool $bubble

true means that this handler allows bubbling. false means that bubbling is not permitted.

Returns

self —

getBubble()

getBubble() : bool

Gets the bubbling behavior.

Returns

bool —

true means that this handler allows bubbling. false means that bubbling is not permitted.

handleBatch()

handleBatch(array  $records) : void

{@inheritDoc}

Parameters

array $records

close()

close() : void

Closes the handler.

__destruct()

__destruct() : mixed

Returns

mixed —

__sleep()

__sleep() : mixed

Returns

mixed —

setParseMode()

setParseMode(string  $parseMode = null) : self

Parameters

string $parseMode

Returns

self —

disableWebPagePreview()

disableWebPagePreview(bool  $disableWebPagePreview = null) : self

Parameters

bool $disableWebPagePreview

Returns

self —

disableNotification()

disableNotification(bool  $disableNotification = null) : self

Parameters

bool $disableNotification

Returns

self —

splitLongMessages()

splitLongMessages(bool  $splitLongMessages = false) : $this

True - split a message longer than MAX_MESSAGE_LENGTH into parts and send in multiple messages.

False - truncates a message that is too long.

Parameters

bool $splitLongMessages

Returns

$this —

delayBetweenMessages()

delayBetweenMessages(bool  $delayBetweenMessages = false) : $this

Adds 1-second delay between sending a split message (according to Telegram API to avoid 429 Too Many Requests).

Parameters

bool $delayBetweenMessages

Returns

$this —

write()

write(array  $record) : void

Writes the record down to the log of the implementing handler

Parameters

array $record

processRecord()

processRecord(array  $record) : array

Processes a record.

Parameters

array $record

Returns

array —

resetProcessors()

resetProcessors() : void

getDefaultFormatter()

getDefaultFormatter() : \Monolog\Formatter\FormatterInterface

Gets the default formatter.

Overwrite this if the LineFormatter is not a good default for your handler.

Returns

\Monolog\Formatter\FormatterInterface —

send()

send(string  $message) : void

Send request to @link https://api.telegram.org/bot on SendMessage action.

Parameters

string $message

sendCurl()

sendCurl(string  $message) : void

Parameters

string $message

handleMessageLength()

handleMessageLength(string  $message) : string[]

Handle a message that is too long: truncates or splits into several

Parameters

string $message

Returns

string[] —