远程朗读带有 id 的频道消息

问题描述 投票:0回答:2

您好,我正在使用 Telethon 1.21.1 这里最多的问题已经过时了。

此脚本任务是读取每个 id 的特定频道的消息。

我不确定在哪里传递频道信息以及如何使用该方法以正确的方式读取消息。

await
但我不知道如何实现它

这就是我所拥有的:

my_private_channel_id = "-100777000"
my_private_channel = "test"

api_id = # 7 Digit Telegram API ID.
api_hash = ''   # 32 Character API Hash
phone = '+'   #Enter Your Mobilr Number
client = TelegramClient(phone, api_id, api_hash)
async def main():
    await client.send_message('me', 'Hello !!!!') # just to test connection
with client:
    client.loop.run_until_complete(main())
client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone)
    client.sign_in(phone, input('Enter verification code: '))

chats = []
last_date = None
chunk_size = 200
channels=[] #target channel
result = client(GetDialogsRequest(
             offset_date=last_date,
             offset_id=0,
             offset_peer=InputPeerEmpty(),
             limit=chunk_size,
             hash = 0
         ))
chats.extend(result.chats)

for chat in chats:
    try:
        if chat.channels== True:
            readmsg =  client.get_messages(chat, None)
    except:
        continue

python telegram telethon
2个回答
4
投票
from telethon import TelegramClient, events

client = TelegramClient('session', api_id, api_hash)

@client.on(events.NewMessage(chats="@TelethonUpdates"))
async def my_event_handler(event):
   print(event.text)

client.start()
client.run_until_disconnected()

这是正确又简单的方法。


0
投票

我用这个

channel = await client.get_entity(PeerChannel(-xxxxxxxxxx))
# ids: int or list(int)
messages = await client.get_messages(channel, ids=[1234, 4564])
for msg in messages:
    print(msg)
© www.soinside.com 2019 - 2024. All rights reserved.