Twilio消息API不允许在其消息正文中使用变量或串联的字符串

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

我正在尝试将包含生成的令牌的SMS发送到手机。如果将普通的硬编码字符串传递给消息正文,则会得到文本,但是如果传递变量或串联字符串,则会在Twilio仪表板上显示错误:30003 - Unreachable destination handset。但是,即使发送失败,我也收到了Twilio的成功回复。

// twilio.js

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;

const client = require('twilio')(accountSid, authToken);

exports.sendSMS = async ({ phone, message }) => {
  await client.messages.create({
    to: phone,
    from: process.env.TWILIO_PHONE_NUMBER,
    body: message // this doesn't work
    // body: 'Hello' works
  });
};

// somewhere in other-file.js

const token = generateToken() // abc123
const message = `Your token is ${token}.`;

await twilio.sendSMS({
  phone: user.phone,
  message
});

是否有解决方法?我在这里错了吗?

node.js sms twilio
1个回答
0
投票

而不是(允许嵌入表达式的字符串文字)

const message = `Your token is ${token}.`;

尝试使用“ JavaScript串联运算符”

const message = "Your token is " + token + ".";
© www.soinside.com 2019 - 2024. All rights reserved.