From a07c7bd0d07b3e21ccf8bcfa2f1a4c1c1bf5681f Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 8 Jun 2017 14:00:27 +0200 Subject: [PATCH] Move InteractiveClient to examples/ (and stop shipping it to pip) --- README.rst | 39 +++++++++-------- telethon/__init__.py | 1 - .../interactive_telegram_client.py | 43 +++++++++++-------- try_telethon.py | 7 ++- 4 files changed, 50 insertions(+), 40 deletions(-) rename {telethon => telethon_examples}/interactive_telegram_client.py (89%) diff --git a/README.rst b/README.rst index d9aa6c24..046072f8 100755 --- a/README.rst +++ b/README.rst @@ -80,25 +80,28 @@ If you've installed Telethon via pip, launch an interactive python3 session and .. code:: python - >>> from telethon import InteractiveTelegramClient + >>> from telethon import TelegramClient + >>> api_id = 12345 + >>> api_hash = '0123456789abcdef0123456789abcdef' >>> # 'session_id' can be 'your_name'. It'll be saved as your_name.session - >>> # Also (obviously) replace the api_id and api_hash with your values + ... client = TelegramClient('session_id', api_id, api_hash) + >>> client.connect() + True + >>> + >>> if not client.is_user_authorized(): + >>> client.send_code_request('+34600000000') + >>> client.sign_in('+34600000000', input('Enter code: ')) ... - >>> client = InteractiveTelegramClient('session_id', '+34600000000', - ... api_id=12345, api_hash='0123456789abcdef0123456789abcdef') - - ================== - = Initialization = - ================== - Initializing interactive example... - Connecting to Telegram servers... - >>> client.run() + >>> # Now you can use the connected client as you wish + >>> dialogs, entities = client.get_dialogs(10) + >>> print('\n'.join('{}. {}'.format(i, str(e)) + ... for i, e in enumerate(entities))) If, on the other hand, you've installed Telethon manually, head to the ``api/`` directory and create a copy of the ``settings_example`` file, naming it ``settings`` (lowercase!). Then fill the file with the corresponding values (your ``api_id``, ``api_hash`` and phone number in international format). -Then, simply run ``python3 try_telethon.py`` to start the interactive example. +Then, simply run ``./try_telethon.py`` to start the interactive example. .. _Using Telethon: @@ -106,9 +109,9 @@ Using Telethon ============== If you really want to learn how to use Telethon, it is **highly advised** that you take a look to the -`InteractiveTelegramClient `_ file -and check how it works. This file contains everything you'll need to build -your own application, since it shows, among other things: +`InteractiveTelegramClient `_ +file and check how it works. This file contains everything you'll need to +build your own application, since it shows, among other things: 1. Authorizing the user for the first time. 2. Support to enter the 2-steps-verification code. @@ -122,6 +125,8 @@ disposal, please check the `official Telethon documentation `_. There you'll find a list of all the methods, types and available constructors. +More examples are also available under the ``telethon_examples/`` folder. + Common errors ------------- @@ -228,9 +233,9 @@ Once this is done, pass the proxy settings to the ``TelegramClient`` constructor .. code:: python - >>> from telethon import InteractiveTelegramClient + >>> from telethon import TelegramClient >>> import socks - >>> client = InteractiveTelegramClient('session_id', '+34600000000', + >>> client = TelegramClient('session_id', ... api_id=12345, api_hash='0123456789abcdef0123456789abcdef', ... proxy=(socks.SOCKS5, 'localhost', 4444)) diff --git a/telethon/__init__.py b/telethon/__init__.py index 1f4a9dce..db442a11 100644 --- a/telethon/__init__.py +++ b/telethon/__init__.py @@ -1,5 +1,4 @@ from .errors import * from .telegram_bare_client import TelegramBareClient from .telegram_client import TelegramClient -from .interactive_telegram_client import InteractiveTelegramClient from . import tl diff --git a/telethon/interactive_telegram_client.py b/telethon_examples/interactive_telegram_client.py similarity index 89% rename from telethon/interactive_telegram_client.py rename to telethon_examples/interactive_telegram_client.py index 281fef03..a018b54b 100644 --- a/telethon/interactive_telegram_client.py +++ b/telethon_examples/interactive_telegram_client.py @@ -1,10 +1,10 @@ import shutil from getpass import getpass -from . import TelegramClient -from .errors import RPCError -from .tl.types import UpdateShortChatMessage, UpdateShortMessage -from .utils import get_display_name +from telethon import TelegramClient +from telethon.errors import RPCError +from telethon.tl.types import UpdateShortChatMessage, UpdateShortMessage +from telethon.utils import get_display_name # Get the (current) number of lines in the terminal cols, rows = shutil.get_terminal_size() @@ -40,6 +40,13 @@ def bytes_to_string(byte_count): class InteractiveTelegramClient(TelegramClient): + """Full featured Telegram client, meant to be used on an interactive + session to see what Telethon is capable off - + + This client allows the user to perform some basic interaction with + Telegram through Telethon, such as listing dialogs (open chats), + talking to people, downloading media, and receiving updates. + """ def __init__(self, session_user_id, user_phone, api_id, api_hash, proxy=None): print_title('Initialization') @@ -130,14 +137,10 @@ class InteractiveTelegramClient(TelegramClient): print('Available commands:') print(' !q: Quits the current chat.') print(' !Q: Quits the current chat and exits.') - print( - ' !h: prints the latest messages (message History) of the chat.') - print( - ' !up : Uploads and sends a Photo located at the given path.') - print( - ' !uf : Uploads and sends a File document located at the given path.') - print( - ' !dm : Downloads the given message Media (if any).') + print(' !h: prints the latest messages (message History).') + print(' !up : Uploads and sends the Photo from path.') + print(' !uf : Uploads and sends the File from path.') + print(' !dm : Downloads the given message Media (if any).') print(' !dp: Downloads the current dialog Profile picture.') print() @@ -155,8 +158,10 @@ class InteractiveTelegramClient(TelegramClient): # First retrieve the messages and some information total_count, messages, senders = self.get_message_history( entity, limit=10) - # Iterate over all (in reverse order so the latest appears the last in the console) - # and print them in "[hh:mm] Sender: Message" text format + + # Iterate over all (in reverse order so the latest appear + # the last in the console) and print them with format: + # "[hh:mm] Sender: Message" for msg, sender in zip( reversed(messages), reversed(senders)): # Get the name of the sender if any @@ -165,16 +170,18 @@ class InteractiveTelegramClient(TelegramClient): # Format the message content if getattr(msg, 'media', None): self.found_media.add(msg) - content = '<{}> {}'.format( # The media may or may not have a caption - msg.media.__class__.__name__, - getattr(msg.media, 'caption', '')) + # The media may or may not have a caption + caption = getattr(msg.media, 'caption', '') + content = '<{}> {}'.format( + type(msg.media).__name__, caption) + elif hasattr(msg, 'message'): content = msg.message elif hasattr(msg, 'action'): content = str(msg.action) else: # Unknown message, simply print its class name - content = msg.__class__.__name__ + content = type(msg).__name__ # And print it to the user sprint('[{}:{}] (ID={}) {}: {}'.format( diff --git a/try_telethon.py b/try_telethon.py index 3b47551d..6793cdd2 100755 --- a/try_telethon.py +++ b/try_telethon.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 import traceback -from telethon.interactive_telegram_client import (InteractiveTelegramClient, - print_title) +from telethon_examples.interactive_telegram_client \ + import InteractiveTelegramClient def load_settings(path='api/settings'): @@ -40,6 +40,5 @@ if __name__ == '__main__': type(e), e, traceback.format_exc())) finally: - print_title('Exit') - print('Thanks for trying the interactive example! Exiting...') client.disconnect() + print('Thanks for trying the interactive example! Exiting...')