I have seen all the entries asking about telethon but none answer my question.
WHAT TO I WANT TO ACHIEVE? I want to download an image from a telegram channel into my PC
WHAT HAVE I DONE SO FAR? I am able to create a client and to read the message which includes the image. However after I download_media I do not know where the file is or how to continue.
NOTE: I do not want to use any BOT.
This is my code:
import configparser #read API credentials from a config file
import json # dump data into JSON formatted files.
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch
from telethon.tl.types import (PeerChannel)
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon import utils
#**************** CREATE TELEGRAM CLIENT ******************************
# Reading Configs
config = configparser.ConfigParser()
config.read("E:\TelegramBOT\config.ini")
# Setting configuration values
api_id = config['Telegram']['api_id']
api_hash = config['Telegram']['api_hash']
api_hash = str(api_hash)
phone = config['Telegram']['phone']
username = config['Telegram']['username']
# Log in to Telegram and create a client object to use for getting data
# Create the client and connect
client = TelegramClient(username, api_id, api_hash)
client.start()
print("Client Created")
# Ensure you're authorized
if not client.is_user_authorized():
client.send_code_request(phone)
try:
client.sign_in(phone, input('Enter the code: '))
except SessionPasswordNeededError:
client.sign_in(password=input('Password: '))
async def main():
# You can print the message history of any chat:
async for message in client.iter_messages('<CHANNEL>'):
print(message.sender.username, message.text)
if not message.media == False:
message.download_media(message.media, "E:\TelegramBOT\Tips")
The last lines outputs:
<coroutine object Message.download_media at 0x00000238F1E657C8>
So the question is: How do I save/view/download that image?