Telethon icon indicating copy to clipboard operation
Telethon copied to clipboard

Sending documents for inline results fails

Open NightMachinery opened this issue 4 years ago • 13 comments

Checklist

  • [x] The error is in the library's code, and not in my own.
  • [x] I have searched for this issue before posting it and there isn't a duplicate.
  • [x] I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/master.zip and triggered the bug in the latest version.

I’m trying to send a file for an inline request, but it fails with this:

ERROR:telethon.client.updates:Unhandled exception on handler
Traceback (most recent call last):
  File "/home/eva/miniconda3/lib/python3.7/site-packages/telethon/client/updates.py", line 428, in _dispatch_update
    await callback(event)
  File "stdplugins/advanced_get.py", line 56, in handler
    await event.answer([rfiles[-1]]) #returns true
  File "/home/eva/miniconda3/lib/python3.7/site-packages/telethon/events/inlinequery.py", line 234, in answer
    switch_pm=switch_pm
  File "/home/eva/miniconda3/lib/python3.7/site-packages/telethon/client/users.py", line 74, in call
    result = await future
telethon.errors.rpcerrorlist.ResultTypeInvalidError: Result type invalid (caused by SetInlineBotResultsRequest)

Here is how I build the result:

builder.document(file_path, type='document')

I tried settingtext=‘sth’and unsetting type and adding a description and title, too, but none of these changed the error.

If I’m understanding https://core.telegram.org/bots/api#inlinequeryresultdocument correctly, this should be possible, though only for .pdf and .zip.

NightMachinery avatar Apr 01 '20 12:04 NightMachinery

I have tried to send audio (mp3) files, too, with no success. I have tried builder.document(file_path, type='audio') and builder.document(http_url_of_file, type='audio'). How can I use the raw API to send requests? I want to send a https://core.telegram.org/bots/api#inlinequeryresultaudio, but I don't know how to do that with Telethon ...

NightMachinery avatar Apr 01 '20 13:04 NightMachinery

If it is still relevant, try setting your document in the results array: builder.document(results=[file_path, type='document'])

MrSnowball-dev avatar Aug 27 '20 23:08 MrSnowball-dev

@MrSnowball-dev commented on Aug 28, 2020, 4:12 AM GMT+4:30:

If it is still relevant, try setting your document in the results array:
builder.document(results=[file_path, type='document'])

Is there a typo in that? Your suggestion raises a syntax error. I tried:

    rfiles.append(builder.document(results=[file_add], type='document'))
TypeError: document() got an unexpected keyword argument 'results'

NightMachinery avatar Aug 28 '20 22:08 NightMachinery

Using rfiles.append(builder.document([file_add], type='document')):

ERROR:telethon.client.updates:Unhandled exception on handler
Traceback (most recent call last):
  File "/home/eva/miniconda3/lib/python3.7/site-packages/telethon/utils.py", line 327, in get_input_document
    if document.SUBCLASS_OF_ID == 0xf33fdb68:  # crc32(b'InputDocument'):
AttributeError: 'list' object has no attribute 'SUBCLASS_OF_ID'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/eva/miniconda3/lib/python3.7/site-packages/telethon/tl/custom/inlinebuilder.py", line 198, in document
    fh = utils.get_input_document(file)
  File "/home/eva/miniconda3/lib/python3.7/site-packages/telethon/utils.py", line 330, in get_input_document
    _raise_cast_fail(document, 'InputDocument')
  File "/home/eva/miniconda3/lib/python3.7/site-packages/telethon/utils.py", line 139, in _raise_cast_fail
    type(entity).__name__, target))
TypeError: Cannot cast list to any kind of InputDocument.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/eva/miniconda3/lib/python3.7/site-packages/telethon/utils.py", line 442, in get_input_media
    if media.SUBCLASS_OF_ID == 0xfaf846f4:  # crc32(b'InputMedia')
AttributeError: 'NoneType' object has no attribute 'SUBCLASS_OF_ID'

NightMachinery avatar Aug 28 '20 22:08 NightMachinery

Is there a typo in that?

Oh yeah sorry, there is a typo, you should use this as an answer: await event.answer(results=[builder.document(file, type='document')])

MrSnowball-dev avatar Aug 28 '20 22:08 MrSnowball-dev

Is there a typo in that?

Oh yeah sorry, there is a typo, you should use this as an answer: await event.answer(results=[builder.document(file, type='document')])

What should file be? I tried this whole thing in the opening post of the issue. It errs:

