You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python Telegram Bot捕获群聊中用户的输入状态?

解决Telegram Bot捕获群聊用户打字状态的问题

一、Pyrogram 无捕获效果的排查方案

  1. 确认事件监听逻辑
    必须使用on_user_status事件监听UserStatus.TYPING状态,同时确保群聊ID格式正确(带负号,如-1001234567890)。示例代码:

    from pyrogram import Client, filters
    from pyrogram.types import UserStatus
    
    app = Client("my_bot", api_id=YOUR_API_ID, api_hash="YOUR_API_HASH", bot_token="YOUR_BOT_TOKEN")
    
    @app.on_user_status(filters.chat(YOUR_GROUP_ID))
    async def track_typing(client, update):
        if update.status == UserStatus.TYPING:
            print(f"{update.user.first_name} 正在群聊中打字")
    
    app.run()
    
  2. 检查Bot权限与群聊设置

    • 确保Bot在群聊中拥有读取消息权限,即使是管理员也需确认权限未被限制。
    • 检查群聊隐私设置,部分私密群可能会限制Bot查看用户状态的权限。
  3. 升级库版本
    Pyrogram 2.0.106可能存在事件监听的小bug,尝试升级到最新稳定版:

    pip install --upgrade pyrogram
    

二、Telethon 触发ChatAdminRequiredError的解决

  1. 修正事件监听代码
    捕获打字状态不需要管理员权限,只需监听UserTyping事件,确保群聊ID格式正确。示例代码:

    from telethon import TelegramClient, events
    
    client = TelegramClient("my_bot", YOUR_API_ID, "YOUR_API_HASH").start(bot_token="YOUR_BOT_TOKEN")
    
    @client.on(events.UserTyping(chats=YOUR_GROUP_ID))
    async def handler(event):
        print(f"{event.sender.first_name} 正在打字")
    
    client.run_until_disconnected()
    
  2. 排查群聊权限与设置

    • 进入群聊Bot权限设置,确认Bot拥有「读取消息」权限。
    • 如果是超级群,检查是否开启「仅管理员可查看成员」,该设置会导致Bot无法获取用户状态,需关闭或赋予Bot查看成员权限。

通用排查步骤

  • 确认Bot已正确加入群聊,未被静音或限制权限。
  • 在不同类型群聊(公开、超级、私密)中测试,排除群聊设置差异。
  • 再次核对API ID、API Hash、Bot Token,确保无拼写或空格错误。

内容的提问来源于stack exchange,提问作者Kate Loganotviz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 01:17:01