Hello, I have this code
def send_WA(self, name='', cid='', ticketid='', number='', templateWA='', user='', pw=''): todo = { "username": user, "password": pw } headers= { "Content-Type":"application/json" } r = requests.post("API URL",json.dumps(todo),headers=headers) response = r.json() jsonString=json.dumps(response) jsonObject=json.loads(jsonString) temp_token=jsonObject['jwt']['token'] token=json.dumps(temp_token) token_final=token.strip('"') unique_token="Bearer " + token_final api_url="API URL" + templateWA todo1 = { "param":[name,cid,ticketid], "templateName":templateWA, "smsFallback":"false", "to": [number] } headers1 = { "Content-Type":"application/json", "Authorization":unique_token } response1 = requests.post(api_url, json.dumps(todo1), headers=headers1) r1 = response1.json() jsonString1=json.dumps(r1) jsonObject1=json.loads(jsonString1) temp_value = jsonObject1["data"][0]["status"] if temp_value == "failed": temp_value1 = jsonObject1["data"][0]["error"] if temp_value1 == "(#100) Invalid parameter": temp_value = "Invalid Phone Number" value = json.dumps(temp_value) final_value = value.strip('"') return final_value
And always get this error:
Traceback (most recent call last): File "/usr/lib/python3/dist-packages/odoo/tools/safe_eval.py", line 391, in safe_eval return unsafe_eval(c, globals_dict, locals_dict) File "ir.actions.server(606,)", line 7, in File "/opt/odoo/odoo-myrep/myrep_addon_ticketing/models/helpdesk_ticket.py", line 1236, in send_WA temp_value = jsonObject1["data"][0]["status"] KeyError: 'data'
ValueError: : "data" while evaluating
When I try to run the function manually (not from ODOO), it works and the response for jsonObject1 is as follows: {'status': 200, 'data': [{'to': '+628********', 'messageId': '0ffb8cfb-be5d-447f-a002-*********', 'whatsappMessageId': 'wamid.HBgNNjI4OTYzNjEwNjI4MRUCABEYEj*******', 'status': 'sent'}], 'success': 1, 'failed': 0, 'msg': 'success'}
How to resolve this problem? thanks in advance