ptbtest package

Module contents

exception ptbtest.BadUserException(error=None)

Bases: exceptions.Exception

exception ptbtest.BadChatException(error=None)

Bases: exceptions.Exception

exception ptbtest.BadMessageException(error=None)

Bases: exceptions.Exception

exception ptbtest.BadBotException(error=None)

Bases: exceptions.Exception

class ptbtest.Mockbot(username='MockBot', **kwargs)

Bases: telegram.base.TelegramObject

The Mockbot is a fake telegram-bot that does not require a token or a connection to the telegram servers. It’s used to mimmick all methods of python-telegram-bot instance, but never contact the telegram servers. All methods as described in telegram.Bot are functional and described here are only the special methods added for testing functionality

sent_messages

[dict<sent message>] – A list of every message sent with this bot.

It will contain the data dict usually passed to the methods actually sending data to telegram. With an added field named method which will contain the method used to send this message to the server.

Examples

A call to sendMessage(1, "hello") will return the following:

{'text': 'hello', 'chat_id': 1, 'method': 'sendMessage'}

A call to editMessageText(text="test 2", inline_message_id=404, disable_web_page_preview=True):

{'inline_message_id': 404, 'text': 'test 2', 'method': 'editMessageText', 'disable_web_page_preview': True}
Parameters:username (Optional[str]) – Username for this bot. Defaults to ‘MockBot’
answerCallbackQuery(callback_query_id, text=None, show_alert=False, url=None, cache_time=None, timeout=None, **kwargs)
answerInlineQuery(inline_query_id, results, cache_time=300, is_personal=None, next_offset=None, switch_pm_text=None, switch_pm_parameter=None, timeout=None, **kwargs)
answer_callback_query(callback_query_id, text=None, show_alert=False, url=None, cache_time=None, timeout=None, **kwargs)
answer_inline_query(inline_query_id, results, cache_time=300, is_personal=None, next_offset=None, switch_pm_text=None, switch_pm_parameter=None, timeout=None, **kwargs)
static de_json(data, bot)
editMessageCaption(*args, **kwargs)
editMessageReplyMarkup(*args, **kwargs)
editMessageText(*args, **kwargs)
edit_message_caption(*args, **kwargs)
edit_message_reply_markup(*args, **kwargs)
edit_message_text(*args, **kwargs)
first_name
forwardMessage(*args, **kwargs)
forward_message(*args, **kwargs)
getChat(chat_id, timeout=None, **kwargs)
getChatAdministrators(chat_id, timeout=None, **kwargs)
getChatMember(chat_id, user_id, timeout=None, **kwargs)
getChatMembersCount(chat_id, timeout=None, **kwargs)
getFile(file_id, timeout=None, **kwargs)
getGameHighScores(user_id, chat_id=None, message_id=None, inline_message_id=None, timeout=None, **kwargs)
getMe(timeout=None, **kwargs)
getUpdates(offset=None, limit=100, timeout=0, network_delay=None, read_latency=2.0, **kwargs)
getUserProfilePhotos(user_id, offset=None, limit=100, timeout=None, **kwargs)
get_chat(chat_id, timeout=None, **kwargs)
get_chat_administrators(chat_id, timeout=None, **kwargs)
get_chat_member(chat_id, user_id, timeout=None, **kwargs)
get_chat_members_count(chat_id, timeout=None, **kwargs)
get_file(file_id, timeout=None, **kwargs)
get_game_high_scores(user_id, chat_id=None, message_id=None, inline_message_id=None, timeout=None, **kwargs)
get_me(timeout=None, **kwargs)
get_updates(offset=None, limit=100, timeout=0, network_delay=None, read_latency=2.0, **kwargs)
get_user_profile_photos(user_id, offset=None, limit=100, timeout=None, **kwargs)
id
info(func)
insertUpdate(update)

This inserts an update into the the bot’s storage. these will be retreived on a call to getUpdates which is used by the telegram.Updater. This way the updater can function without any modifications.

Parameters:update (telegram.Update) – The update to insert in the queue.
kickChatMember(chat_id, user_id, timeout=None, **kwargs)
kick_chat_member(chat_id, user_id, timeout=None, **kwargs)
last_name
leaveChat(chat_id, timeout=None, **kwargs)
leave_chat(chat_id, timeout=None, **kwargs)
message(func)
name
reset()

Resets the sent_messages property to an empty list.

