英文:
Meta WhatsApp Client Localization Request Body not working in my Webhook code as prescribed in the Doc
问题
我有一个Node.js的机器人,有一个方法可以帮助我向客户发送一个请求,要求他们发送位置信息,使用特殊的Meta Whatsapp格式,如交互式消息文档中的位置请求消息部分所述:
文档中的说明如下:
{
"type": "location_request_message",
"body": {
"type": "text",
"text": "<TEXT>"
},
"action": {
"name": "send_location"
}
}
根据文档中的要求,将上述代码包装到以下代码的交互属性中:
{
"recipient_type": "individual",
"to" : "whatsapp-id", // 您的收件人的WhatsApp ID
"type": "interactive",
"interactive":{
// 您的交互对象
}
}
我的代码如下(我将上述两个代码段合并成一个,按照文档的要求):
const body = {
recipient_type: "individual",
to: "mobile_number", // 您的收件人的WhatsApp ID
type: "interactive",
interactive:{
// 您的交互对象
type: "location_request_message",
body: {
type: "text",
text: "Finally"
// text: "Good day"
},
action: {
name: "send_location"
}
}
}
然后,我使用axios发送这个请求:
return new Promise((next) => {
var headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
};
AxiosService.post(
`https://graph.facebook.com/${VERSION}/${JC_PHONE_ID}/messages`,
body,
headers
)
.then((response) => {
if (response.status == 200) {
console.log(response.status)
next({ success: true, status: 200 });
} else {
console.log(response.status)
console.log("Heehehehe....")
next({ success: false, status: 400 });
}
})
.catch((err) => {
next({ success: false, status: 400 });
});
});
最后,使用我的Webhook链接发送请求:
最终,我收到了400错误。我非常确定这是因为Facebook不接受我设置的请求体的方式。
有人可以帮助我正确设置上述JSON请求体吗?
英文:
I have a nodejs bot and a method which helps me send client a request for their location using a special Meta Whatsapp body as prescribed in the section Location Request Messages of Sending Interactive Messages Doc:
Doc prescription below:
{
"type": "location_request_message",
"body": {
"type": "text",
"text": "<TEXT>"
},
"action": {
"name": "send_location"
}
}
Wrapping component of the code above according to the doc (doc says you have to add the above code inside of the interactive property of the blow code):
{
"recipient_type": "individual",
"to" : "whatsapp-id", // WhatsApp ID of your recipient
"type": "interactive",
"interactive":{
// Your interactive object
}
}
My own code is below (I combined the 2 above codes in one as prescribed by the doc):
const body = {
recipient_type: "individual",
to: "mobile_number", // WhatsApp ID of your recipient
type: "interactive",
interactive:{
// Your interactive object
type: "location_request_message",
body: {
type: "text",
text: "Finally"
// text: "Good day"
},
action: {
name: "send_location"
}
}
}
And i send it with axios with my promise:
return new Promise((next) => {
var headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
};
AxiosService.post(
`https://graph.facebook.com/${VERSION}/${JC_PHONE_ID}/messages`,
body,
headers
)
.then((response) => {
// console.log("THIS RESPONSE IS REALLY BIG: ", response)
if (response.status == 200) {
console.log(response.status)
next({ success: true, status: 200 });
} else {
console.log(response.status)
console.log("Heehehehe....")
next({ success: false, status: 400 });
}
})
.catch((err) => {
next({ success: false, status: 400 });
});
});
Finally sending a request for it using my webhook link:
> https://0852-154-72-160-109.ngrok-free.app/webhook/
At the end i get a 400 error:
I am pretty sure it is because facebook does not accept the way i set the body.
Can anyone help me on how to set the above json body properly?
答案1
得分: 1
如果您已经知道有两种设置方式,
- 云 API
云 API 允许您通过 Meta 拥有的基于云的服务器发送和接收与客户的消息。由于我们托管了 API,您可以避免托管自己的服务器的成本,并轻松扩展您的业务消息。
- 场地 API
场地 API 允许您通过您自己的服务器发送和接收与客户的消息。
您可以在这里了解更多区别。
从我看到的,您的请求发送到云 API 的主机:
https://graph.facebook.com/${VERSION}/${JC_PHONE_ID}/messages
而您正在查看场地 API 文档:
发送交互式消息文档 的 位置请求消息
您需要参考云 API 文档以获取交互类型消息的信息,而在交互消息中没有提到位置类型,它仅在场地 API 设置中支持,
https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-messages#interactive-messages
https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#interactive-object
英文:
If you are aware there are 2 types of setups,
-
cloud API
> The Cloud API allows you to send and receive messages to and from customers using cloud-based servers owned by Meta. Since we host the API, you avoid the cost of hosting your own servers and can easily scale your business messaging. -
on-premises API
> The On-Premises API allow you to send and receive messages to and from customers using your own servers.
You can read more differences here.
As I can see your request goes to the cloud API's host:
> https://graph.facebook.com/${VERSION}/${JC_PHONE_ID}/messages
And you are checking in on-premises API documentation:
> Location Request Messages of Sending Interactive Messages Doc
You need to refer to the cloud API documentation for the interactive type message, and there is no mention of location type in interactive, it is just supported in on-premises API setup,
https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-messages#interactive-messages
https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#interactive-object
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论