尝试使用Telethon iter_messages在全球搜索没有结果

发布于 2025-02-08 20:21:19 字数 1359 浏览 3 评论 0原文

我正在将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2025-02-15 20:21:19

我只是尝试了这个,这对我有用:

async def main():
    async with TelegramClient('session', 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 just tried this and this works for me:

async def main():
    async with TelegramClient('session', 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())

if it still doesnt work for you, please provide Error message you get.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文