|
| 1 | +""" |
| 2 | +Run this file with python3.5+ in order to log in to Pyrogram with a Telegram user account |
| 3 | +interactively, creating a Pyrogram "session" file (json) in the process. |
| 4 | +It will try to log into the provided account like every regular Telegram client does, |
| 5 | +and you will be prompted for the login code. |
| 6 | +
|
| 7 | +Once the session is generated, you will be able to run the integration tests. |
| 8 | +
|
| 9 | +The configuration can be found in tests/bots.py and may be set via environment variables: |
| 10 | + - API_ID=12345 |
| 11 | + - API_HASH=abcdefghijklmnopqrstuvwxyz123456 |
| 12 | + - PHONE_NUMBER=+49123456789 |
| 13 | +
|
| 14 | +Read here about how to obtain the values for API_ID and API_HASH: |
| 15 | + https://docs.pyrogram.ml/start/ProjectSetup#api-keys |
| 16 | +
|
| 17 | +It is okay to use the provided defaults for API_ID and API_HASH, but you should use your own |
| 18 | +(or a spare) Telegram account's PHONE_NUMBER. |
| 19 | +
|
| 20 | +Feel free to contact @JosXa on Telegram if you encounter problems. |
| 21 | +""" |
| 22 | +import os |
| 23 | +import sys |
| 24 | + |
| 25 | +from tests.bots import get_bot, get_userbot |
| 26 | +from tests.integration import USERBOT_SESSION_ENV_VAR |
| 27 | + |
| 28 | +try: |
| 29 | + from tgintegration import InteractionClient |
| 30 | +except ImportError: |
| 31 | + print("You need to install the TgIntegration library to run tests:") |
| 32 | + print("$ pip install -r requirements-dev.txt") |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | +output_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), |
| 36 | + 'session_min_base64.txt') |
| 37 | + |
| 38 | +if __name__ == '__main__': |
| 39 | + userbot_info = get_userbot() |
| 40 | + bot_info = get_bot() |
| 41 | + peer_username = bot_info['username'] |
| 42 | + |
| 43 | + # Create a TgIntegration client (based on Pyrogram) |
| 44 | + client = InteractionClient( |
| 45 | + session_name="my_account", |
| 46 | + api_id=userbot_info['api_id'], |
| 47 | + api_hash=userbot_info['api_hash'] |
| 48 | + ) |
| 49 | + print("Starting client...") |
| 50 | + client.start() |
| 51 | + |
| 52 | + # Send an initial message to the bot to register it as a known peer |
| 53 | + print("Sending initial message to peer") |
| 54 | + try: |
| 55 | + client.send_message(peer_username, "Hi, I'll be your testing buddy for today.") |
| 56 | + except Exception as e: |
| 57 | + print("Could not contact {}. Please write a message to this peer manually and run the " |
| 58 | + "script again.\nException is: {}".format(peer_username, e)) |
| 59 | + sys.exit(1) |
| 60 | + |
| 61 | + # Export the minified and base64-encoded session to be used in environment variables |
| 62 | + b64_string = client.export_minimal_session_b64( |
| 63 | + output_file, |
| 64 | + include_peers=[peer_username] |
| 65 | + ).strip() |
| 66 | + |
| 67 | + print('Initialized successfully.\n\n' |
| 68 | + 'Now please set the {} test environment variable ' |
| 69 | + 'to the following:\n{}'.format(USERBOT_SESSION_ENV_VAR, b64_string)) |
| 70 | + print('\nThis value was also written to\n{}'.format(output_file)) |
| 71 | + print("\nIf you're using PyCharm, copy the key to clipboard and set the " |
| 72 | + "{} environment variable here:".format(USERBOT_SESSION_ENV_VAR)) |
| 73 | + print("Run| Edit Configurations| Defaults| Python tests| py.test") |
| 74 | + client.stop() |
0 commit comments