pythonjsontelethon

how to convert telethon object to valid JSON in python


I using telethon package, for connecting and getting data from telegram. I want to convert Message object to valid JSON. for example, I get this object from the one API in telethon :

{
Message: {
    via_bot_id: None,
    entities: [],
    out: None,
    post: True,
    from_id: None,
    message: "hi everybody",
    id: 71,
    media_unread: None,
    reply_markup: None,
    fwd_from: {
        channel_id: 1119999790,
        channel_post: 2,
        date: 2017 - 09 - 04 15: 43: 48,
        from_id: None
    },
    reply_to_msg_id: None,
    edit_date: None,
    to_id: {
        channel_id: 1099583379
    },
    views: 2,
    mentioned: None,
    date: 2017 - 09 - 05 09: 28: 46,
    media: None,
    silent: None
} }

and this is my favorite result:

{
"Message": {
    "via_bot_id": "None",
    "entities": [],
    "out": "None",
    "post": "True",
    "from_id": "None",
    "message": "hi everybody",
    "id": 71,
    "media_unread": "None",
    "reply_markup": "None",
    "fwd_from": {
        "channel_id": 1119999790,
        "channel_post": 2,
        "date": "2017 - 09 - 04 15: 43: 48",
        "from_id": "None"
    },
    "reply_to_msg_id": "None",
    "edit_date": "None",
    "to_id": {
        "channel_id": 1099583379
    },
    "views": 2,
    "mentioned": "None",
    "date": "2017 - 09 - 05 09: 28: 46",
    "media": "None",
    "silent": "None"
}}

Is there a way to convert in Python?


Solution

  • I solved my problem this way

    I hope it will be useful for you too

    for example, I get channel object and convert it to JSON.

    index.py

    import json
    
    from telethon import TelegramClient
    from telethon.tl.functions.channels import GetFullChannelRequest
    from telethon.tl.types import ChannelParticipantsSearch
    
    from helpers import date_format
    
    api_id = XXXXX
    api_hash = 'XXXXXXXXXXXXXXXXXXXXXX'
    phone_number = '+98XXXXXXXXXXX'
    ################################################
    channel_username = 'tehrandb'
    ################################################
    
    client = TelegramClient('session_name',
                        api_id,
                        api_hash)
    
    assert client.connect()
    if not client.is_user_authorized():
    client.send_code_request(phone_number)
    me = client.sign_in(phone_number, input('Enter code: '))
    
    # ---------------------------------------
    my_filter = ChannelParticipantsSearch('')
    all_participants = []
    while_condition = True
    # ---------------------------------------
    channel = client(GetFullChannelRequest(channel_username))
    channel_dict = channel.to_dict()
    channel_as_json = json.dumps(channel_dict,default=date_format)
    

    and helpers.py

    import datetime
    
    
    def date_format(message):
        """
        :param message:
        :return:
        """
        if isinstance(message, datetime.datetime):
            return message.strftime("%Y-%m-%d %H:%M:%S")