Telegram

telegram package

This module contains the TelegramHandler class.

class TelegramLoggingHandler(bot_token, channel_name, level=0, host='api.telegram.org', protect_content=True, disable_notification=False)[source]

This method is called by the logging framework to write a log message to the telegram channel. https://core.telegram.org/bots/api#sendmessage

Usage:

from bohicalog import logger
from bohicalog.handlers.telegram import (
    HTMLFormatter,
    TelegramLoggingHandler,
)

BOT_TOKEN = TOKEN"
CHANNEL_NAME = "chat id"  # if its public you can use the name with an @ infront: '@example'

telegram_handler = TelegramLoggingHandler(
    BOT_TOKEN, CHANNEL_NAME, level=logging.INFO, disable_notification=False
)

telegram_handler.setFormatter(HTMLFormatter(use_emoji=True))

logger.addHandler(telegram_handler)
logger.info("Telegram test")
Parameters:
  • bot_token – Token of the telegram bot

  • channel_name – channel name to write to

  • level – Logging level

  • host – host to use

  • protect_content – disable forwards (default: True)

  • disable_notification – disable notifications (default: False)

write(message)[source]

Write a message to the telegram channel.

Parameters:

message

emit(record)[source]

Emit a record.

Parameters:

record (LogRecord) – record to emit

Return type:

None

close()[source]

Close the handler.

get_chat_id()[source]

Get the chat id of the channel.

Return type:

list(int)

Returns:

chat ids

This module contains the buffer class.

class Buffer(max_size=None)[source]

Buffer class for the telegram handler

Usage:

from bohicalog.handlers.telegram.buffer import Buffer

buffer = Buffer(max_size=1000)
Parameters:

max_size – max size of the buffer

write(data)[source]

Write data to the buffer

Parameters:

data – data to write

read(count)[source]

Read data from the buffer

Parameters:

count – count of data to read

Return type:

str

Returns:

data read

Contains constants for the telegram handler.

Formatters for bohicalog.handlers.telegram.

class TelegramFormatter(fmt=None, *args, **kwargs)[source]

Base formatter class suitable for use with TelegramHandler

Parameters:
  • fmt – format string

  • args – positional arguments

  • kwargs – keyword arguments

class MarkdownFormatter(fmt=None, *args, **kwargs)[source]

Markdown formatter for telegram.

Parameters:
  • fmt – format string

  • args – positional arguments

  • kwargs – keyword arguments

formatException(*args, **kwargs)[source]

Format exception as markdown.

Parameters:
  • args – positional arguments

  • kwargs – keyword arguments

class HTMLFormatter(*args, **kwargs)[source]

HTML formatter for telegram.

Parameters:
  • args – positional arguments

  • kwargs – keyword arguments

format(record)[source]

Format the record.

Parameters:

record (logging.LogRecord) –

formatException(*args, **kwargs)[source]

Format exception as html.

formatStack(*args, **kwargs)[source]

Format stack as html.

Utility functions for telegram handler

escape_html(text)[source]

Escapes all html characters in text

Parameters:

text (str) – Text to escape

Return type:

str

Returns:

Escaped text