我简单的“发送消息” Twilio 的 javascript 程序循环两次,但它只向同一个号码发送一条消息,缺少什么?
我正在尝试创建一个简单的JavaScript程序,该程序将向不同数字的数组发送一条消息。我找到了一个Twilio页面,几乎可以让我到达那里,但这并没有告诉我如何循环。诚然,我是一个编程努比,发现了循环代码线。该程序确实循环,但是在数组中调用了相同的数字,而不是调用数组中的两个号码。这是我正在使用的代码:
// 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 twilio = require('twilio')(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);
const numbers = [+180xxxxxxxx, +180xxxxxxxx,]
const body = 'Hello World'
let nLen = numbers.length
for (let i = 0; i<nLen; i++)
{
twilio.messages
.create({
to: numbers,
from: +180xxxxxxxx,
body: body
})
.then(message => {
console.log(message.sid);
})
.catch(err => console.error(err));
}
是否需要添加另一个函数以使其第二次将消息发送到第二个数字?
I'm trying to create a simple Javascript program that will send a single message to an array of different numbers. I found a twilio page that got me almost there but it doesn't tell me how to loop. I'm admittedly a programming nubie and found a line of loop code. The program did loop, but it called the same number in the array twice instead of calling the two numbers in the array. Here is the code I was using:
// 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 twilio = require('twilio')(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);
const numbers = [+180xxxxxxxx, +180xxxxxxxx,]
const body = 'Hello World'
let nLen = numbers.length
for (let i = 0; i<nLen; i++)
{
twilio.messages
.create({
to: numbers,
from: +180xxxxxxxx,
body: body
})
.then(message => {
console.log(message.sid);
})
.catch(err => console.error(err));
}
Is there another function I need to add in to make it send the message to the second number the second time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从未使用过 twilio,但看起来您在 .create 函数中给出了数字数组而不是一个数字,
请尝试将“numbers”切换为“numbers[i]”:
I've never used twilio but it looks like you give the array of numbers in the .create function instead of one number,
try switching "numbers" to "numbers[i]":