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

Pyrogram创建频道后无法识别频道ID,发送消息报错Peer ID Invalid

Pyrogram脚本问题解决:Peer ID Invalid与会话同步异常

一、Peer ID Invalid错误解决

问题原因

  1. 频道ID格式错误:广播/超级频道的ID为负数格式(-100xxxxxxxxx),若传入的ID是正数、错误字符串格式,会触发该错误。
  2. 会话一致性问题:第二个脚本使用的会话未关联创建频道的账号,或会话文件未正确加载,导致无法识别目标频道。

解决步骤

  • 使用正确的频道ID:直接传入int类型的负数频道ID,不要用字符串。比如第一个脚本输出的chat.id是-100123456789,第二个脚本中就写:
    await worker.send_message(-100123456789, "Message")
    
  • 确保会话文件一致:两个脚本的Client()实例若未指定session_name参数,默认使用my_account.session,需保证两个脚本在同一目录下运行,且会话文件未被删除或覆盖。
  • 验证频道可访问性:在第二个脚本中先调用await worker.get_chat(频道ID),若这一步报错,说明会话无权限或ID错误,需重新确认账号和ID。

二、.session-journal文件重建问题解决

问题原因

Pyrogram通过.session-journal记录会话运行时的临时数据,只有当Client正常调用await worker.stop()关闭时,才会将journal中的数据合并到主.session文件中。你的脚本未执行关闭操作就直接退出,导致会话数据未同步,下次运行时Pyrogram会判定会话损坏,重建journal文件。

解决步骤

在两个脚本的main函数末尾添加await worker.stop(),确保会话数据正确同步:

修改后的第一个脚本

import asyncio
from pyrogram import Client

async def main():
    worker = Client()
    await worker.start()
    chat = await worker.create_channel("New channel")
    link = await worker.export_chat_invite_link(chat.id)
    print(link)
    print(chat.id)
    await worker.stop()  # 确保会话正常关闭

if __name__=='__main__':
    asyncio.run(main()) 

修改后的第二个脚本

import asyncio
from pyrogram import Client

async def main():
    worker = Client()
    await worker.start()
    # 替换为实际的负数频道ID
    await worker.send_message(-100123456789, "Message")
    print("Done")
    await worker.stop()  # 确保会话正常关闭

if __name__=='__main__':
    asyncio.run(main()) 

若会话文件已出现异常,可直接删除.session和.session-journal文件,重新运行脚本登录账号,生成正常的会话文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 02:23:15