简体   繁体   English

Telegram 获取聊天消息/帖子 - python Telethon

[英]Telegram get chat messages /posts - python Telethon

I am using Telethon and Python 3.6xx我正在使用 Telethon 和 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)

I've searched the telethon documentation but most answers were in response to the old get_message_history .我搜索了 Telethon 文档,但大多数答案都是对旧的get_message_history

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):当我尝试使用以下chat.id = 1097988869 (news.bitcoin.com) 时,出现以下错误(对于群组, chat.id工作正常):

PeerIdInvalidError: An invalid Peer was used. PeerIdInvalidError: 使用了无效的 Peer。 Make sure to pass the right peer type确保传递正确的对等类型

The accepted answer is good, but recent versions of Telethon let you achieve the same more easily.接受的答案很好,但最近版本的 Telethon 让您更轻松地实现相同的目标。 This will iterate over all messages in chat (for this example we use telethon.sync to avoid typing out async ):这将遍历chat所有消息(在这个例子中,我们使用telethon.sync来避免输入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):例如,变量应该很明显(注意这些 API 值不起作用,您需要自己的值):

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

More examples using async are available in client.iter_messages documentation. client.iter_messages文档中提供了更多使用async示例。

update :更新 :

in the new version of Telethon, @Lonami answer is best and use it.在新版本的 Telethon 中,@Lonami 回答是最好的并使用它。

############################################################ ############################################### ###########

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`

that work for me!这对我有用!

api_hash from https://my.telegram.org , under API Development. api_hash 来自https://my.telegram.org ,在 API Development 下。

from telethon import TelegramClient, events, sync

api_id = 'your api_id'
api_hash = 'your api_hash'

client = TelegramClient('session_name', api_id, api_hash)
client.start()
channel_username = 'username'# your channel
for message in client.get_messages(channel_username, limit=10):
    print(message.message)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM