2016-09-12 15:07:45 +03:00
|
|
|
import shutil
|
2016-11-26 14:04:02 +03:00
|
|
|
from getpass import getpass
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2017-05-21 14:02:54 +03:00
|
|
|
from . import TelegramClient
|
|
|
|
from .errors import RPCError
|
|
|
|
from .tl.types import UpdateShortChatMessage, UpdateShortMessage
|
|
|
|
from .utils import get_display_name
|
2016-11-30 00:29:42 +03:00
|
|
|
|
2016-09-12 15:07:45 +03:00
|
|
|
# Get the (current) number of lines in the terminal
|
|
|
|
cols, rows = shutil.get_terminal_size()
|
|
|
|
|
|
|
|
|
|
|
|
def print_title(title):
|
|
|
|
# Clear previous window
|
|
|
|
print('\n')
|
2017-05-07 14:05:49 +03:00
|
|
|
print('=={}=='.format('=' * len(title)))
|
2017-05-07 14:33:35 +03:00
|
|
|
try:
|
|
|
|
print('= {} ='.format(title))
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
# Issue #70, some consoles lack support for certain characters
|
|
|
|
print('= {} ='.format('?' * len(title)))
|
2017-05-07 14:05:49 +03:00
|
|
|
print('=={}=='.format('=' * len(title)))
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
|
2016-09-17 18:04:30 +03:00
|
|
|
def bytes_to_string(byte_count):
|
|
|
|
"""Converts a byte count to a string (in KB, MB...)"""
|
|
|
|
suffix_index = 0
|
|
|
|
while byte_count >= 1024:
|
|
|
|
byte_count /= 1024
|
|
|
|
suffix_index += 1
|
|
|
|
|
2016-11-30 00:29:42 +03:00
|
|
|
return '{:.2f}{}'.format(byte_count,
|
|
|
|
[' bytes', 'KB', 'MB', 'GB', 'TB'][suffix_index])
|
2016-09-17 18:04:30 +03:00
|
|
|
|
|
|
|
|
2016-09-12 15:07:45 +03:00
|
|
|
class InteractiveTelegramClient(TelegramClient):
|
2017-03-20 19:16:34 +03:00
|
|
|
def __init__(self, session_user_id, user_phone, api_id, api_hash, proxy=None):
|
2016-09-12 15:07:45 +03:00
|
|
|
print_title('Initialization')
|
|
|
|
|
|
|
|
print('Initializing interactive example...')
|
2017-03-20 19:16:34 +03:00
|
|
|
super().__init__(session_user_id, api_id, api_hash, proxy)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2016-09-12 16:39:23 +03:00
|
|
|
# Store all the found media in memory here,
|
|
|
|
# so it can be downloaded if the user wants
|
|
|
|
self.found_media = set()
|
|
|
|
|
2016-09-12 15:07:45 +03:00
|
|
|
print('Connecting to Telegram servers...')
|
|
|
|
self.connect()
|
|
|
|
|
|
|
|
# Then, ensure we're authorized and have access
|
|
|
|
if not self.is_user_authorized():
|
|
|
|
print('First run. Sending code request...')
|
|
|
|
self.send_code_request(user_phone)
|
|
|
|
|
2016-09-12 20:32:16 +03:00
|
|
|
code_ok = False
|
|
|
|
while not code_ok:
|
|
|
|
code = input('Enter the code you just received: ')
|
2016-11-26 14:04:02 +03:00
|
|
|
try:
|
|
|
|
code_ok = self.sign_in(user_phone, code)
|
|
|
|
|
|
|
|
# Two-step verification may be enabled
|
|
|
|
except RPCError as e:
|
|
|
|
if e.password_required:
|
2016-11-30 00:29:42 +03:00
|
|
|
pw = getpass(
|
|
|
|
'Two step verification is enabled. Please enter your password: ')
|
2016-11-26 14:04:02 +03:00
|
|
|
code_ok = self.sign_in(password=pw)
|
|
|
|
else:
|
2017-05-19 08:48:01 +03:00
|
|
|
raise
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
# Listen for updates
|
|
|
|
self.add_update_handler(self.update_handler)
|
|
|
|
|
|
|
|
# Enter a while loop to chat as long as the user wants
|
|
|
|
while True:
|
|
|
|
# Retrieve the top dialogs
|
|
|
|
dialog_count = 10
|
2016-10-03 20:44:01 +03:00
|
|
|
|
|
|
|
# Entities represent the user, chat or channel
|
|
|
|
# corresponding to the dialog on the same index
|
|
|
|
dialogs, entities = self.get_dialogs(dialog_count)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
i = None
|
|
|
|
while i is None:
|
2017-05-07 14:33:35 +03:00
|
|
|
print_title('Dialogs window')
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2017-05-07 14:33:35 +03:00
|
|
|
# Display them so the user can choose
|
|
|
|
for i, entity in enumerate(entities):
|
2017-05-21 14:59:16 +03:00
|
|
|
i += 1 # 1-based index
|
2017-05-07 14:33:35 +03:00
|
|
|
try:
|
2016-10-09 13:57:38 +03:00
|
|
|
print('{}. {}'.format(i, get_display_name(entity)))
|
2017-05-07 14:33:35 +03:00
|
|
|
except UnicodeEncodeError:
|
|
|
|
# Issue #70, some consoles lack support for certain characters
|
|
|
|
print('{}. {}'.format(i, '(could not render name)'))
|
|
|
|
|
|
|
|
# Let the user decide who they want to talk to
|
|
|
|
print()
|
|
|
|
print('> Who do you want to send messages to?')
|
|
|
|
print('> Available commands:')
|
|
|
|
print(' !q: Quits the dialogs window and exits.')
|
|
|
|
print(' !l: Logs out, terminating this session.')
|
|
|
|
print()
|
|
|
|
i = input('Enter dialog ID or a command: ')
|
|
|
|
if i == '!q':
|
|
|
|
return
|
|
|
|
if i == '!l':
|
|
|
|
self.log_out()
|
|
|
|
return
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2017-05-07 14:33:35 +03:00
|
|
|
try:
|
2016-09-12 15:07:45 +03:00
|
|
|
i = int(i if i else 0) - 1
|
|
|
|
# Ensure it is inside the bounds, otherwise set to None and retry
|
|
|
|
if not 0 <= i < dialog_count:
|
|
|
|
i = None
|
|
|
|
except ValueError:
|
2017-01-17 22:22:47 +03:00
|
|
|
i = None
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2016-10-03 20:44:01 +03:00
|
|
|
# Retrieve the selected user (or chat, or channel)
|
|
|
|
entity = entities[i]
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
# Show some information
|
2016-10-09 13:57:38 +03:00
|
|
|
print_title('Chat with "{}"'.format(get_display_name(entity)))
|
2016-09-16 14:35:14 +03:00
|
|
|
print('Available commands:')
|
2016-09-12 15:07:45 +03:00
|
|
|
print(' !q: Quits the current chat.')
|
2016-09-12 20:32:16 +03:00
|
|
|
print(' !Q: Quits the current chat and exits.')
|
2016-11-30 00:29:42 +03:00
|
|
|
print(
|
|
|
|
' !h: prints the latest messages (message History) of the chat.')
|
|
|
|
print(
|
|
|
|
' !up <path>: Uploads and sends a Photo located at the given path.')
|
|
|
|
print(
|
|
|
|
' !uf <path>: Uploads and sends a File document located at the given path.')
|
|
|
|
print(
|
|
|
|
' !dm <msg-id>: Downloads the given message Media (if any).')
|
2016-10-03 20:44:01 +03:00
|
|
|
print(' !dp: Downloads the current dialog Profile picture.')
|
2016-09-16 14:35:14 +03:00
|
|
|
print()
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
# And start a while loop to chat
|
|
|
|
while True:
|
|
|
|
msg = input('Enter a message: ')
|
|
|
|
# Quit
|
|
|
|
if msg == '!q':
|
|
|
|
break
|
2016-09-12 20:32:16 +03:00
|
|
|
elif msg == '!Q':
|
|
|
|
return
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
# History
|
|
|
|
elif msg == '!h':
|
|
|
|
# First retrieve the messages and some information
|
2016-11-30 00:29:42 +03:00
|
|
|
total_count, messages, senders = self.get_message_history(
|
2017-01-17 22:22:47 +03:00
|
|
|
entity, limit=10)
|
2016-09-12 15:07:45 +03:00
|
|
|
# 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
|
2016-11-30 00:29:42 +03:00
|
|
|
for msg, sender in zip(
|
|
|
|
reversed(messages), reversed(senders)):
|
2016-09-12 15:07:45 +03:00
|
|
|
# Get the name of the sender if any
|
|
|
|
name = sender.first_name if sender else '???'
|
|
|
|
|
|
|
|
# Format the message content
|
2017-04-17 10:36:24 +03:00
|
|
|
if getattr(msg, 'media', None):
|
2016-09-12 16:39:23 +03:00
|
|
|
self.found_media.add(msg)
|
2016-09-12 15:07:45 +03:00
|
|
|
content = '<{}> {}'.format( # The media may or may not have a caption
|
2016-11-30 00:29:42 +03:00
|
|
|
msg.media.__class__.__name__,
|
|
|
|
getattr(msg.media, 'caption', ''))
|
2017-04-17 10:36:24 +03:00
|
|
|
elif hasattr(msg, 'message'):
|
2016-09-12 15:07:45 +03:00
|
|
|
content = msg.message
|
2017-04-17 10:36:24 +03:00
|
|
|
elif hasattr(msg, 'action'):
|
|
|
|
content = str(msg.action)
|
|
|
|
else:
|
|
|
|
# Unknown message, simply print its class name
|
|
|
|
content = msg.__class__.__name__
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
# And print it to the user
|
|
|
|
print('[{}:{}] (ID={}) {}: {}'.format(
|
2016-11-30 00:29:42 +03:00
|
|
|
msg.date.hour, msg.date.minute, msg.id, name,
|
|
|
|
content))
|
2016-09-12 15:07:45 +03:00
|
|
|
|
|
|
|
# Send photo
|
2016-10-03 20:44:01 +03:00
|
|
|
elif msg.startswith('!up '):
|
2016-09-12 20:32:16 +03:00
|
|
|
# Slice the message to get the path
|
2017-01-17 22:22:47 +03:00
|
|
|
self.send_photo(path=msg[len('!up '):], entity=entity)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2016-09-12 20:32:16 +03:00
|
|
|
# Send file (document)
|
2016-10-03 20:44:01 +03:00
|
|
|
elif msg.startswith('!uf '):
|
2016-09-12 20:32:16 +03:00
|
|
|
# Slice the message to get the path
|
2017-01-17 22:22:47 +03:00
|
|
|
self.send_document(path=msg[len('!uf '):], entity=entity)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2016-09-12 16:39:23 +03:00
|
|
|
# Download media
|
2016-10-03 20:44:01 +03:00
|
|
|
elif msg.startswith('!dm '):
|
2016-09-12 20:32:16 +03:00
|
|
|
# Slice the message to get message ID
|
2017-01-17 22:22:47 +03:00
|
|
|
self.download_media(msg[len('!dm '):])
|
2016-09-12 16:39:23 +03:00
|
|
|
|
2016-10-03 20:44:01 +03:00
|
|
|
# Download profile photo
|
|
|
|
elif msg == '!dp':
|
|
|
|
output = str('usermedia/propic_{}'.format(entity.id))
|
|
|
|
print('Downloading profile picture...')
|
|
|
|
success = self.download_profile_photo(entity.photo, output)
|
|
|
|
if success:
|
2016-11-30 00:29:42 +03:00
|
|
|
print('Profile picture downloaded to {}'.format(
|
|
|
|
output))
|
2016-10-03 20:44:01 +03:00
|
|
|
else:
|
2017-05-07 14:33:35 +03:00
|
|
|
print('No profile picture found for this user.')
|
2016-10-03 20:44:01 +03:00
|
|
|
|
2016-09-12 15:07:45 +03:00
|
|
|
# Send chat message (if any)
|
|
|
|
elif msg:
|
2016-11-30 00:29:42 +03:00
|
|
|
self.send_message(
|
2017-01-17 22:22:47 +03:00
|
|
|
entity, msg, markdown=True, no_web_page=True)
|
2016-09-12 15:07:45 +03:00
|
|
|
|
2017-01-17 22:22:47 +03:00
|
|
|
def send_photo(self, path, entity):
|
2016-09-12 20:32:16 +03:00
|
|
|
print('Uploading {}...'.format(path))
|
2016-11-30 00:29:42 +03:00
|
|
|
input_file = self.upload_file(
|
|
|
|
path, progress_callback=self.upload_progress_callback)
|
2016-09-12 20:32:16 +03:00
|
|
|
|
|
|
|
# After we have the handle to the uploaded file, send it to our peer
|
2017-01-17 22:22:47 +03:00
|
|
|
self.send_photo_file(input_file, entity)
|
2016-09-12 20:32:16 +03:00
|
|
|
print('Photo sent!')
|
|
|
|
|
2017-01-17 22:22:47 +03:00
|
|
|
def send_document(self, path, entity):
|
2016-09-12 20:32:16 +03:00
|
|
|
print('Uploading {}...'.format(path))
|
2016-11-30 00:29:42 +03:00
|
|
|
input_file = self.upload_file(
|
|
|
|
path, progress_callback=self.upload_progress_callback)
|
2016-09-12 20:32:16 +03:00
|
|
|
|
|
|
|
# After we have the handle to the uploaded file, send it to our peer
|
2017-01-17 22:22:47 +03:00
|
|
|
self.send_document_file(input_file, entity)
|
2016-09-12 20:32:16 +03:00
|
|
|
print('Document sent!')
|
|
|
|
|
|
|
|
def download_media(self, media_id):
|
|
|
|
try:
|
|
|
|
# The user may have entered a non-integer string!
|
|
|
|
msg_media_id = int(media_id)
|
|
|
|
|
|
|
|
# Search the message ID
|
|
|
|
for msg in self.found_media:
|
|
|
|
if msg.id == msg_media_id:
|
|
|
|
# Let the output be the message ID
|
|
|
|
output = str('usermedia/{}'.format(msg_media_id))
|
|
|
|
print('Downloading media with name {}...'.format(output))
|
2016-11-30 00:29:42 +03:00
|
|
|
output = self.download_msg_media(
|
|
|
|
msg.media,
|
|
|
|
file_path=output,
|
|
|
|
progress_callback=self.download_progress_callback)
|
2016-09-12 20:32:16 +03:00
|
|
|
print('Media downloaded to {}!'.format(output))
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
print('Invalid media ID given!')
|
|
|
|
|
2016-09-17 18:04:30 +03:00
|
|
|
@staticmethod
|
|
|
|
def download_progress_callback(downloaded_bytes, total_bytes):
|
2016-11-30 00:29:42 +03:00
|
|
|
InteractiveTelegramClient.print_progress('Downloaded',
|
|
|
|
downloaded_bytes, total_bytes)
|
2016-09-17 18:04:30 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def upload_progress_callback(uploaded_bytes, total_bytes):
|
2016-11-30 00:29:42 +03:00
|
|
|
InteractiveTelegramClient.print_progress('Uploaded', uploaded_bytes,
|
|
|
|
total_bytes)
|
2016-09-17 18:04:30 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def print_progress(progress_type, downloaded_bytes, total_bytes):
|
2016-11-30 00:29:42 +03:00
|
|
|
print('{} {} out of {} ({:.2%})'.format(progress_type, bytes_to_string(
|
|
|
|
downloaded_bytes), bytes_to_string(total_bytes), downloaded_bytes /
|
|
|
|
total_bytes))
|
2016-09-17 18:04:30 +03:00
|
|
|
|
2016-09-12 15:07:45 +03:00
|
|
|
@staticmethod
|
|
|
|
def update_handler(update_object):
|
2017-05-07 14:33:35 +03:00
|
|
|
try:
|
|
|
|
if type(update_object) is UpdateShortMessage:
|
|
|
|
if update_object.out:
|
|
|
|
print('You sent {} to user #{}'.format(update_object.message,
|
|
|
|
update_object.user_id))
|
|
|
|
else:
|
|
|
|
print('[User #{} sent {}]'.format(update_object.user_id,
|
|
|
|
update_object.message))
|
|
|
|
|
|
|
|
elif type(update_object) is UpdateShortChatMessage:
|
|
|
|
if update_object.out:
|
|
|
|
print('You sent {} to chat #{}'.format(update_object.message,
|
|
|
|
update_object.chat_id))
|
|
|
|
else:
|
|
|
|
print('[Chat #{}, user #{} sent {}]'.format(
|
|
|
|
update_object.chat_id, update_object.from_id,
|
|
|
|
update_object.message))
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
# Issue #70, some consoles lack support for certain characters
|
|
|
|
print('(Could not print update since it contains '
|
|
|
|
'characters your terminal does not support)')
|