I'm trying to follow this answer using Telethon to download a specific number of messages from a telegram group. I had to modify the code because there were multiple errors and warnings and the library and its classes had also changed since then. This is what I have got so far:
import os
import sys
from telethon.sync import TelegramClient
from telethon.tl.types import InputPeerChat
session_name = "<session_name>"
api_id = <api_id>
api_hash = "<api_hash>"
chat_id = <chat_id>
os.chdir(sys.path[0])
if f"{session_name}.session" in os.listdir():
os.remove(f"{session_name}.session")
client = TelegramClient(session_name, api_id, api_hash)
await client.connect()
chat = InputPeerChat(chat_id)
client.get_messages(chat, limit=10)
however, running the above code on Jupyter I just get:
<coroutine object MessageMethods.get_messages at 0x1049c8cb0>
I tried to use the for msg in messages
part to extract/parse the information, but I get the error:
TypeError: 'coroutine' object is not iterable
I would appreciate if you could help me know what is the canonical and concise way to download the specific number of last messages in a telegram group given the chat ID.