sendAudio(*args, **kwargs)
sendChatAction(*args, **kwargs)
sendContact(*args, **kwargs)
sendDocument(*args, **kwargs)
sendGame(*args, **kwargs)
sendLocation(*args, **kwargs)
sendMessage(*args, **kwargs)
sendPhoto(*args, **kwargs)
sendSticker(*args, **kwargs)
sendVenue(*args, **kwargs)
sendVideo(*args, **kwargs)
sendVoice(*args, **kwargs)
send_audio(*args, **kwargs)
send_chat_action(*args, **kwargs)
send_contact(*args, **kwargs)
send_document(*args, **kwargs)
send_game(*args, **kwargs)
send_location(*args, **kwargs)
send_message(*args, **kwargs)
send_photo(*args, **kwargs)
send_sticker(*args, **kwargs)
send_venue(*args, **kwargs)
send_video(*args, **kwargs)
send_voice(*args, **kwargs)
sent_messages
setGameScore(user_id, score, chat_id=None, message_id=None, inline_message_id=None, edit_message=None, force=None, disable_edit_message=None, timeout=None, **kwargs)
setWebhook(webhook_url=None, certificate=None, timeout=None, **kwargs)
set_game_score(user_id, score, chat_id=None, message_id=None, inline_message_id=None, edit_message=None, force=None, disable_edit_message=None, timeout=None, **kwargs)
set_webhook(webhook_url=None, certificate=None, timeout=None, **kwargs)
to_dict()
unbanChatMember(chat_id, user_id, timeout=None, **kwargs)
unban_chat_member(chat_id, user_id, timeout=None, **kwargs)
updates
username
class ptbtest.UserGenerator

Bases: ptbtest.ptbgenerator.PtbGenerator

User generator class. placeholder for random names and mainly used via it’s get_user() method

FIRST_NAMES = ['James', 'Mary', 'John', 'Patricia', 'Robert', 'Jennifer', 'Michael', 'Elizabeth', 'William', 'Linda', 'David', 'Barbara', 'Richard', 'Susan', 'Joseph', 'Jessica', 'Thomas', 'Margaret', 'Charles', 'Sarah']
LAST_NAMES = ['Smith', 'Johnson', 'Williams', 'Jones', 'Brown', 'Davis', 'Miller', 'Wilson', 'Moore', 'Taylor']
get_user(first_name=None, last_name=None, username=None, id=None)

Returns a telegram.User object with the optionally given name(s) or username If any of the arguments are omitted the names will be chosen randomly and the username will be generated as first_name + last_name.

Parameters:
  • first_name (Optional[str]) – First name for the returned user.
  • last_name (Optional[str]) – Lst name for the returned user.
  • username (Optional[str]) – Username for the returned user.
Returns:

A telegram user object

Return type:

telegram.User

class ptbtest.ChatGenerator

Bases: ptbtest.ptbgenerator.PtbGenerator

Chat generator class. placeholder for random names and mainly used via it’s get_chat() method

GROUPNAMES = ['Frustrated Vagabonds', 'Heir Apparents', 'Walky Talky', 'Flirty Crowns', 'My Amigos']
get_chat(cid=None, type='private', title=None, username=None, user=None, all_members_are_administrators=False)

Returns a telegram.Chat object with the optionally given type or username If any of the arguments are omitted the names will be chosen randomly and the username will be generated as first_name + last_name.

When called without arguments will return a telegram.Chat object for a private chat with a randomly generated user.

Parameters:
  • type (str) – Type of chat can be private, group, supergroup or channel.
  • title (Optional[str]) – Title for the group/supergroup/channel/
  • username (Optional[str]) – Username for the private/supergroup/channel.
  • user (Optional[telegram.User]) – If given a private chat for the supplied user will be generated.
  • all_members_are_administrators – set’s this flag for a group.
Returns:

A telegram Chat object.

Return type:

telegram.Chat

class ptbtest.MessageGenerator(bot=None)

Bases: ptbtest.ptbgenerator.PtbGenerator

Message generator class.

bot

ptbtest.Mockbot – Bot to encode with the messages

Parameters:bot (Optional[ptbtest.Mockbot]) – supply your own for a custom botname
get_channel_post(*args, **kwargs)
Parameters:
  • chat (Optional[telegram.Chat]) – Chat with type=’channel’ to use with this update
  • user (Optional[telegram.User]) – User for the update. None if omitted
  • **kwargs – See get_message
Returns:

