Update interactive example to show how to get user from updates

This commit is contained in:
Lonami Exo 2017-10-01 20:12:27 +02:00
parent ace44965f8
commit 3805aa3772

View File

@ -3,7 +3,9 @@ from getpass import getpass
from telethon import TelegramClient, ConnectionMode from telethon import TelegramClient, ConnectionMode
from telethon.errors import SessionPasswordNeededError from telethon.errors import SessionPasswordNeededError
from telethon.tl.types import UpdateShortChatMessage, UpdateShortMessage from telethon.tl.types import (
UpdateShortChatMessage, UpdateShortMessage, PeerChat
)
from telethon.utils import get_display_name from telethon.utils import get_display_name
@ -263,35 +265,44 @@ class InteractiveTelegramClient(TelegramClient):
@staticmethod @staticmethod
def download_progress_callback(downloaded_bytes, total_bytes): def download_progress_callback(downloaded_bytes, total_bytes):
InteractiveTelegramClient.print_progress('Downloaded', InteractiveTelegramClient.print_progress(
downloaded_bytes, total_bytes) 'Downloaded', downloaded_bytes, total_bytes
)
@staticmethod @staticmethod
def upload_progress_callback(uploaded_bytes, total_bytes): def upload_progress_callback(uploaded_bytes, total_bytes):
InteractiveTelegramClient.print_progress('Uploaded', uploaded_bytes, InteractiveTelegramClient.print_progress(
total_bytes) 'Uploaded', uploaded_bytes, total_bytes
)
@staticmethod @staticmethod
def print_progress(progress_type, downloaded_bytes, total_bytes): def print_progress(progress_type, downloaded_bytes, total_bytes):
print('{} {} out of {} ({:.2%})'.format(progress_type, bytes_to_string( print('{} {} out of {} ({:.2%})'.format(
downloaded_bytes), bytes_to_string(total_bytes), downloaded_bytes / progress_type, bytes_to_string(downloaded_bytes),
total_bytes)) bytes_to_string(total_bytes), downloaded_bytes / total_bytes)
)
@staticmethod def update_handler(self, update):
def update_handler(update_object): if isinstance(update, UpdateShortMessage):
if isinstance(update_object, UpdateShortMessage): who = self.get_entity(update.user_id)
if update_object.out: if update.out:
sprint('You sent {} to user #{}'.format( sprint('>> "{}" to user {}'.format(
update_object.message, update_object.user_id)) update.message, get_display_name(who)
))
else: else:
sprint('[User #{} sent {}]'.format( sprint('<< {} sent "{}"]'.format(
update_object.user_id, update_object.message)) get_display_name(who), update.message
))
elif isinstance(update_object, UpdateShortChatMessage): elif isinstance(update, UpdateShortChatMessage):
if update_object.out: which = self.get_entity(PeerChat(update.chat_id))
sprint('You sent {} to chat #{}'.format( if update.out:
update_object.message, update_object.chat_id)) sprint('>> sent "{}" to chat {}'.format(
update.message, get_display_name(which)
))
else: else:
sprint('[Chat #{}, user #{} sent {}]'.format( who = self.get_entity(update.from_id)
update_object.chat_id, update_object.from_id, sprint('<< {} @ {} sent "{}"'.format(
update_object.message)) get_display_name(which), get_display_name(who),
update.message
))