LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.95k stars 1.4k forks source link

AttributeError when sending SearchRequest #184

Closed gurland closed 7 years ago

gurland commented 7 years ago

Code snippet:

dialogs, entities = client.get_dialogs(10)
entity = entities[0]

result = client(SearchRequest(
    entity,
    q='',
    filter=InputMessagesFilterEmpty,
    min_date=0,
    max_date=0,
    offset=0,
    max_id=0,
    limit=10,
    from_id=InputUserSelf
))

And traceback:

Traceback (most recent call last):
  File "/home/gurland/Projects/tg_deleter/main.py", line 50, in <module>
    from_id=InputUserSelf
  File "/home/gurland/Projects/ve/bot_test/lib/python3.5/site-packages/telethon/tl/functions/messages/search.py", line 34, in __init__
    self.from_id = get_input_user(from_id)
  File "/home/gurland/Projects/ve/bot_test/lib/python3.5/site-packages/telethon/utils.py", line 107, in get_input_user
    if type(entity).subclass_of_id == 0xe669bf46:  # crc32(b'InputUser')
AttributeError: type object 'type' has no attribute 'subclass_of_id'

Documentation says that from_id supports InputUserSelf constructor

Lonami commented 7 years ago

Documentation says that from_id supports InputUserSelf constructor

Yeah, an instance.

You're passing the class, InputMessagesFilterEmpty and InputUserSelf itself, not an instance (in Python, you would do InputMessagesFilterEmpty() and InputUserSelf()).

msus commented 7 years ago

So what else need to be done to call that method?

dialogs, entities = client.get_dialogs(10)
entity = entities[0]

me = InputUserSelf()
filter = InputMessagesFilterEmpty()

result = client(SearchRequest(
    entity,
    q='',
    filter=filter,
    min_date=0,
    max_date=0,
    offset=0,
    max_id=0,
    limit=10,
    from_id=me
))

Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
  File "C:\Users\*\Desktop\Telethon-master\telethon\telegram_client.py", line 199, in invoke
    request, updates=updates
  File "C:\Users\*\Desktop\Telethon-master\telethon\telegram_bare_client.py", line 283, in invoke
    self._sender.send(request)
  File "C:\Users\*\Desktop\Telethon-master\telethon\network\mtproto_sender.py", line 60, in send
    request.on_send(writer)
  File "C:\Users\*\Desktop\Telethon-master\telethon\tl\functions\messages\search.py", line 68, in on_send
    writer.tgwrite_date(self.min_date)
  File "C:\Users\*\Desktop\Telethon-master\telethon\extensions\binary_writer.py", line 100, in tgwrite_date
    value = 0 if datetime is None else int(datetime.timestamp())
AttributeError: 'int' object has no attribute 'timestamp'
Lonami commented 7 years ago

min_date should be a date, not an integer. You can leave it as None. Under the hood, they are indeed timestamps but working with Python's date object is more comfortable.

msus commented 7 years ago

OK cool, I got it working (example for whoever reads this page:)

search = 'hello'
chat = InputPeerChannel(id, access_hash)
filter = InputMessagesFilterEmpty()
result = client(SearchRequest(
    chat,
    q=search,
    filter=filter,
    min_date=None,
    max_date=None,
    offset=0,
    max_id=0,
    limit=1,
    from_id=InputUserEmpty()
))

But why can't I search in supergroups? When assigning ID and ACCESS_HASH of a supergroup I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\telethon\telegram_client.py", line 205, in invoke
    request, updates=updates
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\telethon\telegram_bare_client.py", line 276, in invoke
    self._sender.receive(request, updates=updates)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\telethon\network\mtproto_sender.py", line 109, in receive
    remote_msg_id, remote_seq, reader, updates)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\telethon\network\mtproto_sender.py", line 207, in _process_msg
    return self._handle_rpc_result(msg_id, sequence, reader)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\telethon\network\mtproto_sender.py", line 355, in _handle_rpc_result
    raise error
telethon.errors.rpc_errors_400.ChatAdminRequiredError: (ChatAdminRequiredError(...), 'Chat admin privileges are required to do that in the specified chat (for example, to send a message in a channel which is not yours).')

(when I make a search from the app \ web client it works) Any Idea?

EDIT: Just found out SearchGlobalRequest helps! EDIT # 2 this method returns results from all chats..? this is confuzing is there a way to get search results from only one group which is also a supergroup?