Telegram testing utils#
This module defines functions used to test Telegram interface.
- replace_click_button(happy_path)[source]#
Replace all _ClickButton instances in happy_path. This allows using
check_happy_path()
instead of :py:meth:~dff.utils.testing.telegram.TelegramTesting.check_happy_path`.- Returns:
A happy_path with all _ClickButton replaced with payload values of the buttons.
- class TelegramTesting(pipeline, api_credentials=None, session_file=None, client=None, bot_username=None, bot=None)[source]#
Bases:
object
Defines functions for testing.
- Parameters:
pipeline (
Pipeline
) – Pipeline with the telegram messenger interface. Required forsend_and_check()
andcheck_happy_path()
with run_bot=Trueapi_credentials (
Optional
[Tuple
[int
,str
]]) – Telegram API id and hash. Obtainable via https://core.telegram.org/api/obtaining_api_id.session_file (
Optional
[str
]) – A telethon session file. Obtainable by connecting totelethon.TelegramClient
and entering phone number and code.client (
Optional
[TelegramClient
]) – An alternative to passing api_credentials and session_file.bot_username (
Optional
[str
]) – Either a link to the bot user or its handle. Used to determine whom to talk with as a client.bot (
Optional
[User
]) – An alternative to passing bot_username. Result of callingget_bot_user()
with bot_username as parameter.
- client#
Telegram client (not bot). Needed to verify bot replies.
- bot#
Bot user (to know whom to send messages to from client).
- async send_message(message, last_bot_messages)[source]#
Send a message from client to bot. If the message contains callback_query, only press the button, ignore other fields.
- Parameters:
message (
TelegramMessage
) – Message to send.last_bot_messages (
List
[Message
]) – The last bot response. Accepts a list because messages with multiple fields are split in telegram. Can only contain one keyboard in the list. Used to determine which button to press when message contains_ClickButton
.
- async static parse_responses(responses, file_download_destination)[source]#
Convert a list of bot responses into a single message. This function accepts a list because messages with multiple attachments are split.
- Parameters:
responses (
List
[Message
]) – A list of bot responses that are considered to be a single message.file_download_destination – A directory to download sent media to.
- Return type:
- run_bot_loop()[source]#
A context manager that returns a function to run one polling loop of a messenger interface.
- async send_and_check(message, file_download_destination=None)[source]#
Send a message from a bot, receive it as client, verify it.
- Parameters:
message (
Message
) – Message to send and check.file_download_destination – Temporary directory (used to download sent files). Defaults to
tempfile.TemporaryDirectory
.
- async check_happy_path(happy_path, file_download_destination=None, run_bot=True)[source]#
Play out a dialogue with the bot. Check that the dialogue is correct.
- Parameters:
happy_path – Expected dialogue
file_download_destination – Temporary directory (used to download sent files)
run_bot (
bool
) – Whether a bot inside pipeline should be running (disable this to test non-async bots)
- Returns: