我正在尝试使用whatsapp can api向用户发送确认消息。
下面是我的nodejs代码,
app.post("/send-confirmation", async (req, res) => {
const { phoneNo, bookingId, name, tripType, origin, destination, bookedDate, bookedTime, carType, distance, estimate } = req.body;
console.log(phoneNo, bookingId, name, tripType, origin, destination, bookedDate, bookedTime, carType, distance, estimate);
// Prepare the message template with the booking details
const postData = JSON.stringify({
messaging_product: 'whatsapp',
recipient_type: 'individual',
to: [`91${phoneNo}`], // Phone number of the customer
type: 'template',
template: {
name: 'booking_confirmation',
language: {
code: 'en',
},
components: [
{
type: 'body',
parameters: [
{
type: 'text',
text: `Booking ID: ${bookingId}`,
},
{
type: 'text',
text: `Passenger Name: ${name}`,
},
{
type: 'text',
text: `Contact Number: ${phoneNo}`,
},
{
type: 'text',
text: `Journey Type: ${tripType}`,
},
{
type: 'text',
text: `Pickup Location: ${origin}`,
},
{
type: 'text',
text: `Drop-off Location: ${destination}`,
},
{
type: 'text',
text: `Pickup Date and Time: ${bookedDate} ${bookedTime}`,
},
{
type: 'text',
text: `Vehicle Type: ${carType}`,
},
{
type: 'text',
text: `Total Km: ${distance}`,
},
{
type: 'text',
text: `Total Fare: ${estimate} Rs`,
},
],
}
],
},
});
try {
const response = await axios({
method: "POST",
url:
"https://graph.facebook.com/v17.0/" +
'117505268034839' +
"/messages?access_token=" +
token,
data: postData,
headers: { "Content-Type": "application/json" },
});
console.log(response.data); // or do something with the response data
res.sendStatus(200);
} catch (error) {
console.error(error);
res.sendStatus(500);
}
});
我的
booking_confirmation
已获得FB批准。以下是FB模板代码
*Booking ID*: {{1}}
*Passenger Name*: {{2}}
*Contact Number*: {{3}}
*Journey Type*: {{4}}
*Pickup Location*: {{5}}
*Drop-off Location*: {{6}}
*Pickup Date and Time*: {{7}}
*Vehicle Type*:{{8}}
*Total Km*: {{9}}
*Total Fare*: {{10}} Rs
调用 API 后,我收到以下错误
AxiosError:请求失败,状态码 400 代码:'ERR_BAD_REQUEST'
消息未送达,我在这里做错了什么?请帮忙
我也遇到同样的问题,请问你解决了吗?