aiotg reference

Submodules

aiotg.bot module

class aiotg.bot.Bot(api_token, api_timeout=60, botan_token=None, name=None)[source]

Bases: object

Telegram bot framework designed for asyncio

Parameters:
  • api_token (str) – Telegram bot token, ask @BotFather for this
  • api_timeout (int) – Timeout for long polling
  • botan_token (str) – Token for http://botan.io
  • name (str) – Bot name
api_call(method, **params)[source]

Call Telegram API.

See https://core.telegram.org/bots/api for reference.

Parameters:
  • method (str) – Telegram API method
  • params – Arguments for the method call
callback(callback)[source]

Set callback for callback queries

Example:
>>> @bot.callback
>>> def echo(chat, cq):
>>>     return cq.answer()
channel(channel_name)[source]

Construct a Chat object used to post to channel

Parameters:channel_name (str) – Channel name
command(regexp)[source]

Register a new command

Parameters:regexp (str) – Regular expression matching the command to register
Example:
>>> @bot.command(r"/echo (.+)")
>>> def echo(chat, match):
>>>     return chat.reply(match.group(1))
default(callback)[source]

Set callback for default command that is called on unrecognized commands for 1-to-1 chats

Example:
>>> @bot.default
>>> def echo(chat, message):
>>>     return chat.reply(message["text"])
download_file(file_path, range=None)[source]

Dowload a file from Telegram servers

edit_message_text(chat_id, message_id, text, **options)[source]

Edit a text message in a chat

Parameters:
  • chat_id (int) – ID of the chat the message to edit is in
  • message_id (int) – ID of the message to edit
  • text (str) – Text to edit the message to
  • options – Additional API options
get_file(file_id)[source]

Get basic information about a file and prepare it for downloading.

Parameters:file_id (int) – File identifier to get information about
Returns:File object (see https://core.telegram.org/bots/api#file)
group(group_id)[source]

Construct a Chat object used to post group messages

Parameters:group_id (str) – Group chat id
handle(msg_type)[source]

Set handler for specific message type

Example:
>>> @bot.handle("audio")
>>> def handle(chat, audio):
>>>     pass
inline(callback)[source]

Set callback for inline queries

Example:
>>> @bot.inline
>>> def echo(iq):
>>>     return iq.answer([
>>>         {"type": "text", "title": "test", "id", "0"}
>>>     ])
loop()[source]

Return bot’s main loop as coroutine. Use with asyncio.

Example:
>>> loop = asyncio.get_event_loop()
>>> loop.run_until_complete(bot.loop())

or

>>> loop = asyncio.get_event_loop()
>>> loop.create_task(bot.loop())
private(user_id)[source]

Construct a Chat object used to post direct messages

Parameters:user_id (str) – User id
run()[source]

Convenience method for running bots

Example:
>>> if __name__ == '__main__':
>>>     bot.run()
send_message(chat_id, text, **options)[source]

Send a text message to chat

Parameters:
stop()[source]
track(message, name='Message')[source]

Track message using http://botan.io Set botak_token to make it work

class aiotg.bot.CallbackQuery(bot, src)[source]

Bases: object

answer()[source]
class aiotg.bot.InlineQuery(bot, src)[source]

Bases: object

Incoming inline query See https://core.telegram.org/bots/api#inline-mode for details

answer(results, **options)[source]
class aiotg.bot.TgBot(*args, **kwargs)[source]

Bases: aiotg.bot.Bot

class aiotg.bot.TgInlineQuery(*args, **kwargs)[source]

Bases: aiotg.bot.InlineQuery

aiotg.chat module

class aiotg.chat.Chat(bot, chat_id, chat_type='private', src_message=None)[source]

Bases: object

Wrapper for telegram chats, passed to most callbacks

edit_text(message_id, text, markup={}, parse_mode=None)[source]

Edit the message in this chat.

Parameters:
  • message_id (int) – ID of the message to edit
  • text (str) – Text to edit the message to
  • markup (dict) – Markup options
  • parse_mode (str) – Text parsing mode ("Markdown", "HTML" or None)
forward_message(from_chat_id, message_id)[source]

Forward a message from another chat to this chat.

Parameters:
  • from_chat_id (int) – ID of the chat to forward the message from
  • message_id (int) – ID of the message to forward
static from_message(bot, message)[source]

Create a Chat object from a message.

Parameters:
  • bot (Bot) – Bot object the message and chat belong to
  • message (dict) – Message to base the object on
Returns:

A chat object based on the message

is_group()[source]

Check if this chat is a group.

Returns:True if this chat is a group, False otherwise
reply(text, markup={}, parse_mode=None)[source]

Reply to the message this Chat object is based on.

Parameters:
  • text (str) – Text of the message to send
  • markup (dict) – Markup options
  • parse_mode (str) – Text parsing mode ("Markdown", "HTML" or None)
send_audio(audio, **options)[source]

Send an mp3 audio file to the chat.

Parameters:
Example:
>>> with open("foo.mp3", "rb") as f:
>>>     chat.send_audio(f, performer="Bar Fighters", title="Eversong")
send_chat_action(action)[source]

Send a chat action, to tell the user that something is happening on the bot’s side.

Available actions:

  • typing for text messages
  • upload_photo for photos
  • record_video and upload_video for videos
  • record_audio and upload_audio for audio files
  • upload_document for general files
  • find_location for location data
Parameters:action (str) – Type of action to broadcast
send_document(document, caption='', **options)[source]

Send a general file.

Parameters:
Example:
>>> with open("file.doc", "rb") as f:
>>>     chat.send_document(f)
send_location(latitude, longitude, **options)[source]

Send a point on the map.

Parameters:
send_photo(photo, caption='', **options)[source]

Send a photo to the chat.

Parameters:
Example:
>>> with open("foo.png", "rb") as f:
>>>     chat.send_photo(f, caption="Would you look at this!")
send_sticker(**options)
send_text(text, **options)[source]

Send a text message to the chat.

Parameters:
send_video(video, caption='', **options)[source]

Send an mp4 video file to the chat.

Parameters:
Example:
>>> with open("foo.mp4", "rb") as f:
>>>     chat.send_video(f)
send_voice(voice, **options)[source]

Send an OPUS-encoded .ogg audio file.

Parameters:
Example:
>>> with open("voice.ogg", "rb") as f:
>>>     chat.send_voice(f)
class aiotg.chat.Sender[source]

Bases: dict

A small wrapper for sender info, mostly used for logging

class aiotg.chat.TgChat(*args, **kwargs)[source]

Bases: aiotg.chat.Chat

class aiotg.chat.TgSender(*args, **kwargs)[source]

Bases: aiotg.chat.Sender

Module contents