1

I need to pass the Telegram message object between multiple AWS Lambda functions, so I need to serialize it. I found that it's possible using jsonpickle module, but I'm getting an AttributeError during decoding.

This is how it works without serializing:

>>> from telegram import Bot, Update
>>> bot = Bot('XXXXXXXX')
>>> update = Update.de_json({...}, bot)
>>> type(update)
<class 'telegram.update.Update'>
>>> dir(update)
['ALL_TYPES', 'CALLBACK_QUERY', 'CHANNEL_POST', 'CHAT_JOIN_REQUEST', 'CHAT_MEMBER', 'CHOSEN_INLINE_RESULT', 'EDITED_CHANNEL_POST', 'EDITED_MESSAGE', 'INLINE_QUERY', 'MESSAGE', 'MY_CHAT_MEMBER', 'POLL', 'POLL_ANSWER', 'PRE_CHECKOUT_QUERY', 'SHIPPING_QUERY', '__annotations__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slotnames__', '__slots__', '__str__', '__subclasshook__', '_effective_chat', '_effective_message', '_effective_user', '_id_attrs', '_parse_data', 'callback_query', 'channel_post', 'chat_join_request', 'chat_member', 'chosen_inline_result', 'de_json', 'de_list', 'edited_channel_post', 'edited_message', 'effective_chat', 'effective_message', 'effective_user', 'inline_query', 'message', 'my_chat_member', 'poll', 'poll_answer', 'pre_checkout_query', 'shipping_query', 'to_dict', 'to_json', 'update_id']
>>> update.message.reply_text('hello')
<telegram.message.Message object at 0x7ff6282da788>

Then I'm trying to serialize the update object:

>>> e_update = jsonpickle.encode(update)
>>> e_update
'{"py/object": "telegram.update.Update"}'

Then deserialize and invoke:

>>> d_update = jsonpickle.decode(e_update)
>>> type(d_update)
<class 'telegram.update.Update'>
>>> dir(d_update)
['ALL_TYPES', 'CALLBACK_QUERY', 'CHANNEL_POST', 'CHAT_JOIN_REQUEST', 'CHAT_MEMBER', 'CHOSEN_INLINE_RESULT', 'EDITED_CHANNEL_POST', 'EDITED_MESSAGE', 'INLINE_QUERY', 'MESSAGE', 'MY_CHAT_MEMBER', 'POLL', 'POLL_ANSWER', 'PRE_CHECKOUT_QUERY', 'SHIPPING_QUERY', '__annotations__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slotnames__', '__slots__', '__str__', '__subclasshook__', '_effective_chat', '_effective_message', '_effective_user', '_id_attrs', '_parse_data', 'callback_query', 'channel_post', 'chat_join_request', 'chat_member', 'chosen_inline_result', 'de_json', 'de_list', 'edited_channel_post', 'edited_message', 'effective_chat', 'effective_message', 'effective_user', 'inline_query', 'message', 'my_chat_member', 'poll', 'poll_answer', 'pre_checkout_query', 'shipping_query', 'to_dict', 'to_json', 'update_id']
>>> d_update.ALL_TYPES
['message', 'edited_message', 'channel_post', 'edited_channel_post', 'inline_query', 'chosen_inline_result', 'callback_query', 'shipping_query', 'pre_checkout_query', 'poll', 'poll_answer', 'my_chat_member', 'chat_member', 'chat_join_request']
>>> d_update.message
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: message

As you can see, after jsonpickle I can't execute the same method call.

Can't find anything, do you have any ideas?

jumpy
  • 317
  • 1
  • 12

0 Answers0