eternnoir / pyTelegramBotAPI

Python Telegram bot api.
GNU General Public License v2.0
8.11k stars 2.03k forks source link

"Object of type User is not JSON serializable" when using User in MessageEntity #2284

Closed MAH69IK closed 5 months ago

MAH69IK commented 5 months ago

Please answer these questions before submitting your issue. Thanks!

  1. What version of pyTelegramBotAPI are you using? 4.18.1

  2. What OS are you using? Linux

  3. What version of python are you using? 3.12.3

I want to use entity to mention a user. What I'm doing:

#!/usr/bin/env python

from telebot import types
from telebot import TeleBot

bot = TeleBot('...')

u = types.User(1...6, False, 'K...a')
print(u)
e = types.MessageEntity('text_mention', 0, len('🍌 '), user=u)
print(e)
bot.send_message(-4...0, '🍌 ', entities=[e])

What I'm getting:

{'id': 1...6, 'is_bot': False, 'first_name': 'K...a', 'username': None, 'last_name': None, 'language_code': None, 'can_join_groups': None, 'can_read_all_group_messages': None, 'supports_inline_queries': None, 'is_premium': None, 'added_to_attachment_menu': None, 'can_connect_to_business': None}
{'type': 'text_mention', 'offset': 0, 'length': 2, 'url': None, 'user': {'id': 1...6, 'is_bot': False, 'first_name': 'K...a', 'username': None, 'last_name': None, 'language_code': None, 'can_join_groups': None, 'can_read_all_group_messages': None, 'supports_inline_queries': None, 'is_premium': None, 'added_to_attachment_menu': None, 'can_connect_to_business': None}, 'language': None, 'custom_emoji_id': None}
Traceback (most recent call last):
  File "/tmp/a.py", line 16, in <module>
    bot.send_message(-4...0, '🍌 ', entities=[e])
  File "/home/.../lib/python3.12/site-packages/telebot/__init__.py", line 1751, in send_message
    apihelper.send_message(
  File "/home/.../lib/python3.12/site-packages/telebot/apihelper.py", line 262, in send_message
    payload['entities'] = json.dumps(types.MessageEntity.to_list_of_dicts(entities))
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type User is not JSON serializable

Judging by the second print() - I'm fine creating a User object as part of a MessageEntity, but as you can see, this causes an error. But if I replace this object with a dictionary - everything starts working:

#e = types.MessageEntity('text_mention', 0, len('🍌 '), user=u)
e = types.MessageEntity('text_mention', 0, len('🍌 '), user={'id': 1...6, 'is_bot': False, 'first_name': 'K...a'})
Badiboy commented 5 months ago

We'll take a look.

coder2020official commented 5 months ago

@Badiboy I think we need to call the to_dict method of types.User here: https://github.com/eternnoir/pyTelegramBotAPI/blob/master/telebot/types.py#L1656

Badiboy commented 5 months ago

https://github.com/eternnoir/pyTelegramBotAPI/pull/2288