Add !i for information to the interactive telegram client (#614)

This commit is contained in:
Joscha Götzer 2018-02-23 21:34:15 +01:00 committed by Lonami
parent f9cec54c39
commit b7a61510bf

View File

@ -1,12 +1,13 @@
import os import os
from getpass import getpass from getpass import getpass
from telethon import TelegramClient, ConnectionMode from telethon.utils import get_display_name
from telethon import ConnectionMode, TelegramClient
from telethon.errors import SessionPasswordNeededError from telethon.errors import SessionPasswordNeededError
from telethon.tl.types import ( from telethon.tl.types import (
UpdateShortChatMessage, UpdateShortMessage, PeerChat PeerChat, UpdateShortChatMessage, UpdateShortMessage
) )
from telethon.utils import get_display_name
def sprint(string, *args, **kwargs): def sprint(string, *args, **kwargs):
@ -47,6 +48,7 @@ class InteractiveTelegramClient(TelegramClient):
Telegram through Telethon, such as listing dialogs (open chats), Telegram through Telethon, such as listing dialogs (open chats),
talking to people, downloading media, and receiving updates. talking to people, downloading media, and receiving updates.
""" """
def __init__(self, session_user_id, user_phone, api_id, api_hash, def __init__(self, session_user_id, user_phone, api_id, api_hash,
proxy=None): proxy=None):
""" """
@ -182,14 +184,15 @@ class InteractiveTelegramClient(TelegramClient):
# Show some information # Show some information
print_title('Chat with "{}"'.format(get_display_name(entity))) print_title('Chat with "{}"'.format(get_display_name(entity)))
print('Available commands:') print('Available commands:')
print(' !q: Quits the current chat.') print(' !q: Quits the current chat.')
print(' !Q: Quits the current chat and exits.') print(' !Q: Quits the current chat and exits.')
print(' !h: prints the latest messages (message History).') print(' !h: prints the latest messages (message History).')
print(' !up <path>: Uploads and sends the Photo from path.') print(' !up <path>: Uploads and sends the Photo from path.')
print(' !uf <path>: Uploads and sends the File from path.') print(' !uf <path>: Uploads and sends the File from path.')
print(' !d <msg-id>: Deletes a message by its id') print(' !d <msg-id>: Deletes a message by its id')
print(' !dm <msg-id>: Downloads the given message Media (if any).') print(' !dm <msg-id>: Downloads the given message Media (if any).')
print(' !dp: Downloads the current dialog Profile picture.') print(' !dp: Downloads the current dialog Profile picture.')
print(' !i: Prints information about this chat..')
print() print()
# And start a while loop to chat # And start a while loop to chat
@ -234,8 +237,7 @@ class InteractiveTelegramClient(TelegramClient):
# And print it to the user # And print it to the user
sprint('[{}:{}] (ID={}) {}: {}'.format( sprint('[{}:{}] (ID={}) {}: {}'.format(
msg.date.hour, msg.date.minute, msg.id, name, msg.date.hour, msg.date.minute, msg.id, name, content))
content))
# Send photo # Send photo
elif msg.startswith('!up '): elif msg.startswith('!up '):
@ -264,12 +266,16 @@ class InteractiveTelegramClient(TelegramClient):
os.makedirs('usermedia', exist_ok=True) os.makedirs('usermedia', exist_ok=True)
output = self.download_profile_photo(entity, 'usermedia') output = self.download_profile_photo(entity, 'usermedia')
if output: if output:
print( print('Profile picture downloaded to', output)
'Profile picture downloaded to {}'.format(output)
)
else: else:
print('No profile picture found for this user!') print('No profile picture found for this user!')
elif msg == '!i':
attributes = list(entity.to_dict().items())
pad = max(len(x) for x, _ in attributes)
for name, val in attributes:
print("{:<{width}} : {}".format(name, val, width=pad))
# Send chat message (if any) # Send chat message (if any)
elif msg: elif msg:
self.send_message(entity, msg, link_preview=False) self.send_message(entity, msg, link_preview=False)
@ -356,6 +362,5 @@ class InteractiveTelegramClient(TelegramClient):
else: else:
who = self.get_entity(update.from_id) who = self.get_entity(update.from_id)
sprint('<< {} @ {} sent "{}"'.format( sprint('<< {} @ {} sent "{}"'.format(
get_display_name(which), get_display_name(who), get_display_name(which), get_display_name(who), update.message
update.message
)) ))