plug.telegram

exception immp.plug.telegram.TelegramAPIConnectError

Bases: immp.core.error.PlugError

Generic error whilst attempting to call the Telegram API.

exception immp.plug.telegram.TelegramAPIRequestError

Bases: immp.core.error.PlugError

Generic error from the Telegram API.

class immp.plug.telegram.TelegramUser(id_=None, plug=None, *, username=None, real_name=None, avatar=None, link=None, suggested=False, raw=None)

Bases: immp.core.message.User

User present in Telegram.

classmethod from_bot_user(telegram, json)

Convert a user dict (attached to a message) to a User.

Parameters
  • telegram (TelegramPlug) – Related plug instance that provides the user.
  • json (dict) – Telegram API User object.
Returns

Parsed user object.

Return type

TelegramUser

classmethod from_bot_channel(telegram, json)

Convert a chat dict (attached to a message) to a User.

Parameters
  • telegram (TelegramPlug) – Related plug instance that provides the user.
  • json (dict) – Telegram API Chat object.
Returns

Parsed user object.

Return type

TelegramUser

classmethod from_proto_user(telegram, user)

Convert a telethon.tl.types.User into a User.

Parameters
  • telegram (TelegramPlug) – Related plug instance that provides the user.
  • user (telethon.tl.types.User) – Telegram user retrieved from the MTProto API.
Returns

Parsed user object.

Return type

TelegramUser

classmethod from_proto_channel(telegram, user, author=None)

Convert a telethon.tl.types.Channel into a User.

Parameters
  • telegram (TelegramPlug) – Related plug instance that provides the user.
  • user (telethon.tl.types.Channel) – Telegram channel retrieved from the MTProto API.
  • author (str) – Optional post author, for channel messages.
Returns

Parsed user object.

Return type

TelegramUser

classmethod from_entity(telegram, entity)

Convert a client entity row into a User.

Parameters
  • telegram (TelegramPlug) – Related plug instance that provides the user.
  • entity ((str, str, str) tuple) – ID, username and real name of a cached Telegram user.
Returns

Parsed user object.

Return type

TelegramUser

class immp.plug.telegram.TelegramSegment(text, *, bold=False, italic=False, underline=False, strike=False, code=False, pre=False, link=None, mention=None)

Bases: immp.core.message.Segment

Plug-friendly representation of Telegram message formatting.

classmethod to_html(telegram, segment)

Convert a Segment into HTML suitable for Telegram’s automatic parsing.

Parameters
  • telegram (TelegramPlug) – Related plug instance to cross-reference users.
  • segment (Segment) – Message segment created by another plug.
Returns

HTML-formatted string.

Return type

str

class immp.plug.telegram.TelegramRichText(segments=None)

Bases: immp.core.message.RichText

Wrapper for Telegram-specific parsing of formatting.

classmethod from_bot_entities(telegram, text, entities)

Convert a string annotated by Telegram’s entities to RichText.

Parameters
  • telegram (TelegramPlug) – Related plug instance that provides the text.
  • text (str) – Plain text without formatting.
  • entities (dict list) – List of Telegram API MessageEntity objects.
Returns

Parsed rich text container.

Return type

TelegramRichText

classmethod from_proto_entities(telegram, text, entities)

Convert a string annotated by Telegram’s entities to RichText.

Parameters
  • telegram (TelegramPlug) – Related plug instance that provides the text.
  • text (str) – Plain text without formatting.
  • entities (telethon.types.TypeMessageEntity list) – List of Telegram entity objects.
Returns

Parsed rich text container.

Return type

TelegramRichText

class immp.plug.telegram.TelegramFile(title=None, type_=<Type.unknown: 0>, source=None)

Bases: immp.core.message.File

File attachment originating from Telegram.

classmethod from_id(telegram, id_, type_=<Type.unknown: 0>, name=None)

Generate a file using the bot API URL for a Telegram file.

Parameters
  • telegram (TelegramPlug) – Related plug instance that provides the file.
  • id (str) – File ID as provided in the bot API, or constructed from a raw MTProto file.
  • type (File.Type) – Corresponding file type.
  • name (str) – Original filename, if available for the file format.
Returns

Parsed file object.

Return type

TelegramFile

class immp.plug.telegram.TelegramMessage(*, text=None, user=None, edited=False, action=False, reply_to=None, joined=None, left=None, title=None, attachments=None, raw=None)

Bases: immp.core.message.Message

Message originating from Telegram.

classmethod from_bot_message(telegram, json)

Convert an API message dict to a Message.

Parameters
  • telegram (TelegramPlug) – Related plug instance that provides the event.
  • json (dict) – Telegram API message object.
Returns

Parsed message object.

Return type

TelegramMessage

classmethod from_bot_update(telegram, update)

Convert an API update dict to a Message.

Parameters
  • telegram (TelegramPlug) – Related plug instance that provides the event.
  • update (dict) – Telegram API update object.
Returns

Parsed message object.

Return type

TelegramMessage

classmethod from_proto_message(telegram, message)

Convert a Telegram message event to a Message.

Parameters
  • telegram (TelegramPlug) – Related plug instance that provides the event.
  • message (telethon.tl.custom.Message) – Received message from an event or get request.
Returns

Parsed message object.

Return type

TelegramMessage

class immp.plug.telegram.TelegramPlug(name, config, host)

Bases: immp.core.util.HTTPOpenable, immp.core.plug.Plug

Plug for a Telegram bot.