【发布时间】:2020-06-15 15:45:54
【问题描述】:
我已经看到所有关于电视马拉松的条目,但没有一个回答我的问题。
我想达到什么目标? 我想从电报频道下载一张图片到我的电脑中
到目前为止我做了什么? 我能够创建一个客户端并阅读包含图像的消息。但是,在我 download_media 之后,我不知道文件在哪里或如何继续。
注意:我不想使用任何 BOT。
这是我的代码:
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")
最后几行输出:
<coroutine object Message.download_media at 0x00000238F1E657C8>
所以问题是:如何保存/查看/下载该图像?
【问题讨论】:
-
你应该
await它。 -
找到解决办法了吗