T
T
Tenebrius2018-04-03 11:39:53
Python
Tenebrius, 2018-04-03 11:39:53

How to interpret messages.search Telegram API response?

The task is to get the texts of the channel's messages in Telegram.
I use a ready-made library for python - https://github.com/LonamiWebs/Telethon
Specifically, I use the method to search for messages - telethon.readthedocs.io/en/latest/extra/examples/w...

Message search code
from telethon.tl.functions.messages import SearchRequest
from telethon.tl.types import InputMessagesFilterEmpty

filter = InputMessagesFilterEmpty()
result = client(SearchRequest(
    peer=client.get_input_entity("channel_name"),      # On which chat/conversation
    q='me',      # What to search for // link to "t.me/"
    filter=filter,  # Filter to use (maybe filter for media)
    min_date=None,  # Minimum date
    max_date=None,  # Maximum date
    offset_id=0,    # ID of the message to use as offset
    add_offset=0,   # Additional offset
    limit=10,       # How many results
    max_id=0,       # Maximum message ID
    min_id=0,       # Minimum message ID
    from_id=None    # Who must have sent the message (peer)
))

Even something comes in response, but it
print(result.stringify())returns an error:
Traceback (most recent call last):
  File "telegram.py", line 55, in <module>
    print(result)
  File "D:\Progs\Puthon3\lib\encodings\cp1251.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 1011-1013: character maps to <undefined>

I tried to display resultthrough dir(), I also did not find anything similar to the text.
pprint(result)displays
telethon.tl.types.messages.ChannelMessages object at 0x05E287F0

I don't understand python at all. How to receive that messages though in any text format? More preferably in JSON.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
spikejke, 2018-04-03
@spikejke

Try: or like this:

# Откроет файл var.txt, затем запишет в него переменную result.
with open("var.txt", "w", encoding="utf-8") as f:
    f.write(result)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question