我正在尝试使用 twilio+ Nodejs 使用批准的模板向我的手机发送 WhatsApp 消息。我对过去几天的这个错误感到震惊。
我的代码看起来像这样。
client.messages
.create({ messagingServiceSid: "MG0XXXXXXXXXXXXX",
contentSid:"HTXXXXXXXXXXXXXXXXX",
from: "whatsapp:+91999999XXXX",
contentVariables: { 1: "Name", }, to: "whatsapp:+91789XXXXXXX", })
.then((message) => {
console.log(message);
return success(res, "message sent ", []); })
.catch((err) => {
console.log(err);
return failure(res, "Some error ocurred", err.message);
});
请帮我解决这个问题。
以下是错误信息
RestException [Error]: Invalid Parameter at success (/home/gt-10/GT-Work/gt-web-services/gt-api/node_modules/twilio/lib/base/Version.js:79:23) at processTicksAndRejections (node:internal/process/task_queues:96:5)
{
status: 400, code: 20422, moreInfo: 'https://www.twilio.com/docs/errors/20422', details: undefined
}
如错误20422所述,您使用无效参数调用了API。我认为这是因为您在此通话中使用了消息服务和 WhatsApp 发件人。尝试将消息服务 sid 移至
from
参数,如此处:所述
// Download the helper library from https://www.twilio.com/docs/node/install
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
client.messages
.create({
contentSid: 'HXXXXXXXXX',
from: 'MGXXXXXXXX',
contentVariables: JSON.stringify({
1: 'Name'
}),
to: 'whatsapp:+18551234567'
})
.then(message => console.log(message.sid));