Taosky / telegram-search-bot

A telegram group record search bot that solves the problem of direct search inability in languages such as CJK / Telegram 群聊天记录搜索Bot,解决CJK等语言无法直接搜索问题
MIT License
355 stars 47 forks source link

chat_start.py中的get_chat_member报错 #57

Closed wsnsnsns closed 3 months ago

wsnsnsns commented 3 months ago

代码位置: https://github.com/Taosky/telegram-search-bot/blob/294e08c220dcb7df5ccd0861df2e414f56f0db65/user_handlers/chat_start.py#L40 get_chat_member 概述:

  1. 有些特殊情况没考虑到(用户权限、bot权限、用户状态),导致无法找到用户;
  2. python-telegram-bot的问题;
  3. telegram官方限制。 docker报错: Bot:xxxxxxxxxx:dispatcher - ERROR - No error handlers are registered, logging exception. Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/telegram/ext/dispatcher.py", line 557, in process_update handler.handle_update(update, self, check, context) File "/usr/local/lib/python3.9/site-packages/telegram/ext/handler.py", line 199, in handle_update return self.callback(update, context) File "/app/user_handlers/chat_start.py", line 40, in start chat_member = context.bot.get_chat_member( File "/usr/local/lib/python3.9/site-packages/telegram/bot.py", line 134, in decorator result = func(*args, **kwargs) File "/usr/local/lib/python3.9/site-packages/telegram/bot.py", line 3474, in get_chat_member result = self._post('getChatMember', data, timeout=timeout, api_kwargs=api_kwargs) File "/usr/local/lib/python3.9/site-packages/telegram/bot.py", line 299, in _post return self.request.post( File "/usr/local/lib/python3.9/site-packages/telegram/utils/request.py", line 361, in post result = self._request_wrapper( File "/usr/local/lib/python3.9/site-packages/telegram/utils/request.py", line 279, in _request_wrapper raise BadRequest(message) telegram.error.BadRequest: User not found
wsnsnsns commented 3 months ago

ai分析过的建议: 好的,我已经看过您的代码,并根据您的错误信息进行了一些分析,以下是我的建议:

  1. 检查用户ID: 您在代码中使用 update.effective_user.id 获取用户ID,但请确保该ID是有效的。您可以尝试使用以下方法进行验证:

Python

if not update.effective_user.id:

用户ID无效

return

使用 Telegram API 检查用户是否存在

try: context.bot.get_user(update.effective_user.id) except telegram.error.BadRequest:

用户不存在

return
  1. 检查权限: 根据您的代码,您尝试获取用户在群组中的信息。请确保您的 bot 拥有获取群组成员信息的权限。您可以通过以下方式检查权限:

Python

chat_member = context.bot.get_chat_member(chat_id=update.message.chat.id, user_id=update.effective_user.id)

if chat_member.status not in ("administrator", "member"):

用户没有权限

return
  1. 处理封锁情况: 如果用户已封锁您的 bot,您将无法获取其信息。您可以尝试使用以下方法来处理这种情况:

Python

try: context.bot.get_user(update.effective_user.id) except telegram.error.Unauthorized:

用户已封锁 bot

return
  1. 处理退群情况: 如果用户已退出群组,您将无法获取其信息。您可以使用以下方法来处理这种情况:

Python

if not update.message.chat.id:

这是一个私聊消息

return

chat_member = context.bot.get_chat_member(chat_id=update.message.chat.id, user_id=update.effective_user.id)

if chat_member.status == "left":

用户已退出群组

return
  1. 注册错误处理程序: 建议您在代码中注册错误处理程序,以捕获异常并采取适当的处理措施。例如:

Python

dispatcher.add_error_handler(error_callback)

def error_callback(update, context): try: raise context.error except BadRequest as e: if e.message == "User not found":

处理用户未找到的情况

        pass
except TelegramError as e:
    # 处理其他 Telegram API 错误
    pass

其他建议: 在调试时,可以使用 print 语句打印变量值,以验证代码的执行情况。 善用 Telegram API 文档和社区资源,获取更多信息和帮助。 在正式环境中运行 bot 前进行充分测试,以避免出现意外错误。

wsnsnsns commented 3 months ago

Telegram 对于群组成员数量的限制并没有一个固定的具体数字,而是根据一些因素进行动态调整。这些因素可能包括群组的活跃度、成员之间的交互频率、群组设置等等。

一般来说,当群组的成员数量达到一定程度时,Telegram 就会开始限制非管理员成员查看完整成员列表。这样做是为了保护群组成员的隐私,并减少滥用或侵犯隐私的可能性。

虽然 Telegram 并没有公开透露这个限制的具体数值,但根据社区的观察和经验,一般来说,当群组的成员数量达到几千人甚至更多时,就可能会出现这种限制。然而,这并不是一个确定的规则,因此实际情况可能会有所不同。