我正在尝试打印特定类别的所有频道。 我已经初始化了会话,用户机器人正在查看聊天记录,但不会将满足条件 If 语句要求的聊天记录打印到控制台。
当我执行时:
async for dialog in app.get_dialogs():
print(dialog.chat)
我的所有聊天都会得到这样的输出:
{
"_": "Chat",
"id": -4031246626,
"type": "ChatType.GROUP",
"is_creator": true,
"title": "comments",
"has_protected_content": false,
"members_count": 0
}
但是,当我尝试输出有关“dialog”(循环中的迭代器)的特定信息时,程序不会将任何内容输出到控制台中。这是该函数的完整代码,其中“app”是客户端:
async def main():
async with app:
print("[LOG] --- [INFO] ---> The client has Started.")
# Get all dialogs (chats) the user is involved with
try:
async for dialog in app.get_dialogs():
print(dialog.chat)
if dialog.chat.type == "ChatType.PRIVATE":
print(f"Private Chat: {dialog.chat.title} | ID: {dialog.chat.id}")
except Exception as e:
print(f"[LOG] --- [ERROR] ---> {e}")
app.run(main())
我该怎么做才能获得我想要的输出?
您的
if
语句不会触发:
if dialog.chat.type == "ChatType.PRIVATE":
类型不是这样的字符串,您应该使用以下内容:
if dialog.chat.type == 'private':
我发现了问题所在。很少有事情是错误的:
from pyrogram.enums import ChatType
if dialog.chat.type in [ChatType.GROUP, ChatType.SUPERGROUP, ChatType.CHANNEL]