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

如何用Pyrogram向Telegram Bot发消息?遇ID未找到问题求助

解决思路
  • 验证Bot ID的正确性
    必须使用Bot的纯数字ID作为chat_id,不能用带@的username。获取正确ID的方式:

    • 给Telegram的@userinfobot发送@你的Bot用户名,它会返回Bot的数字ID;
    • 用python-telegram-bot代码打印Bot自身ID:
      from telegram import Bot
      bot = Bot("你的Bot Token")
      print(bot.id)
      

    将得到的数字用于Pyrogram的send_message调用。

  • 建立测试账号与Bot的对话关系
    Telegram Bot默认拒绝陌生账号的消息,需先完成以下任一操作:

    • 用Pyrogram登录的测试账号,手动给目标Bot发一条消息;
    • 在@BotFather中给Bot关闭隐私模式:发送/setprivacy,选中目标Bot后选择Disable,允许Bot接收所有账号的消息。
  • 检查Pyrogram代码调用格式
    确保send_message的chat_id参数为纯数字,示例正确代码:

    from pyrogram import Client
    import asyncio
    
    async def test_send():
        app = Client("test_account")
        await app.start()
        await app.send_message(chat_id=123456789, text="E2E测试")
        await app.stop()
    
    asyncio.run(test_send())
    

    若传入的是字符串格式的username或错误ID,会触发“ID not found”错误。

  • 排查Bot的状态与权限设置
    确认目标Bot正常运行且未被封禁;检查@BotFather中的权限设置,比如/setprivacy是否开启了限制,/setjoingroups是否影响私聊(私聊场景下该设置无影响)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 13:13:37