WhatsApp outbound message via Python

Apologies for a basic question. As I have not got proper solution from Freshchat support team, I am posting it to Dev. community.

Simply I am trying to send a WhatsApp Proactive message (template approved) using python requests library. But I am getting following error

b'{"success":false,"errorCode":415,"errorMessage":"Please provide a valid value for Content-Type header","errorData":null,"errorName":null}'

When I am sending this message using “WhatsApp Proactive Messaging app” from FreshChat marketplace it’s getting delivered. Don’t know what I am missing in the body for the api call.

Below is the body for the api call.

{
  "from": {
    "phone_number": "+447946000000"},
  "provider": "whatsapp",
  "to": [{
    "phone_number": "+44740000000"
    }],
  "data": {
    "message_template": {
      "storage": "conversation",
      "template_name": "payment_confirmation",
      "namespace": "50d678e9_57a8_4275_bc87_xxxxxxxx",
      "language": {
        "policy": "deterministic",
        "code": "en"
        },
      "rich_template_data": {
        "header": {
          "type": "none"},
         "body": {
          "params": [
            {"data": "Variable 1"},
            {"data": "Variable 2"},
            {"data": "Variable 3"}
          ]}
      }
    }
  }
}

Here’s the python code which is calling api endpoint

import requests

headers = {
    'accept': 'application/json',
    'Authorization': 'Bearer ' + apitoken}


response = requests.post('https://api.freshchat.com/v2/outbound-messages/whatsapp', headers=headers, data=data)

Thanks and Regards

Hi Khaled

Welcome to Freshworks Developer Community!

This error denotes that you need to provide a valid Content-Type header. Can you try adding the header and check if it works

3 Likes

Thanks @banu.

but now I am getting a different error message.

Here’s the code which I am using to call API

import requests

headers = {

'Content-type':'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer ' + apitoken
}

response = requests.post('https://api.eu.freshchat.com/v2/outbound-messages/whatsapp', headers=headers, data=data)

Response content is:

b'{"success":false,"error_code":5,"error_message":"Invalid request format"}'

And payload which I am using

data = {
   "from":{
      "phone_number":"+4479xxxxx"
   },
   "provider":"whatsapp",
   "to":[
      {
         "phone_number":"+4474xxxxxx"
      }
   ],
   "data":{
      "message_template":{
         "storage":"conversation",
         "template_name":"payment_confirmation",
         "namespace":"xxxxxx_xxx_bxx_xxxxxx",
         "language":{
            "policy":"deterministic",
            "code":"en"
         },
         "template_data":[
            {
               "data":"First Name"
            },
            {
               "data":"Last Name"
            },
            {
               "data":"Course Name"
            }
         ]
      }
   }
}

Lastly apologies if these novice questions.

Thank you.

Resolved this issue.

After carefully looking at the code, found that I should dump by payload and pass to api as json.

Regards.

2 Likes

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.