尝试使用Telethon iter_messages在全球搜索没有结果
我正在将Telethon Python软件包用于电报API。
使用以下代码,我可以根据关键字搜索收集特定用户的消息:
import config
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.sync import TelegramClient
from telethon import functions, types
import datetime
import asyncio
api_id = config.telegram_crawler_secrets_and_settings['TELEGRAM_API_ID']
api_hash = config.telegram_crawler_secrets_and_settings['TELEGRAM_API_HASH']
phone = config.telegram_crawler_secrets_and_settings['TELEGRAM_API_PHONE_NUMBER']
async def main():
async with TelegramClient('anon', api_id, api_hash) as client:
async for message in client.iter_messages(5067150042, limit=10, search='hello', wait_time=0):
print(message.stringify())
await asyncio.sleep(0.2)
asyncio.run(main())
根据这篇文章,应该可以用none
替换实体(用户ID)并在公共渠道上执行全局搜索,但是在我的尝试中似乎不起作用(请参见下文)。
async def main():
async with TelegramClient('anon', api_id, api_hash) as client:
async for message in client.iter_messages(None, limit=10, search='hello', wait_time=0):
print(message.stringify())
await asyncio.sleep(0.2)
asyncio.run(main())
任何建议都非常感激!
I'm using Telethon python package for the Telegram API.
Using the following code, I can collect messages for a particular user, based on a keyword search:
import config
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.sync import TelegramClient
from telethon import functions, types
import datetime
import asyncio
api_id = config.telegram_crawler_secrets_and_settings['TELEGRAM_API_ID']
api_hash = config.telegram_crawler_secrets_and_settings['TELEGRAM_API_HASH']
phone = config.telegram_crawler_secrets_and_settings['TELEGRAM_API_PHONE_NUMBER']
async def main():
async with TelegramClient('anon', api_id, api_hash) as client:
async for message in client.iter_messages(5067150042, limit=10, search='hello', wait_time=0):
print(message.stringify())
await asyncio.sleep(0.2)
asyncio.run(main())
According to this post, it should be possible to replace the entity (User ID) with None
and perform a global search across public channels, however it does not appear to work in my attempts (see below).
async def main():
async with TelegramClient('anon', api_id, api_hash) as client:
async for message in client.iter_messages(None, limit=10, search='hello', wait_time=0):
print(message.stringify())
await asyncio.sleep(0.2)
asyncio.run(main())
Any suggestions gratefully received!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是尝试了这个,这对我有用:
如果它仍然不适合您,请提供您收到的错误消息。
I just tried this and this works for me:
if it still doesnt work for you, please provide Error message you get.