Please answer these questions before submitting your issue. Thanks!
What version of pyTelegramBotAPI are you using? 4.18.1
What OS are you using? Linux
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:
Please answer these questions before submitting your issue. Thanks!
What version of pyTelegramBotAPI are you using?
4.18.1
What OS are you using?
Linux
What version of python are you using?
3.12.3
I want to use entity to mention a user. What I'm doing:
What I'm getting:
Judging by the second
print()
- I'm fine creating aUser
object as part of aMessageEntity
, but as you can see, this causes an error. But if I replace this object with a dictionary - everything starts working: