Telegram get chat messages /posts - python Telethon

后端 未结 3 2114
-上瘾入骨i
-上瘾入骨i 2021-01-11 13:08

I am using Telethon and Python 3.6xx

Been able to retreive message from groups, no problem but when it comes to channels I am stuck.

dialogs = clien         


        
3条回答
  •  猫巷女王i
    2021-01-11 13:26

    The accepted answer is good, but recent versions of Telethon let you achieve the same more easily. This will iterate over all messages in chat (for this example we use telethon.sync to avoid typing out async):

    from telethon.sync import TelegramClient
    
    with TelegramClient(name, api_id, api_hash) as client:
        for message in client.iter_messages(chat):
            print(message.sender_id, ':', message.text)
    

    Where the variables should be obvious, for example (note these API values won't work, you need your own):

    name = 'anon'
    api_id = 123
    api_hash = 'abcdefgh'
    chat = 'me'
    

    More examples using async are available in client.iter_messages documentation.

提交回复
热议问题