使用twilio Passthrough API将消息发送到多个号码

时间:2019-12-27 20:02:27

标签: javascript node.js sms twilio

我正在使用twilio在单个API调用中将消息发送到多个电话号码。

client.notify.services(notifyServiceSid)
  .notifications.create({
    toBinding: JSON.stringify([
      binding_type: 'sms', address: '** First phone number here **',
      binding_type: 'sms', address: '** Second phone number here **'
    ]),
    body: 'You just sent your first message with the Passthrough API!'
  })
  .then(notification => console.log(notification.sid))
  .catch(error => console.log(error));

上面的示例代码片段向电话号码数组发送了相同的消息(“您刚刚通过Passthrough API发送了第一条消息!”),尽管我想做的是向每个电话号码发送不同的消息。上面的代码片段似乎不可能实现。我在给定的博客中也找不到任何内容:Passthrough API。因此,无论如何都会向每个电话号码发送不同的消息。我也有一个向每个收件人发送不同消息的实现:

const prepareSendingMessages = async (body) => {
    //parse message input and send message in loop
    try {
        console.log(body.data)
        for (const sms of body.data.messages) {
            await sendMessage(sms.to, sms.message)
        }
    } catch (error) {
        console.log(error);
    }

};

const sendMessage = async (to, message) => {
    let messageResult = await client.messages
        .create({
            body: message,
            from: process.env.TWILIO_NUMBER,
            to
        });

    console.log(messageResult);

    return true;
};

但是我正在研究是否可以通过Twilio's Passthrough API

1 个答案:

答案 0 :(得分:1)

您为此使用了消息资源。

如何发送短信 https://www.twilio.com/docs/sms/send-messages