我尝试使用 Whatsapp 业务云 API 和 Webhook 返回通过 Whatsapp 进行 API 调用生成的响应。我可以记录该消息,它是正确的,但是当我使用 webhook 返回它时,它不会在 Whatsapp 中发送。这是我的代码:
app.post("/webhook", (req, res) => {
// Parse the request body from the POST
let body = req.body;
// Check the Incoming webhook message
console.log(JSON.stringify(req.body, null, 2));
// info on WhatsApp text message payload: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages
if (req.body.object) {
if (
req.body.entry &&
req.body.entry[0].changes &&
req.body.entry[0].changes[0] &&
req.body.entry[0].changes[0].value.messages &&
req.body.entry[0].changes[0].value.messages[0]
) {
let phone_number_id =
req.body.entry[0].changes[0].value.metadata.phone_number_id;
let from = req.body.entry[0].changes[0].value.messages[0].from; // extract the phone number from the webhook payload
let msg_body = req.body.entry[0].changes[0].value.messages[0].text.body; // extract the message text from the webhook payload
axios({
method: "POST",
url: API_URL,
data: {
id: phone_number_id,
question: msg_body,
email: "N/A",
conversation: [],
save: true,
resolved: "N/A",
ads: 0,
},
headers: {
"Content-Type": "application/json",
"x-api-key": process.env.API_KEY,
},
})
.then(apiResponse => {
if (apiResponse.status !== 200) {
throw new Error(`Request failed with status ${apiResponse.status}`);
}
return apiResponse.data;
})
.then(responseData => {
console.log(responseData);
res.status(200).json({
message: responseData.answer,
});
})
.catch(error => {
console.error(error);
res.status(500).json({
message: "An error occurred while chatting.",
});
});
}
}
});
如您所见,我正在控制台记录
responseData
,它向我展示了良好的响应。但正如我所提到的,当我返回它时,它不会被发送到它收到初始发布请求的 Whatsapp 电话号码。
我试图通过使用 Meta For Developers 及其 Whatsapp 业务云 API 文档来实现整个过程,但无法弄清楚。
看起来您正在从 WhatsApp 接收 Webhook,调用您自己的 API,然后将答案作为对原始 Webhook 请求的 JSON 响应返回。但是,这不会通过 WhatsApp 将消息发送回用户; Webhook 请求只是确认您已成功收到消息。
要将消息发送回用户,您通常必须向 WhatsApp API 发出 POST 请求,并附带必要的参数,例如收件人电话号码和消息正文。您应该在处理 webhook POST 请求的代码中执行此操作。
<pre>
app.post("/webhook", (req, res) => {
// Parse the request body from the POST
let body = req.body;
// Log the incoming webhook message
console.log(JSON.stringify(req.body, null, 2));
// ... (existing code to extract phone_number_id, from, and msg_body)
// Make a request to your API
axios({
method: "POST",
url: API_URL,
// ... (existing code)
})
.then(apiResponse => {
// ... (existing code)
})
.then(responseData => {
// Log the answer received from your API
console.log(responseData);
// Prepare to send the message back to the user via WhatsApp API
axios({
method: "POST",
url: "YOUR_WHATSAPP_API_ENDPOINT_HERE", // Replace with the correct endpoint URL
headers: {
"Content-Type": "application/json",
Authorization: `Bearer YOUR_WHATSAPP_ACCESS_TOKEN_HERE`, // Replace with your token
},
data: {
to: from, // The phone number of the user who sent the message
type: "text",
text: {
body: responseData.answer, // The answer to send back
},
},
})
.then(() => {
// Confirm the message has been sent
res.status(200).end();
})
.catch((error) => {
console.error("Error sending WhatsApp message: ", error);
res.status(500).end();
});
})
.catch(error => {
// ... (existing code)
});
});
</pre>