Question:
I have WhatsApp business API set up and running. I’m trying to receive messages to a dataset. I have the HTTP function set up and working, it receives data but when logging the (request) it simply says “[object WixHttpFunctionRequestBody]” and doesn’t seem to offer any useable data.
I have set up using glitch.com and that shows the object data as you’d expect. Likewise using webhooks with Make.com (Integromat).
Can anyone help??
//http-function.js
export function post_whatsApp(request) {
console.log(request + " post_whatsApp is accessed");
/*
{
"object": "whatsapp_business_account",
"entry": [
{
"id": "0",
"changes": [
{
"field": "messages",
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "16505551111",
"phone_number_id": "123456123"
},
"contacts": [
{
"profile": {
"name": "test user name"
},
"wa_id": "16315551181"
}
],
"messages": [
{
"from": "16315551181",
"id": "ABGGFlA5Fpa",
"timestamp": "1504902988",
"type": "text",
"text": {
"body": "this is a text message"
}
}
]
}
}
]
}
]
}
*/
let options = {
"headers": {
"Content-Type": "application/json"
}
};
return request.body.json()
.then( (body) => {
return createLine(body, options);
})
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
});
}
function createLine(body, options) {
let dataInsert = {
"number" : body.number,
"title": body.title,
"message": body.message,
"media" : body.media,
};
return wixData.insert('WhatsApp', dataInsert)
.then( (result) => {
let item = result;
options.body = {
"status": 201,
"response": item
};
return created(options);
});
}