电报获取聊天消息/帖子-Python Telethon [英] Telegram get chat messages /posts - python Telethon

查看:894
本文介绍了电报获取聊天消息/帖子-Python Telethon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Telethon和Python 3.6xx

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 = client(get_dialogs)
for chat in dialogs.chats:
   getmessage = client.get_messages(chat.id, limit=400)
   for message in getmessage:
        print(message.message)

我已经搜索了telethon文档,但是大多数答案都是对旧版get_message_history的回应.

I've searched the telethon documentation but most answers were in response to the old get_message_history.

当我尝试以下chat.id = 1097988869(news.bitcoin.com)时,出现以下错误(对于组,chat.id正常工作):

When I'm trying with the following chat.id = 1097988869 (news.bitcoin.com) I'm getting an error below (for groups the chat.id works fine):

PeerIdInvalidError:使用了无效的Peer.确保传递正确的对等类型

PeerIdInvalidError: An invalid Peer was used. Make sure to pass the right peer type

推荐答案

您可以使用以下代码获取消息:

you can use this code for get messages :

client = TelegramClient('session_name',
                    api_id,
                    api_hash,
                    update_workers=1,
                    spawn_read_thread=False)
assert client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone_number)
    me = client.sign_in(phone_number, input('Enter code: '))

channel_username='tehrandb' # your channel
channel_entity=client.get_entity(channel_username)
posts = client(GetHistoryRequest(
    peer=channel_entity,
    limit=100,
    offset_date=None,
    offset_id=0,
    max_id=0,
    min_id=0,
    add_offset=0,
    hash=0))
# messages stored in `posts.messages`

这篇关于电报获取聊天消息/帖子-Python Telethon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