mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-13 04:56:35 +03:00
Added ability to download message media*
* Although this was already supported, now the `InteractiveTelegramClient` has a command for it
This commit is contained in:
parent
0fe0cdf682
commit
13f7e6170f
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -5,6 +5,7 @@ tl/all_tlobjects.py
|
||||||
|
|
||||||
# User session
|
# User session
|
||||||
*.session
|
*.session
|
||||||
|
*.jpg
|
||||||
api/settings
|
api/settings
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import tl_generator
|
import tl_generator
|
||||||
|
from tl.types import MessageMediaPhoto
|
||||||
from tl.types import UpdateShortChatMessage
|
from tl.types import UpdateShortChatMessage
|
||||||
from tl.types import UpdateShortMessage
|
from tl.types import UpdateShortMessage
|
||||||
|
|
||||||
|
@ -38,6 +39,10 @@ class InteractiveTelegramClient(TelegramClient):
|
||||||
print('Initializing interactive example...')
|
print('Initializing interactive example...')
|
||||||
super().__init__(session_user_id, layer, api_id, api_hash)
|
super().__init__(session_user_id, layer, api_id, api_hash)
|
||||||
|
|
||||||
|
# Store all the found media in memory here,
|
||||||
|
# so it can be downloaded if the user wants
|
||||||
|
self.found_media = set()
|
||||||
|
|
||||||
print('Connecting to Telegram servers...')
|
print('Connecting to Telegram servers...')
|
||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
|
@ -92,6 +97,7 @@ class InteractiveTelegramClient(TelegramClient):
|
||||||
print(' !q: Quits the current chat.')
|
print(' !q: Quits the current chat.')
|
||||||
print(' !h: prints the latest messages (message History) of the chat.')
|
print(' !h: prints the latest messages (message History) of the chat.')
|
||||||
print(' !p <path>: sends a Photo located at the given path')
|
print(' !p <path>: sends a Photo located at the given path')
|
||||||
|
print(' !d <msg-id>: Downloads the given message media (if any)')
|
||||||
|
|
||||||
# And start a while loop to chat
|
# And start a while loop to chat
|
||||||
while True:
|
while True:
|
||||||
|
@ -112,6 +118,7 @@ class InteractiveTelegramClient(TelegramClient):
|
||||||
|
|
||||||
# Format the message content
|
# Format the message content
|
||||||
if msg.media:
|
if msg.media:
|
||||||
|
self.found_media.add(msg)
|
||||||
content = '<{}> {}'.format( # The media may or may not have a caption
|
content = '<{}> {}'.format( # The media may or may not have a caption
|
||||||
msg.media.__class__.__name__, getattr(msg.media, 'caption', ''))
|
msg.media.__class__.__name__, getattr(msg.media, 'caption', ''))
|
||||||
else:
|
else:
|
||||||
|
@ -132,6 +139,27 @@ class InteractiveTelegramClient(TelegramClient):
|
||||||
self.send_photo_file(input_file, input_peer)
|
self.send_photo_file(input_file, input_peer)
|
||||||
print('Media sent!')
|
print('Media sent!')
|
||||||
|
|
||||||
|
# Download media
|
||||||
|
elif msg.startswith('!d '):
|
||||||
|
msg_media_id = msg[len('!d '):] # Slice the message to get message ID
|
||||||
|
try:
|
||||||
|
# The user may have entered a non-integer string!
|
||||||
|
msg_media_id = int(msg_media_id)
|
||||||
|
|
||||||
|
# Search the message ID and ensure the media is a Photo
|
||||||
|
for msg in self.found_media:
|
||||||
|
if (msg.id == msg_media_id and
|
||||||
|
type(msg.media) == MessageMediaPhoto):
|
||||||
|
|
||||||
|
# Retrieve the output and download the photo
|
||||||
|
output = '{}.jpg'.format(str(msg_media_id))
|
||||||
|
print('Downloading to {}...'.format(output))
|
||||||
|
self.download_photo(msg.media, file_path=output)
|
||||||
|
print('Photo downloaded to {}!'.format(output))
|
||||||
|
|
||||||
|
except ValueError:
|
||||||
|
print('Invalid media ID given!')
|
||||||
|
|
||||||
# Send chat message (if any)
|
# Send chat message (if any)
|
||||||
elif msg:
|
elif msg:
|
||||||
self.send_message(input_peer, msg, markdown=True, no_web_page=True)
|
self.send_message(input_peer, msg, markdown=True, no_web_page=True)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user