Код: Выделить всё
from telethon.sync import TelegramClient
import pandas as pd
import asyncio
import time
api_id = '******' # substitute with your API ID
api_hash = '********' # substitute with your API HASH
phone = '+*******' # substitute with your phone number
client = TelegramClient('anon', api_id, api_hash)
data = {
'user_id' : [],
'message_timestamp' : [],
'message_text' : [],
'message_media_type' : []
}
async def collect_messages(client):
chat = await client.get_input_entity('-*******') # substitute with actual chat id
# Initialize variable to store last message id
last_msg_id = None
limit = 100 # Fetch 100 messages at a time
for _ in range(10): # 10*100=1000 messages
# Getting messages from the chat
messages = await client.get_messages(chat, limit=limit, max_id=last_msg_id)
time.sleep(1) # To respect rate limits
for message in messages:
user_id = message.sender_id
message_timestamp = message.date
message_text = message.text
if message.media is not None:
# Media information without downloading the file
message_media_type = message.media.__class__.__name__
else:
message_media_type = None
data['user_id'].append(user_id)
data['message_timestamp'].append(message_timestamp)
data['message_text'].append(message_text)
data['message_media_type'].append(message_media_type)
# Save the id of the last message to fetch next set of messages
last_msg_id = messages[-1].id
# Run the async function
with client:
client.loop.run_until_complete(collect_messages(client))
df = pd.DataFrame(data)
df.to_excel('telegram_messages.xlsx', index=False)
Подробнее здесь: https://stackoverflow.com/questions/772 ... hat-partic
Мобильная версия