【问题标题】:Telethon reading message from Channel with id来自具有 id 的频道的 Telethon 阅读消息
【发布时间】:2021-08-20 11:30:09
【问题描述】:

您好,我使用的是 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


【解决方案1】:
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()

这是正确而简单的方法。

【讨论】:

    猜你喜欢
    • 2019-11-26
    • 2022-12-16
    • 2017-03-04
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多