# Buggy code
def to_dict(self):
    data = {
        "message_id": self.message_id,
        "from": self.from_user.to_dict(),
        "chat": self.chat.to_dict(),
    }
    try:
        # Python 3.3+ supports .timestamp()
        data["date"] = int(self.date.timestamp())

        if self.forward_date:
            data["forward_date"] = int(self.forward_date.timestamp())
    except AttributeError:
        # _totimestamp() for Python 3 (< 3.3) and Python 2
        data["date"] = self._totimestamp(self.date)

        if self.forward_date:
            data["forward_date"] = self._totimestamp(self.forward_date)

    if self.forward_from:
        data["forward_from"] = self.forward_from
    if self.reply_to_message:
        data["reply_to_message"] = self.reply_to_message
    if self.text:
        data["text"] = self.text
    if self.audio:
        data["audio"] = self.audio.to_dict()
    if self.document:
        data["document"] = self.document.to_dict()
    if self.photo:
        data["photo"] = [p.to_dict() for p in self.photo]
    if self.sticker:
        data["sticker"] = self.sticker.to_dict()
    if self.video:
        data["video"] = self.video.to_dict()
    if self.caption:
        data["caption"] = self.caption
    if self.contact:
        data["contact"] = self.contact.to_dict()
    if self.location:
        data["location"] = self.location.to_dict()
    if self.new_chat_participant:
        data["new_chat_participant"] = self.new_chat_participant
    if self.left_chat_participant:
        data["left_chat_participant"] = self.left_chat_participant
    if self.new_chat_title:
        data["new_chat_title"] = self.new_chat_title
    if self.new_chat_photo:
        data["new_chat_photo"] = self.new_chat_photo
    if self.delete_chat_photo:
        data["delete_chat_photo"] = self.delete_chat_photo
    if self.group_chat_created:
        data["group_chat_created"] = self.group_chat_created
    return data


# Traceback (TypeError)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/rq/worker.py", line 558, in perform_job
rv = job.perform()
File "/usr/local/lib/python2.7/dist-packages/rq/job.py", line 495, in perform
self._result = self.func(*self.args, **self.kwargs)
File "/home/joker/projects/bot/operations.py", line 50, in handle_update
handle_command(text, message)
File "/home/joker/projects/bot/operations.py", line 77, in handle_command
result = getattr(botcommands, command)(message, debug=debug)
File "/home/joker/projects/bot/config.py", line 85, in wrapper
MQTT['topic'], payload=msg.to_json(),
File "/usr/local/lib/python2.7/dist-packages/telegram/base.py", line 40, in to_json
return json.dumps(self.to_dict())
File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 178, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <telegram.user.User object at 0xf44c50> is not JSON serializable

# Fixed code
def to_dict(self):
    data = {
        "message_id": self.message_id,
        "from": self.from_user.to_dict(),
        "chat": self.chat.to_dict(),
    }
    try:
        # Python 3.3+ supports .timestamp()
        data["date"] = int(self.date.timestamp())

        if self.forward_date:
            data["forward_date"] = int(self.forward_date.timestamp())
    except AttributeError:
        # _totimestamp() for Python 3 (< 3.3) and Python 2
        data["date"] = self._totimestamp(self.date)

        if self.forward_date:
            data["forward_date"] = self._totimestamp(self.forward_date)

    if self.forward_from:
        data["forward_from"] = self.forward_from.to_dict()
    if self.reply_to_message:
        data["reply_to_message"] = self.reply_to_message
    if self.text:
        data["text"] = self.text
    if self.audio:
        data["audio"] = self.audio.to_dict()
    if self.document:
        data["document"] = self.document.to_dict()
    if self.photo:
        data["photo"] = [p.to_dict() for p in self.photo]
    if self.sticker:
        data["sticker"] = self.sticker.to_dict()
    if self.video:
        data["video"] = self.video.to_dict()
    if self.caption:
        data["caption"] = self.caption
    if self.contact:
        data["contact"] = self.contact.to_dict()
    if self.location:
        data["location"] = self.location.to_dict()
    if self.new_chat_participant:
        data["new_chat_participant"] = self.new_chat_participant
    if self.left_chat_participant:
        data["left_chat_participant"] = self.left_chat_participant
    if self.new_chat_title:
        data["new_chat_title"] = self.new_chat_title
    if self.new_chat_photo:
        data["new_chat_photo"] = self.new_chat_photo
    if self.delete_chat_photo:
        data["delete_chat_photo"] = self.delete_chat_photo
    if self.group_chat_created:
        data["group_chat_created"] = self.group_chat_created
    return data