3 回答
TA贡献1804条经验 获得超2个赞
如果您有权访问该频道,它就会显示在您的聊天列表中。
您必须遍历聊天以检查其标题,然后将所需的聊天存储在变量中:
my_private_channel_id = None
my_private_channel = None
async for dialog in tg.client.iter_dialogs():
if dialog.name == "private chat name":
my_private_channel = dialog
my_private_channel_id = dialog.id
break
if my_private_channel is None:
print("chat not found")
else:
print("chat id is", my_private_channel_id)
比您可以过滤发送到 my_private_channel 的消息。
TA贡献1828条经验 获得超4个赞
您可以打印您参与的所有对话/对话。您还需要从您获得的 id 中删除 -100 前缀:-1001419092328 = 1419092328 (actual ID)
from telethon import TelegramClient, events
client = TelegramClient("bot", API_ID, API_HASH)
client.start()
print("🎉 Connected")
@client.on(events.NewMessage())
async def my_event_handler(event):
async for dialog in client.iter_dialogs():
print(dialog.name, 'has ID', dialog.id) # test ID -1001419092328
client.run_until_disconnected()
如果您想收听特定频道,可以使用channel_id=1419092328. 您只会收到广播给它的消息:
from telethon import TelegramClient, events
from telethon.tl.types import PeerChannel
print(f"👉 Connecting...")
client = TelegramClient("bot", API_ID, API_HASH)
client.start()
print("🎉 Connected")
@client.on(events.NewMessage(PeerChannel(channel_id=1419092328)))
async def my_event_handler(event):
msg = event.text
print(f"[M] {msg}")
client.run_until_disconnected()
添加回答
举报