A telegram update object containing a :py:class:`telegram.Message.

Return type:

telegram.Update

get_edited_channel_post(*args, **kwargs)
Parameters:
  • channel_post (Optional(telegram.Message)) – The edited_channel_post will the same user, chat and message_id
  • **kwargs – See get_message for the full list
Returns:

A telegram update object containing a telegram.Message.

Return type:

telegram.Update

get_edited_message(*args, **kwargs)
Parameters:
  • message (Optional(telegram.Message)) – The edited_message will have the same user, chat and message_id
  • **kwargs – See get_message for the full list
Returns:

A telegram update object containing a telegram.Message.

Return type:

telegram.Update

get_message(*args, **kwargs)

When called without arguments will return an update object for a message from a private chat with a random user. for modifiers see args.

Notes

whenever a list of telegram.PhotoSize objects is expected but not supplied it will always be a list with two random sizes between 40-400 pixels. These will not be valid file id’s

Parameters:
  • user (Optional[telegram.User]) – User the message is from (m.from_user)
  • chat (Optional[telegram.Chat]) – Chat the message is from (m.chat).
  • private (Optional[bool]) – If the message is private (optionally with the supplied user) default=True
  • text (str) – The text for the message, can make use of markdown or html, make sure to specify with parse_mode
  • parse_mode (Optional[str]) – “HTML” or “Markdown” parses the text and fills entities
  • entities (Optional[lst(telegram.MessageEntity)]) – when text and parsemode are set this will be filled with the entities in the text. # noqa: E501
  • reply_to_message (Optional[telegram.Message) – Messages this one is a reply to
  • forward_from (Optional[telegram.User) – User this message is forwarded from
  • forward_from_chat (Optional[telegram.Chat]) – channel this message is forwarded from
  • forward_date (Optional[int]) – Original sent date
  • forward_from_message_id (Optional[int]) – message id from forwarded channel post.
  • new_chat_member (Optional[telegram.User]) – New member for this chat
  • left_chat_member (Optional[telegram.User]) – Member left this chat
  • new_chat_title (Optional[str]) – New title for the chat
  • new_chat_photo (Optional[lst(telegram.Photosize)] or True) – New picture for the group
  • pinned_message (Optional[telegram.Message]) – Pinned message for supergroups
  • channel_chat_created (Optional[True]) – Not integrated
  • migrate_from_chat_id (Optional[int]) – Not integrated
  • migrate_to_chat_id (Optional[int]) – Not integrated
  • supergroup_chat_created (Optional[True]) – Not integrated
  • group_chat_created (Optional[True]) – Not integrated
  • delete_chat_photo (Optional[True]) – Not integrated
  • venue (Optional[telegram.Venue or True]) – Either the right object or True to generate one
  • location (optional[telegram.Location or True]) – Either the right object or True to generate one
  • contact (optional[telegram.Contact or True]) – Either the right object or True to generate one
  • (Optional[str or True] (caption) – Either the right object or True to generate one
  • voice (Optional[telegram.Voice or True]) – Either the right object or True to generate one
  • video (Optional[telegram.Video or True]) – Either the right object or True to generate one
  • sticker (Optional[telegram.Sticker] or True) – Either the right object or True to generate one
  • photo (Optional[lst(telegram.PhotoSize) or True]) – Either the right object or True to generate one
  • document (Optional[telegram.Document or True]) – Either the right object or True to generate one
  • audio (Optional[telegram.Audio] or True) – Either the right object or True to generate one
Returns:

A telegram update object containing a telegram.Message.

Return type:

telegram.Update

exception ptbtest.BadMarkupException(error=None)

Bases: exceptions.Exception

class ptbtest.CallbackQueryGenerator(bot=None)

Bases: ptbtest.ptbgenerator.PtbGenerator

Callback query generator class.

bot

ptbtest.Mockbot – Bot to encode with the messages

Parameters:bot (Optional[ptbtest.Mockbot]) – supply your own for a custom botname
get_callback_query(*args, **kwargs)

Returns a telegram.Update object containing a callback_query.

Notes

One of message and inline_message_id must be present One of data and game_short_name must be present

Parameters:
  • user (Optional[telegram.User]) – User that initiated the callback_query
  • chat_instance (Optional[str]) – unique identifier, not used
  • message (Optional[telegram.Message]) – Message the callback_query button belongs to
  • inline_message_id (Optional[str]) – Message the callback_query button belongs to
  • data (Optional[string]) – Data attached to the button
  • game_short_name (Optional[str]) – game identifier with this button
Returns:

containing a telegram.CallbackQuery

Return type:

telegram.Update

exception ptbtest.BadCallbackQueryException(error=None)

Bases: exceptions.Exception

class ptbtest.InlineQueryGenerator(bot=None)

Bases: ptbtest.ptbgenerator.PtbGenerator

Callback query generator class.

bot

ptbtest.Mockbot – Bot to encode with the messages

Parameters:bot (Optional[ptbtest.Mockbot]) – supply your own for a custom botname
get_chosen_inline_result(*args, **kwargs)

Returns a telegram.Update object containing a inline_query.

Parameters:
  • result_id (str) – The result_id belonging to this chosen result
  • inline_message_id (Optional[str]) – Of omitted will be generated
  • location (Optional[telegram.Location or True]) – simulates a location
  • query (Optional[str]) – The query used to send this query
  • user (Optional[telegram.User) – If omitted will be randomly generated
Returns:

an update containing a telegram.ChosenInlineResult

Return type:

telegram.Update

get_inline_query(*args, **kwargs)

Returns a telegram.Update object containing a inline_query.

Parameters:
  • location (Optional[telegram.Location or True]) – simulates a location
  • offset (Optional[str]) –
  • query (Optional[str]) –
  • user (Optional[telegram.User) – If omitted will be randomly generated
Returns:

an update containing a telegram.InlineQuery

Return type:

telegram.Update