def postMessage(message, to_uid): print( f"Posting message to {USERS[to_uid]['fname']} {USERS[to_uid]['lname']}..." ) route = 'postMessage' url = f"{API}{route}&token={TOKEN}&uid={UID}" A = Crypto() A.load_keys(PUBKEYS[to_uid]['pubkey']) encrypted = A.encrypt(message) encrypted_bytes = base64.b64encode(encrypted) message = encrypted_bytes.decode('utf-8') payload = { 'uid': UID, 'to_uid': to_uid, 'message': message, 'token': TOKEN } headers = {'Content-Type': 'application/json'} r = requests.post(url, headers=headers, json=payload) return r.json()
def postMessage(message, to_uid): route = 'postMessage' url = f"{API}{route}&token={TOKEN}&uid={UID}" # to encrypt the message lets use the Crypto class C = Crypto() # to load the public key for encryption C.load_keys(KEYS[to_uid]['pubkey']) encryptedMessage = C.encrypt(message) # to make sure that text is in bytes format Byte_text = base64.b64encode(encryptedMessage) message = Byte_text.decode('utf-8') payload = { 'uid': UID, 'to_uid': to_uid, 'message': message, 'token': TOKEN } headers = {'Content-Type': 'application/json'} r = requests.post(url, headers=headers, json=payload) return r.json()