尝试使用 Whatsapp Cloud API 发送消息时收到 ERR_BAD_REQUEST

问题描述 投票:0回答:1

我正在尝试使用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'

消息未送达,我在这里做错了什么?请帮忙

node.js facebook-graph-api whatsapp-cloud-api
1个回答
-2
投票

我也遇到同样的问题,请问你解决了吗?

© www.soinside.com 2019 - 2024. All rights reserved.