Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
1261 Visualizzazioni

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


Avatar
Abbandona
Risposta migliore

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

Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
1
set 23
1379
0
dic 22
2287
2
ago 24
1627
1
gen 24
3613
1
lug 23
2728