我平均希望将SMS发送到100-1500个电话号码。我想在正文中包括与每个电话号码关联的人名。我将如何使用Twilio做到这一点?
client.notify.services(notifyServiceSid)
.notifications.create({
toBinding: JSON.stringify({
binding_type: 'sms', address: process.env.NUMBER_ONE,
binding_type: 'sms', address: process.env.NUMBER_TWO
}),
body: 'This should work!' //I want to dynamically change this per number.
})
.then(notification => console.log(notification.sid))
.catch(error => console.log(error));
有很多方法可以做到这一点。
一个例子:
文件: broadcast.js
require('dotenv').config();
const twilio = require('twilio')(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);
const template = "Hello {name}, test message";
(async () {
const records = [
{number: "+1234567890", name: "Someone Someonesky"},
{number: "+1234567891", name: "NotSomeone Someonesky"},
...
];
const chunkSize = 99; // let's not overflow concurrency limit: 100
for (let i = 0, j = records.length; i < j; i += chunkSize) {
await Promise.all(
records
.slice(i, i + chunkSize)
.map(
record => {
const {number, name} = record;
// copying template to variable
const body = template.toString();
// replacing placeholder {name} in template
// with name value from object
body = body.replace(/\{name\}/g, name);
return twillio.messages.create({
to: number,
from: process.env.TWILIO_NUMBER,
body
});
}
)
);
}
})();
文件: .env
TWILIO_ACCOUNT_SID=some-sid
TWILIO_AUTH_TOKEN=some-token
TWILIO_NUMBER=+3333333
安装依赖项:
npm i --save twilio
npm i --save dotenv
跑:
node broadcast.js
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句