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

Pyrogram在Debian10运行提示Client无get_history属性报错排查

报错原因
  • 核心问题是两个运行环境安装的Pyrogram版本不统一:
    • VSCode环境安装的是Pyrogram 1.x旧稳定版,该版本客户端提供get_history方法,因此代码可以正常执行
    • Debian 10环境默认拉取的是Pyrogram 2.x新版本,官方在2.0版本迭代中正式移除了get_history方法,将其重命名为get_chat_history,因此调用不存在的方法时触发AttributeError。
  • 原代码还存在三个隐性问题,会在运行时导致异常:
    • 异步回调函数中使用同步的time.sleep(2),会阻塞整个客户端事件循环,导致其他消息、回调全部卡住无法响应
    • 消息回调触发时客户端已经处于连接状态,函数内额外写with app:上下文管理器属于重复调用,可能引发连接冲突
    • 新版本的get_chat_history是异步生成器,不能用await修饰后直接迭代,需要搭配async for语法使用
修复步骤
  1. 优先适配新版本API(旧版本Pyrogram已停止维护,不建议降级),按以下规则调整代码:

    • 新增asyncio导入,删除无用的time导入
    • 删掉回调函数里多余的with app:上下文包裹
    • 将await app.get_history(chat, limit=1)替换为app.get_chat_history(chat, limit=1),同时把普通for迭代改为async for异步迭代
    • 将time.sleep(2)替换为await asyncio.sleep(2),避免阻塞事件循环
  2. 修正后的完整可运行代码如下:

import asyncio
import pyrogram

app = pyrogram.Client("my_account", api_id=********,
                      api_hash="***********ffe01e582ef")

@app.on_message(pyrogram.filters.command('start', prefixes='.') & pyrogram.filters.me)
async def sms(_, msg):
    public = await app.get_chat('@public_name')
    chat = public.id
    while True:
        async for hist_msg in app.get_chat_history(chat, limit=1):
            if hist_msg.text == 'Menu:':
                await app.send_message(chat, '1')
            else:
                await app.send_message(chat, '2')
            break
        await asyncio.sleep(2)

app.run()

临时兼容方案:如果不想修改代码,可以在Debian 10环境安装指定旧版本Pyrogram,执行命令pip install pyrogram==1.4.0即可,但该方案不推荐,旧版本无后续维护,存在兼容性和安全隐患。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 20:42:17