我正在研究Telethon download_media和_download_document从电报下载视频的方法。我的代码是这样的:
def callback(update):
Channel_Entity = client.get_entity(Channel_List) #Get specific Channel information
file_name = str(document_id)+'.mp4'
current_path = os.getcwd()
file_path_gif = current_path+'/media/gif'
file = open(os.path.join(file_path_gif,file_name),'wb')
if isinstance(update, UpdateNewChannelMessage): #Check Update message in channel
if update.message.to_id.channel_id == Channel_Entity.id:
client._download_document(update.message.media, file, update.message.date, progress_callback=None)
# OR
client.download_media(update.message, file, progress_callback=None)
Run Code Online (Sandbox Code Playgroud)
但是,当将视频发送到频道并使用此代码下载时,该视频将无法播放,并且播放器会显示以下消息:无法呈现文件。此代码适用于图像和gif文件,但不适用于视频文件。我该怎么办?
嗨,我正在电报API telethon。在这里,我想不断地用python代码监听群组消息。
我能够从组中读取消息,但是每次我需要运行代码时。有什么方法可以实现我的代码应该同步监听消息。
以下是一些代码段,这些代码段使我可以分组查看消息。需要在其中添加侦听器代码。
client = TelegramClient('session_read', api_id, api_hash)
client.start()
dialog_count = 50
dialogs = client.get_dialogs(dialog_count)
for i, entity in enumerate(dialogs):
if entity.name == 'GroupName':
print('{}'.format(entity.message.message))
Run Code Online (Sandbox Code Playgroud) 我将replier.py作为来自 Telethon 官方示例的基本代码。
每次收到消息时,我都想获取消息 ID。
...
@events.register(events.NewMessage)
async def handler(event):
if not event.out:
print(f'received message_id = {event.message_id()}')
client = event.client
with client:
client.add_event_handler(handler)
client.run_until_disconnected()
Run Code Online (Sandbox Code Playgroud)
>>> ... AttributeError: 'Message' object has no attribute 'message_id'
Run Code Online (Sandbox Code Playgroud)
我尝试了几种变体,但都没有成功。
如何正确操作?
我需要在电报应用程序中读取一些公共频道的消息,我想将电报频道文本存储在一个文本文件中。我想使用python。我尝试使用 Telethon,但它太复杂了。我的代码有一些错误:
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (
PeerChannel
)
client = TelegramClient(username, api_id, api_hash)
client.start()
offset_id = 0
limit = 100
all_messages = []
total_messages = 0
total_count_limit = 0
while True:
print("Current Offset ID is:", offset_id, "; Total Messages:", total_messages)
history = client(GetHistoryRequest(
peer="https://t.me/futballbadnews",
offset_id=offset_id,
offset_date=None,
add_offset=0,
limit=limit,
max_id=0,
min_id=0,
hash=0
))
if not history.messages:
break
messages = history.messages
for message in messages:
all_messages.append(message.to_dict())
offset_id = messages[len(messages) - 1].id
total_messages = len(all_messages)
if total_count_limit != 0 …Run Code Online (Sandbox Code Playgroud) 好的,所以我一直打算使用 Telethon 用 Python 在电报上自动化一些事情,但我不确定我是否理解它的要点。
首先,您需要一个 api_id 和一个 api_hash。为此,您转到my.telegram并转到 API 开发工具。在那里你会收到一个代码到你的电报安卓手机,提交后,你会收到一个唯一的 id/hash。第一个问题,您发送此代码是为了生成一个需要的应用程序吗?
现在在python中,您可以按如下方式启动客户端。
from telethon import TelegramClient
api_id=12345
api_hash='abcdef12345ghij'
client=TelegramClient('name of the session',api_id,api_hash)
Run Code Online (Sandbox Code Playgroud)
您可以尝试连接客户端,但它可能会导致未授权或手机未注册,因此您可以使用 start ,这将决定是登录还是注册。在start中可以设置的参数中,有一个是force_sms (bool, optional)强制电报共享短信注册登录所需的代码。我的问题是,如果电话没有注册,还有什么其他方式可以使用电报?我的意思是,他们无法将其发送到移动应用程序,因为该手机没有。
如果电话未注册是可能的,这是否意味着您获得 ID/哈希的电话不一定与您创建客户端的电话相同?
由于此方法有回调,您可以输入发送到手机的代码并连接到电报。
另一种连接客户端的方法是使用StringSession. 我找到了这段代码:
from telethon.sync import TelegramClient
from telethon.sessions import StringSession
# Generating a new one
with TelegramClient(StringSession(), api_id, api_hash) as client:
print(client.session.save())
# Converting SQLite (or any) to StringSession
with TelegramClient(name, api_id, api_hash) as client:
print(StringSession.save(client.session))
# Usage
string = '1aaNk8EX-YRfwoRsebUkugFvht6DUPi_Q25UOCzOAqzc...'
with TelegramClient(StringSession(string), …Run Code Online (Sandbox Code Playgroud) 我为我的 python 脚本租了一台 CentOS 7 服务器。我创建了一个 python 脚本,它连接我的电报帐户并将消息转发到目标。我用 Telethon 来做到这一点。当我通过 ssh 连接到服务器时,我可以运行脚本。当我注销时,我的脚本不起作用。
我使用了 nohup 和 & 但在重新启动或关闭之前仍然无法运行脚本。使用 nohup 注销后,我的脚本工作了一段时间但再次关闭。
我错过了哪些要点?
我希望使用 Telethon API 抓取 Telegram 组用户列表。因此它会抓取组中的所有用户,我只想抓取过去 24 小时/最近的 ACTIVE 用户。然而我无法这样做/不明白。如果有人可以帮助我并告诉我放置预期代码的位置会非常好
谢谢你的问候!
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
import csv
api_id =
api_hash = ''
phone = '+'
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
chats = []
last_date = None
chunk_size = 200
groups=[]
result = client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash = 0
))
chats.extend(result.chats)
for chat in chats:
try:
if chat.megagroup== True:
groups.append(chat)
except:
continue
print('Choose a group …Run Code Online (Sandbox Code Playgroud) 当消息到达组时,我无法获取 event.message 对象的发件人的用户名。
我尝试了 get_entity、get_input_entity 方法。但这显示错误。AttributeError: 'coroutine' 对象没有属性 'chat' 我什至不知道如何处理 coroutine 对象
@client.on(events.NewMessage(chats=input_groups_entity))
async def handler(event):
print(event)
sender = event.message.chat_id
perticipants = client.get_participants(event.message.to_id)
np = client.get_input_entity(event.message.from_id)
await client.send_message(event.message.to_id, message=event.message.message)
Run Code Online (Sandbox Code Playgroud)
我只想从消息对象中提取用户名
我正在运行这个:
from telethon import TelegramClient, events
from telethon.tl.types import PeerChat, PeerChannel
@client.on(events.ChatAction(chats=[PeerChannel(-1001244988104)]))
async def handle_chat_action(event):
print(event)
print(event.user_left)
@client.on(events.NewMessage(chats=[PeerChat(-100123456789)]))
async def my_event_handler(event):
print(event.raw_text)
client.start()
client.run_until_disconnected()
Run Code Online (Sandbox Code Playgroud)
但是,没有得到更新,为什么?这里有什么问题?
起初,我尝试过chats=[-100123456](没有 PeerChat),但没有得到更新......
尝试运行Telethon 文档提供的基本代码时,我收到以下错误。我很困惑,因为我还没有建立一个循环。
RuntimeError:如果事件循环正在运行,则必须使用“async with”(即您在“async def”中)
我在 spyder 4.0.1 中使用 python 3.7.7
from telethon.sync import TelegramClient
from telethon import functions, types
def channel_info(username, api_id, api_hash):
with TelegramClient(username, api_id, api_hash,channel) as client:
result = client(functions.channels.GetFullChannelRequest(
channel=channel
))
return(result)
out = channel_info(username, api_id, api_hash)
Run Code Online (Sandbox Code Playgroud)