From b5f273c7537cad6ef209d4be25a7a347395c1c87 Mon Sep 17 00:00:00 2001 From: JosXa Date: Sat, 17 Feb 2018 13:12:40 +0100 Subject: [PATCH] Added !i for information to interactive telegram client --- .../interactive_telegram_client.py | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/telethon_examples/interactive_telegram_client.py b/telethon_examples/interactive_telegram_client.py index d45d2ff1..725cf54f 100644 --- a/telethon_examples/interactive_telegram_client.py +++ b/telethon_examples/interactive_telegram_client.py @@ -1,21 +1,20 @@ import os from getpass import getpass -from telethon import TelegramClient, ConnectionMode -from telethon.errors import SessionPasswordNeededError -from telethon.tl.types import ( - UpdateShortChatMessage, UpdateShortMessage, PeerChat -) from telethon.utils import get_display_name +from telethon import ConnectionMode, TelegramClient +from telethon.errors import SessionPasswordNeededError +from telethon.tl.types import (PeerChat, UpdateShortChatMessage, UpdateShortMessage) + def sprint(string, *args, **kwargs): """Safe Print (handle UnicodeEncodeErrors on some terminals)""" try: print(string, *args, **kwargs) except UnicodeEncodeError: - string = string.encode('utf-8', errors='ignore')\ - .decode('ascii', errors='ignore') + string = string.encode('utf-8', errors='ignore') \ + .decode('ascii', errors='ignore') print(string, *args, **kwargs) @@ -47,6 +46,7 @@ class InteractiveTelegramClient(TelegramClient): 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): """ @@ -190,6 +190,7 @@ class InteractiveTelegramClient(TelegramClient): print(' !d : Deletes a message by its id') print(' !dm : Downloads the given message Media (if any).') print(' !dp: Downloads the current dialog Profile picture.') + print(' !i: prints information about this chat..') print() # And start a while loop to chat @@ -234,8 +235,8 @@ class InteractiveTelegramClient(TelegramClient): # And print it to the user sprint('[{}:{}] (ID={}) {}: {}'.format( - msg.date.hour, msg.date.minute, msg.id, name, - content)) + msg.date.hour, msg.date.minute, msg.id, name, + content)) # Send photo elif msg.startswith('!up '): @@ -270,6 +271,23 @@ class InteractiveTelegramClient(TelegramClient): else: print('No profile picture found for this user!') + elif msg == '!i': + blacklist_attrs = ['from_reader', 'on_response', 'pretty_format', 'resolve', 'rpc_error', + 'serialize_bytes', 'serialize_datetime', 'stringify', 'to_dict', + 'constructor_id', 'subclass_of_id', 'confirm_received', 'result'] + interesting_attributes = [ + x for x in dir(entity) + if x.lower() not in blacklist_attrs and not (x.startswith('_') or x.startswith('__')) + ] + + longest_attr = max(len(x) for x in interesting_attributes) + + for attr in interesting_attributes: + print("{:<{width}} : {}".format( + attr, + getattr(entity, attr, ''), + width=longest_attr)) + # Send chat message (if any) elif msg: self.send_message(entity, msg, link_preview=False) @@ -356,6 +374,6 @@ class InteractiveTelegramClient(TelegramClient): else: who = self.get_entity(update.from_id) sprint('<< {} @ {} sent "{}"'.format( - get_display_name(which), get_display_name(who), - update.message + get_display_name(which), get_display_name(who), + update.message ))