Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Object of type InlineQueryResultArticle is not JSON serializable #3358

Closed
hsh01 opened this issue Nov 11, 2022 · 5 comments
Closed

[BUG] Object of type InlineQueryResultArticle is not JSON serializable #3358

hsh01 opened this issue Nov 11, 2022 · 5 comments
Labels
Milestone

Comments

@hsh01
Copy link

hsh01 commented Nov 11, 2022

Steps to Reproduce

Trying to send data from webapp to bot with

        bot.answerWebAppQuery(
            web_app_query_id=str(payload.query_id),
            result=InlineQueryResultArticle(
                id=str(uuid.uuid4()),
                title="Caps",
                input_message_content=InputTextMessageContent('text'),
            )
        )

Expected behaviour

no error

Actual behaviour

but got error Object of type InlineQueryResultArticle is not JSON serializable

Operating System

windows 11 x64

Version of Python, python-telegram-bot & dependencies

python-telegram-bot 13.14 (v2.0.1-78-g9483410)
Bot API 6.2
certifi2022.06.15
Python 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)]

Relevant log output

No response

Additional Context

No response

@hsh01 hsh01 added the bug 🐛 label Nov 11, 2022
@Bibo-Joshi
Copy link
Member

Hi. Can you please show the full traceback?

@hsh01
Copy link
Author

hsh01 commented Nov 12, 2022

Object of type InlineQueryResultArticle is not JSON serializable
  File "C:\dev\gpn\takeaway_app\takeaway\api.py", line 119, in create_order
    bot.answerWebAppQuery(
  File "C:\dev\gpn\takeaway_app\venv\lib\site-packages\telegram\bot.py", line 133, in decorator
    result = func(*args, **kwargs)
  File "C:\dev\gpn\takeaway_app\venv\lib\site-packages\telegram\bot.py", line 3944, in answer_web_app_query
    api_result = self._post(
  File "C:\dev\gpn\takeaway_app\venv\lib\site-packages\telegram\bot.py", line 298, in _post
    return self.request.post(
  File "C:\dev\gpn\takeaway_app\venv\lib\site-packages\telegram\utils\request.py", line 364, in post
    body=json.dumps(data).encode('utf-8'),
  File "C:\Users\Shamil\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "C:\Users\Shamil\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Users\Shamil\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "C:\Users\Shamil\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '

image

@hsh01
Copy link
Author

hsh01 commented Nov 12, 2022

Finally, I found the working state, but I'm not sure about the correct logic for interacting with ptb

        bot.answerWebAppQuery(
            web_app_query_id=str(payload.query_id),
            result=InlineQueryResultArticle(
                id=str(uuid.uuid4()),
                title="Caps",
                input_message_content=InputTextMessageContent(
                    message_text='text',
                    parse_mode=telegram.ParseMode.MARKDOWN, #   default values not working, need to set manual
                    disable_web_page_preview=True   #   default values not working, need to set manual
                ),
            ).to_json() # convert to json here 
        )

@Bibo-Joshi
Copy link
Member

Thanks for the updates. I can reproduce. The method is indeed faulty both on v13 and on v20.

Missing serialization on v13

data: JSONDict = {'web_app_query_id': web_app_query_id, 'result': result}

In this line, we should have result.to_dict(). The corresponding unit test is also missing that.

Defaults handling on both versions

Both versions miss setting the default values for the inline result (and even removing the DEFAULT_NONE references). We have corresponding code already in place that we can reuse. On v13, that's function _set_defaults currently defined as local function within answer_inline_query. On v20, we have _insert_defaults_for_ilq_results. We'd have to call these methods and also add corresponding uni tests

Actions items

We definitely need a fix for the defaults behavior on v20.
Since these bugs are related to a bot method, I'd also vote to fix both on v13, as that can be seen to fall under "API compatibility" IMO.

@Bibo-Joshi Bibo-Joshi added this to the v20.0a5 milestone Nov 12, 2022
@Poolitzer
Copy link
Member

Definitely fix it on V13.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants