我正在构建 WhatsApp Flow 来检索订单。我正在收到请求并成功解密消息。但我无法对 WhatsApp 的回复进行加密。 我收到错误:来自端点的响应无效。我正在使用 Python 3.9 和 Pipedream。有帮助吗?
请参阅下面我的加密代码:
from base64 import b64decode, b64encode
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import json
def handler(pd: "pipedream"):
# Getting the decrypted AES key and IV
aes_key_b64 = pd.steps["Decrypt_WhatsApp_Key"]["$return_value"]["decrypted_aes_key"]
iv_b64 = pd.steps["trigger"]["event"]["body"]["initial_vector"]
# Decoding AES key and base64 IV to bytes
aes_key = b64decode(aes_key_b64)
iv = b64decode(iv_b64)
# Preparing the inverted IV
iv_flipped = flip_iv(iv)
# Preparing response
response = {
"version": "3.0",
"screen": "SUCCESS",
"data": {
"extension_message_response": {
"params": {
"flow_token": pd.steps["Decrypt_WhatsApp_Message"]["$return_value"]["flow_token"],
"status": pd.steps["shopify_developer_app"]["$return_value"]["orders"][0]["id"]
}
}
}
}
response = json.dumps(response)
# Encrypting the response
cipher = Cipher(algorithms.AES(aes_key), modes.GCM(iv_flipped))
encryptor = cipher.encryptor()
encrypted = encryptor.update(response.encode("utf-8")) + encryptor.finalize() + encryptor.tag
encrypted_response = b64encode(encrypted).decode("utf-8")
# Response return
return {
"status": 200,
"body": encrypted_response,
"headers": {
"Content-Type": "application/json"
}
}
def flip_iv(iv):
flipped_bytes = []
for byte in iv:
flipped_byte = byte ^ 0xFF
flipped_bytes.append(flipped_byte)
return bytes(flipped_bytes)```
您需要以纯文本形式返回响应。这是 django 的示例
return HttpResponse(encrypted_response, content_type='text/plain')