Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
1238 Widoki

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


Awatar
Odrzuć
Najlepsza odpowiedź

Hi,
I hope you know that the error is coming as data key is missing in the variable jsonObject1 , this may be due to many reasons, for example lack for proper authentication etc

What you can do to solve the issue is try to execute the same code from the postman application, see if it works from there, if yes, take the python code from the postman and using that python code try to execute from odoo.

Thanks

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
wrz 23
1369
0
gru 22
2280
2
sie 24
1627
1
sty 24
3605
1
lip 23
2714