Update interactive example to use .send_file() and .download_media()

This commit is contained in:
Lonami Exo 2017-08-23 00:55:49 +02:00
parent 1d0ad9628d
commit 9d15008f09

View File

@ -1,3 +1,4 @@
import os
from getpass import getpass from getpass import getpass
from telethon import TelegramClient from telethon import TelegramClient
@ -201,7 +202,7 @@ class InteractiveTelegramClient(TelegramClient):
# Download media # Download media
elif msg.startswith('!dm '): elif msg.startswith('!dm '):
# Slice the message to get message ID # Slice the message to get message ID
self.download_media(msg[len('!dm '):]) self.download_media_by_id(msg[len('!dm '):])
# Download profile photo # Download profile photo
elif msg == '!dp': elif msg == '!dp':
@ -220,24 +221,21 @@ class InteractiveTelegramClient(TelegramClient):
entity, msg, link_preview=False) entity, msg, link_preview=False)
def send_photo(self, path, entity): def send_photo(self, path, entity):
print('Uploading {}...'.format(path)) self.send_file(
input_file = self.upload_file( entity, path,
path, progress_callback=self.upload_progress_callback) progress_callback=self.upload_progress_callback
)
# After we have the handle to the uploaded file, send it to our peer
self.send_photo_file(input_file, entity)
print('Photo sent!') print('Photo sent!')
def send_document(self, path, entity): def send_document(self, path, entity):
print('Uploading {}...'.format(path)) self.send_file(
input_file = self.upload_file( entity, path,
path, progress_callback=self.upload_progress_callback) force_document=True,
progress_callback=self.upload_progress_callback
# After we have the handle to the uploaded file, send it to our peer )
self.send_document_file(input_file, entity)
print('Document sent!') print('Document sent!')
def download_media(self, media_id): def download_media_by_id(self, media_id):
try: try:
# The user may have entered a non-integer string! # The user may have entered a non-integer string!
msg_media_id = int(media_id) msg_media_id = int(media_id)
@ -245,13 +243,13 @@ class InteractiveTelegramClient(TelegramClient):
# Search the message ID # Search the message ID
for msg in self.found_media: for msg in self.found_media:
if msg.id == msg_media_id: if msg.id == msg_media_id:
# Let the output be the message ID print('Downloading media to usermedia/...')
output = str('usermedia/{}'.format(msg_media_id)) os.makedirs('usermedia', exist_ok=True)
print('Downloading media with name {}...'.format(output)) output = self.download_media(
output = self.download_msg_media(
msg.media, msg.media,
file=output, file='usermedia/',
progress_callback=self.download_progress_callback) progress_callback=self.download_progress_callback
)
print('Media downloaded to {}!'.format(output)) print('Media downloaded to {}!'.format(output))
except ValueError: except ValueError: