Search code examples
python-3.xfacebookchatmessenger

How to convert payload to human redeable form


I have been programming a program using fbchat and found an interesting function that appealed me

class listen(fbchat.Client):
    def onMessageUnsent(
        self,
        mid=None,
        author_id=None,
        thread_id=None,
        thread_type=None,
        ts=None,
        msg=None,
    ):
        print(msg)
    

client = listen('','',session_cookies=cookies)
client.listen()

and It gives the following output but how do I convert it to human redeable form...?

{'payload': [123, 34, 100, 101, 108, 116, 97, 115, 34, 58, 91, 123, 34, 100, 101, 108, 116, 97,
 82, 101, 99, 97, 108, 108, 77, 101, 115, 115, 97, 103, 101, 68, 97, 116, 97, 34, 58, 123, 34,
 116, 104, 114, 101, 97, 100, 75, 101, 121, 34, 58, 123, 34, 111, 116, 104, 101, 114, 85, 115,
 101, 114, 70, 98, 73, 100, 34, 58, 49, 48, 48, 48, 52, 52, 53, 55, 50, 49, 57, 50, 57, 48, 54, 
125, 44, 34, 109, 101, 115, 115, 97, 103, 101, 73, 68, 34, 58, 34, 109, 105, 100, 46, 36, 99, 
65, 65, 66, 97, 95, 88, 69, 118, 56, 73, 112, 55, 121, 77, 120, 76, 114, 86, 49, 109, 100, 87,
 85, 49, 112, 48, 70, 108, 34, 44, 34, 100, 101, 108, 101, 116, 105, 111, 110, 84, 105, 109, 
101, 115, 116, 97, 109, 112, 34, 58, 49, 54, 48, 52, 54, 51, 55, 48, 57, 54, 53, 54, 56, 44, 34,
 115, 101, 110, 100, 101, 114, 73, 68, 34, 58, 49, 48, 48, 48, 52, 52, 53, 55, 50, 49, 57, 50, 
57, 48, 54, 44, 34, 109, 101, 115, 115, 97, 103, 101, 84, 105, 109, 101, 115, 116, 97, 109, 112,
 34, 58, 48, 125, 125, 93, 125], 'class': 'ClientPayload'}

what does it even mean...?
This isn't either base64 or hexadecimal...


Solution

  • It's a list of ASCII codes. Try this:

    "".join(map(chr, msg["payload"]))
    

    The result is:

    '{"deltas":[{"deltaRecallMessageData":{"threadKey":{"otherUserFbId":100044572192906},"messageID":"mid.$cAABa_XEv8Ip7yMxLrV1mdWU1p0Fl","deletionTimestamp":1604637096568,"senderID":100044572192906,"messageTimestamp":0}}]}'
    

    which looks like a JSON string you can parse using json.loads(...), for example:

    import json
    import pprint
    
    # Fetch msg here using the code in the question body 
    
    json_string = "".join(map(chr, msg["payload"]))
    d = json.loads(json_string)
    pprint.pprint(d)