ERROR:telethon.client.updates:Unhandled exception on handler
Traceback (most recent call last):
  File "/home/eva/miniconda3/lib/python3.7/site-packages/telethon/client/updates.py", line 443, in _dispatch_update
    await callback(event)
  File "stdplugins/advanced_get.py", line 88, in handler
    await event.answer(rfiles, cache_time=0, private=True)  # returns true
  File "/home/eva/miniconda3/lib/python3.7/site-packages/telethon/events/inlinequery.py", line 233, in answer
    switch_pm=switch_pm
  File "/home/eva/miniconda3/lib/python3.7/site-packages/telethon/client/users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
  File "/home/eva/miniconda3/lib/python3.7/site-packages/telethon/client/users.py", line 77, in _call
    result = await future
telethon.errors.rpcerrorlist.ResultTypeInvalidError: Result type invalid (caused by SetInlineBotResultsRequest)

NightMachinery avatar Aug 28 '20 22:08 NightMachinery

Depends on what file you want to send, for type document I believe it is only PDF or ZIP.

MrSnowball-dev avatar Aug 28 '20 22:08 MrSnowball-dev

@MrSnowball-dev commented on Aug 29, 2020, 3:23 AM GMT+4:30:

Depends on what file you want do send, for type document I believe it is only PDF or ZIP.

I have tried it with PDFs. It still fails. Can you show me a code sample where you can send a PDF?

PS: @toolkitbot is able to send any kind of file as far as I have tested it, though Telegram docs do indeed say only PDF and zip.

NightMachinery avatar Aug 28 '20 23:08 NightMachinery

I use pretty much the same snippet to send .TGS stickers via inline with type sticker. Should work that way for any documents with correct type I believe.

Also, are you using switch_pm? Do you also pass switch_pm_param, because otherwise the answer wouldn't work.

MrSnowball-dev avatar Aug 28 '20 23:08 MrSnowball-dev

@MrSnowball-dev commented on Aug 29, 2020, 4:22 AM GMT+4:30:

I use pretty much the same snippet to send .TGS stickers via inline with type sticker. Should work that way for any documents with correct type I believe.

Also, are you using switch_pm? Do you also pass switch_pm_param, because otherwise answer wouldn't work.

Here is my code. I don't use switch_pm.

@borg.on(events.InlineQuery)
async def handler(event):
    query = event.text
    m = p.match(query)
    if (not await util.isAdmin(event)) or m == None:
        return

    builder = event.builder
    command = m.group(2)
    shell = True
    cwd = util.dl_base + "Inline " + str(uuid.uuid4()) + '/'
    Path(cwd).mkdir(parents=True, exist_ok=True)
    sp = (subprocess.run(command,
                         shell=shell,
                         cwd=cwd,
                         text=True,
                         executable='zsh' if shell else None,
                         stderr=subprocess.STDOUT,
                         stdout=subprocess.PIPE))
    output = sp.stdout
    output = f"The process exited {sp.returncode}." if output == '' else output

    rtext = builder.article('Text', text=output, link_preview=False)
    rfiles = [rtext]
    files = list(Path(cwd).glob('*'))
    files.sort()
    for f in files:
        if not f.is_dir():
            file_add = f.absolute()
            base_name = str(os.path.basename(file_add))
            ext = f.suffix
            rfiles.append(builder.document(file_add, type='document', title="boo"))

    await event.answer(rfiles, cache_time=0, private=True)

I think Lonami used this only for stickers, too, and it might not work for zip/pdf. Can you test with a pdf file?

NightMachinery avatar Aug 29 '20 00:08 NightMachinery

For anyone investigating this issue, one might one to test this with Telegram's HTTP bot API. If the API doesn't support this, it's possible that the Telegram server simply does not allow a particular request (and can't be made work in this library). If the API does support it, then there has to be a way to get this to work in Telethon.

Lonami avatar Aug 29 '20 09:08 Lonami

For anyone investigating this issue, one might one to test this with Telegram's HTTP bot API. If the API doesn't support this, it's possible that the Telegram server simply does not allow a particular request (and can't be made work in this library). If the API does support it, then there has to be a way to get this to work in Telethon.

I got my bot working using another API wrapper: https://gitlab.com/NightMachinary/betterborg/-/blob/master/inline.py

This is a Telethon issue, I think.

E6FB44CB-00F3-420E-A701-437251BD3426 D266640D-7CB2-4E8E-8133-F53E961D6C7E

NightMachinery avatar Aug 29 '20 13:08 NightMachinery

The friendly method Telethon offers may be doing something incorrectly. However, it has to be possible to do it correctly with raw API. Figuring out how is the first step, fixing it in the friendly method is next.

Lonami avatar Aug 29 '20 15:08 Lonami