PTBtest Modules¶
Submodules¶
Module contents¶
- exception ptbtest.BadUserException(error=None)¶
Bases:
Exception
- exception ptbtest.BadChatException(error=None)¶
Bases:
Exception
- exception ptbtest.BadMessageException(error=None)¶
Bases:
Exception
- exception ptbtest.BadBotException(error=None)¶
Bases:
Exception
- class ptbtest.Mockbot(username='MockBot', **kwargs)¶
Bases:
TelegramObjectThe Mockbot is a fake telegram-bot that does not require a token or a connection to Telegram’s servers. It’s used to mimmick all methods of a
python-telegram-botinstance, without a single network connection. All methods described intelegram.Botare functional and here are described only the special methods added for testing functionality.- Parameters
username (Optional[str]) – Username for this bot. Defaults to ‘MockBot’.
- property sent_messages¶
A list of every message sent with this bot.
It contains the data dict usually passed to the methods that actually send data to Telegram, with an added field named
methodwhich will contain the method used to send this message to the server.Examples
A call to
send_message(1, "hello")will return the following
{'text': 'hello', 'chat_id': 1, 'method': 'send_message'}A call to
edit_message_text(text="test 2", inline_message_id=404, disable_web_page_preview=True):results in
{'inline_message_id': 404, 'text': 'test 2', 'method': 'edit_message_text', 'disable_web_page_preview': True}
- property updates¶
Contains a list of updates received by the bot.
- reset()¶
Resets the
sent_messagesproperty to an empty list.
- info()¶
- property id¶
Return the bot’s ID.
- property first_name¶
Return the bot’s first name as a string.
- property last_name¶
Return the bot’s last name as a string.
- property username¶
Return the bot’s username as a string.
- property name¶
Return the username handle as a string.
- message()¶
- getMe(*args, **kwargs)¶
- get_me(**kwargs)¶
Return a bot, a
telegram.Userinstance.- Parameters
**kwargs – A dictionary with arguments for the bot
- Returns
An user or a bot with the supplied arguments.
- Return type
- sendMessage(*args, **kwargs)¶
- send_message(chat_id, text, parse_mode=None, disable_web_page_preview=None, disable_notification=False, reply_to_message_id=None, reply_markup=None, timeout=None, **kwargs)¶
- forwardMessage(*args, **kwargs)¶
- forward_message(chat_id, from_chat_id, message_id, disable_notification=False, timeout=None, **kwargs)¶
- sendPhoto(*args, **kwargs)¶
- send_photo(chat_id, photo, caption=None, disable_notification=False, reply_to_message_id=None, reply_markup=None, timeout=None, **kwargs)¶
- sendAudio(*args, **kwargs)¶
- send_audio(chat_id, unique_id, audio_unique_id, duration=None, performer=None, title=None, caption=None, disable_notification=False, reply_to_message_id=None, reply_markup=None, timeout=None, **kwargs)¶
- sendDocument(*args, **kwargs)¶
- send_document(chat_id, document, document_unique_id, filename=None, caption=None, disable_notification=False, reply_to_message_id=None, reply_markup=None, timeout=None, **kwargs)¶
- sendSticker(*args, **kwargs)¶
- send_sticker(chat_id, sticker, sticker_unique_id, width, height, is_animated, is_video, sticker_type, disable_notification=False, reply_to_message_id=None, reply_markup=None, timeout=None, **kwargs)¶
- sendVideo(*args, **kwargs)¶
- send_video(chat_id, video, video_unique_id, width, height, duration, caption=None, disable_notification=False, reply_to_message_id=None, reply_markup=None, timeout=None, **kwargs)¶
- sendVoice(*args, **kwargs)¶
- send_voice(chat_id, voice, voice_unique_id, duration=None, caption=None, disable_notification=False, reply_to_message_id=None, reply_markup=None, timeout=None, **kwargs)¶
- sendLocation(*args, **kwargs)¶
- send_location(chat_id, latitude, longitude, disable_notification=False, reply_to_message_id=None, reply_markup=None, timeout=None, **kwargs)¶
- sendVenue(*args, **kwargs)¶
- send_venue(chat_id, latitude, longitude, title, address, foursquare_id=None, disable_notification=False, reply_to_message_id=None, reply_markup=None, timeout=None, **kwargs)¶
- sendContact(*args, **kwargs)¶
- send_contact(chat_id, phone_number, first_name, last_name=None, disable_notification=False, reply_to_message_id=None, reply_markup=None, timeout=None, **kwargs)¶
- sendGame(*args, **kwargs)¶
- send_game(chat_id, game_short_name, timeout=None, **kwargs)¶
- sendChatAction(*args, **kwargs)¶
- send_chat_action(chat_id, action, timeout=None, **kwargs)¶
- answerInlineQuery(*args, **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)¶
- getUserProfilePhotos(*args, **kwargs)¶
- get_user_profile_photos(user_id, offset=None, limit=100, timeout=None, **kwargs)¶
- getFile(*args, **kwargs)¶
- get_file(file_id, timeout=None, **kwargs)¶
- kickChatMember(*args, **kwargs)¶
- kick_chat_member(chat_id, user_id, timeout=None, **kwargs)¶
- unbanChatMember(*args, **kwargs)¶
- unban_chat_member(chat_id, user_id, timeout=None, **kwargs)¶
- answerCallbackQuery(*args, **kwargs)¶
- answer_callback_query(callback_query_id, text=None, show_alert=False, url=None, cache_time=None, timeout=None, **kwargs)¶
- editMessageText(*args, **kwargs)¶
- edit_message_text(text, chat_id=None, message_id=None, inline_message_id=None, parse_mode=None, disable_web_page_preview=None, reply_markup=None, timeout=None, **kwargs)¶
- editMessageCaption(*args, **kwargs)¶
- edit_message_caption(chat_id=None, message_id=None, inline_message_id=None, caption=None, reply_markup=None, timeout=None, **kwargs)¶
- editMessageReplyMarkup(*args, **kwargs)¶
- edit_message_reply_markup(chat_id=None, message_id=None, inline_message_id=None, reply_markup=None, timeout=None, **kwargs)¶
- insertUpdate(*args, **kwargs)¶
- insert_update(update)¶
Inserts an update into the bot’s storage. These will be retrieved on a call to
get_updateswhich is used by thetelegram.Updater. This way the updater can function without any modifications.- Parameters
update (telegram.Update) – The update to insert in the queue.
- getUpdates(*args, **kwargs)¶
- get_updates(offset=None, limit=100, timeout=0, network_delay=None, read_latency=2.0, **kwargs)¶
Retrieve the updates contained in the bot’s storage.
- setWebhook(*args, **kwargs)¶
- set_webhook(webhook_url=None, certificate=None, timeout=None, **kwargs)¶
- leaveChat(*args, **kwargs)¶
- leave_chat(chat_id, timeout=None, **kwargs)¶
- getChat(*args, **kwargs)¶
- get_chat(chat_id, timeout=None, **kwargs)¶
- getChatAdministrators(*args, **kwargs)¶
- get_chat_administrators(chat_id, timeout=None, **kwargs)¶
- getChatMembersCount(*args, **kwargs)¶
- get_chat_members_count(chat_id, timeout=None, **kwargs)¶
- getChatMember(*args, **kwargs)¶
- get_chat_member(chat_id, user_id, timeout=None, **kwargs)¶
- setGameScore(*args, **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)¶
- getGameHighScore(*args, **kwargs)¶
- get_game_high_score(user_id, chat_id=None, message_id=None, inline_message_id=None, timeout=None, **kwargs)¶
- static de_json(data, bot)¶
Converts JSON data to a Telegram object.
- Parameters
data (dict[
str, …]) – The JSON data.bot (
telegram.Bot, optional) –The bot associated with this object. Defaults to
None, in which case shortcut methods will not be available.Changed in version 21.4: :paramref:`bot` is now optional and defaults to
None
- Returns
The Telegram object.
- to_dict()¶
Gives representation of object as
dict.Changed in version 20.0:
Now includes all entries of
api_kwargs.Attributes whose values are empty sequences are no longer included.
- Parameters
recursive (
bool, optional) –If
True, will convert any TelegramObjects (if found) in the attributes to a dictionary. Else, preserves it as an object itself. Defaults toTrue.New in version 20.0.
- Returns
dict
- api_kwargs: Mapping[str, Any]¶
- class ptbtest.UserGenerator¶
Bases:
PtbGeneratorUser generator class. Placeholder for random names and mainly used via its 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: Optional[str] = None, last_name: Optional[str] = None, username: Optional[str] = None, user_id: Optional[int] = None) User¶
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]) – Last name for the returned user.
username (Optional[str]) – Username for the returned user.
user_id (Optional[int]) – Id for the returned user.
- Returns
A telegram user object
- Return type
- class ptbtest.ChatGenerator¶
Bases:
PtbGeneratorChat 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(id: Optional[int] = None, type: Optional[Union[ChatType, str]] = None, title: Optional[str] = None, username: Optional[str] = None, user: Optional[User] = None, is_forum: bool = False, *, all_members_are_administrators: bool = False) Chat¶
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
id (Optional[int]) – ID of the returned chat.
type (Optional[Union[ChatType, str]]) – Type of the chat can be either telegram.constants.ChatType or the string literal (“private”, “group”, “supergroup”, “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.
is_forum (bool) – True, if the supergroup chat is a forum (has topics enabled). Default is False.
all_members_are_administrators (Optional[bool]) – Sets this flag for a group.
- Returns
A telegram.Chat object.
- Return type
- get_private_chat(id: Optional[int] = None, user: Optional[User] = None, username: Optional[str] = None, first_name: Optional[str] = None, last_name: Optional[str] = None) Chat¶
The convenient method for generating private chats. If any of the arguments are omitted the values will be chosen randomly.
- Parameters
id (Optional[int]) – ID of the returned chat.
user (Optional[telegram.User]) – If given, a private chat for the supplied user will be generated.
username (Optional[str]) – A username for the user.
first_name (Optional[str]) – The first name for the user.
last_name (Optional[str]) – The last name for the user.
- Returns
A telegram.Chat object with the ‘private’ ChatType.
- Return type
- get_channel_chat(id: Optional[int] = None, title: Optional[str] = None, username: Optional[str] = None) Chat¶
The convenient method for generating channel chats. If any of the arguments are omitted the values will be chosen randomly.
- Parameters
id (Optional[int]) – ID of the returned chat.
title (Optional[str]) – Title for the group/supergroup/channel.
username (Optional[str]) – Username for the private/supergroup/channel.
- Returns
A telegram.Chat object with ‘channel’ ChatType.
- Return type
- get_group_chat(id: Optional[int] = None, title: Optional[str] = None, username: Optional[str] = None, is_forum: bool = False, is_supergroup: bool = False, *, all_members_are_administrators: bool = False) Chat¶
The convenient method for generating [super]group chats. If any of the arguments are omitted the values will be chosen randomly.
- Parameters
id (Optional[int]) – ID of the returned chat.
title (Optional[str]) – Title for the group/supergroup/channel.
username (Optional[str]) – Username for the private/supergroup/channel.
is_forum (bool) – True, if the supergroup chat is a forum (has topics enabled). Default is False.
is_supergroup (bool) – True, if the chat must be supergroup. Default is False.
all_members_are_administrators (Optional[bool]) – Sets this flag for a group.
- Returns
A telegram.Chat object.
- Return type
- class ptbtest.MessageGenerator(bot=None)¶
Bases:
PtbGeneratorMessage generator class.
- bot¶
Bot to encode with the messages
- Type
- Parameters
bot (Optional[ptbtest.Mockbot]) – supply your own for a custom botname
- get_edited_channel_post(channel_post=None, **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
- get_channel_post(chat=None, user=None, **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
telegram.Message.- Return type
- get_edited_message(message=None, **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
- get_message(id=None, user=None, chat=None, private=True, reply_to_message=None, text=None, entities=None, audio=None, document=None, photo=None, sticker=None, video=None, voice=None, caption=None, contact=None, location=None, venue=None, new_chat_members=None, left_chat_member=None, new_chat_title=None, new_chat_photo=None, delete_chat_photo=False, group_chat_created=False, supergroup_chat_created=False, migrate_to_chat_id=None, migrate_from_chat_id=None, channel_chat_created=False, pinned_message=None, parse_mode=None, channel=False, via_bot=None)¶
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
id (Optional [int]) – The user’s id
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
reply_to_message (Optional[telegram.Message) – Messages this one is a reply to
text (str) – The text for the message, can make use of markdown or html, make sure to specify with parse_mode
entities (Optional[lst(telegram.MessageEntity)]) – when text and parsemode are set this will be filled with the entities in the text. # noqa: E501
audio (Optional[telegram.Audio] 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
photo (Optional[lst(telegram.PhotoSize) 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
video (Optional[telegram.Video or True]) – Either the right object or True to generate one
voice (Optional[telegram.Voice or True]) – Either the right object or True to generate one
caption (Optional[str 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
location (optional[telegram.Location or True]) – Either the right object or True to generate one
venue (Optional[telegram.Venue or True]) – Either the right object or True to generate one
new_chat_members (Optional[seq(telegram.User)]) – New member(s) 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
delete_chat_photo (Optional[True]) – Not integrated
group_chat_created (Optional[True]) – Not integrated
supergroup_chat_created (Optional[True]) – Not integrated
migrate_to_chat_id (Optional[int]) – Not integrated
migrate_from_chat_id (Optional[int]) – Not integrated
channel_chat_created (Optional[True]) – Not integrated
pinned_message (Optional[telegram.Message]) – Pinned message for supergroups
parse_mode (Optional[str]) – “HTML” or “Markdown” parses the text and fills entities
channel (Optional[str]) – If the message was sent via a channel
via_bot (Optional[bool]) – If the message was sent via a bot
- Returns
A telegram update object containing a
telegram.Message.- Return type
- exception ptbtest.BadMarkupException(error=None)¶
Bases:
Exception
- class ptbtest.CallbackQueryGenerator(bot=None)¶
Bases:
PtbGeneratorCallback query generator class.
- bot¶
Bot to encode with the messages
- Type
- Paramaters:
bot (Optional[ptbtest.Mockbot]): supply your own for a custom botname
- get_callback_query(user=None, chat_instance=None, message=None, data=None, inline_message_id=None, game_short_name=None)¶
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.
data (Optional[string]) – Data attached to the button.
inline_message_id (Optional[str]) – Message the callback_query button belongs to.
game_short_name (Optional[str]) – game identifier with this button.
- Returns
containing a
telegram.CallbackQuery- Return type
- exception ptbtest.BadCallbackQueryException(error=None)¶
Bases:
Exception
- class ptbtest.InlineQueryGenerator(bot=None)¶
Bases:
PtbGeneratorCallback query generator class.
- bot¶
Bot to encode with the messages
- Type
- Parameters
bot (Optional[ptbtest.Mockbot]) – supply your own for a custom botname
- get_inline_query(user=None, query=None, offset=None, location=None)¶
Returns a telegram.Update object containing a inline_query.
- Parameters
user (Optional[telegram.User) – If omitted, will be randomly generated
query (Optional[str]) – The query string to be used
offset (Optional[str]) – Offset of the results to be returned
location (Optional[telegram.Location or True]) – simulates a location
- Returns
an update containing a
telegram.InlineQuery- Return type
- get_chosen_inline_result(result_id=None, query=None, user=None, location=None, inline_message_id=None)¶
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