我目前正在尝试创建一些需要获取组/通道中的消息数量的东西。最好的方法是在这个聊天中获取最新消息的id。
因为我的机器人将处理被删除的消息,所以我们不关心它们
我试过了:
total_messages = await Client.get_chat_history_count(chat_id)但我得到了以下错误:
2022-06-17 13:22:34,479 - ERROR - pyrogram.dispatcher - MainThread - Telegram says: [400 BOT_METHOD_INVALID] - The method can't be used by bots (caused by "messages.GetHistory")
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/pyrogram/dispatcher.py", line 240, in handler_worker
await handler.callback(self.client, *args)
File "/root/bot.py", line 117, in range
total_mess = await teledump.get_chat_history_count(Var.idtodump)
File "/usr/local/lib/python3.9/site-packages/pyrogram/methods/messages/get_chat_history_count.py", line 54, in get_chat_history_count
r = await self.invoke(
File "/usr/local/lib/python3.9/site-packages/pyrogram/methods/advanced/invoke.py", line 77, in invoke
r = await self.session.invoke(
File "/usr/local/lib/python3.9/site-packages/pyrogram/session/session.py", line 362, in invoke
return await self.send(query, timeout=timeout)
File "/usr/local/lib/python3.9/site-packages/pyrogram/session/session.py", line 332, in send
RPCError.raise_it(result, type(data))
File "/usr/local/lib/python3.9/site-packages/pyrogram/errors/rpc_error.py", line 91, in raise_it
raise getattr(
pyrogram.errors.exceptions.bad_request_400.BotMethodInvalid: Telegram says: [400 BOT_METHOD_INVALID] - The method can't be used by bots (caused by "messages.GetHistory")任何帮助都将不胜感激!
发布于 2022-06-17 14:50:07
为了获得消息ID,我鼓励您深入到Telethon并使用NewMessage。此方法侦听每一条新消息,并向您提供所需的所有信息。
https://docs.telethon.dev/en/stable/modules/events.html#telethon.events.newmessage.NewMessage
下面是如何开始的方法:
https://docs.telethon.dev/en/stable/basic/installation.html
祝好运!
from telethon import TelegramClient, events
chat_ids = []
client = TelegramClient('session', api_id, api_hash)
@client.on(events.NewMessage(chats=chat_ids))
async def newMessageListener(event):
print(event.message.id)这会给你你所需要的。
编辑:
chat_ids是您想要侦听的聊天的数组,在您的例子中,是您聊天的ID。
发布于 2022-06-17 14:35:33
电报不允许机器人检索聊天历史记录,除非您通过聊天和消息id app.get_messages(-100123123, 123)检索特定的消息。
您可能需要的是监视所有传入消息并只存储它们的id,所以当您需要最新的消息时,您已经将其id存储起来供使用。
https://stackoverflow.com/questions/72660942
复制相似问题