【发布时间】:2021-11-30 22:58:45
【问题描述】:
我使用以下脚本来收听来自公共电报频道和群组的新消息。
import configparser
from telethon.errors import SessionPasswordNeededError
from telethon import TelegramClient, events, sync
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (
PeerChannel
)
api_id = 'xxxxxx'
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxx'
#target channels that you want to listen to:
input_channels = ('https://t.me/xxxxxx','https://t.me/xxxx','https://t.me/xxxxxxx')
#create a client
client = TelegramClient('anon', api_id, api_hash)
# Listen to messages from target channels
@client.on(events.NewMessage(chats=input_channels))
async def newMessageListener(event):
# Get message text
newMessage = event.message.message
print(newMessage)
with client:
client.run_until_disconnected()
当频道关闭时,我收到以下错误:ValueError: No user has "closed_channel_name" as username并且我停止接收任何数据。
有没有办法识别无效频道?
到目前为止,我发现以下可以识别有效频道但可能有更好的方法:
client.start()
result = client.get_entity('https://t.me/xxxxxx')
【问题讨论】:
标签: python python-3.x python-asyncio telegram telethon