猿问

Twilio - 如何通过每个电话号码发送不同正文的批量短信?

我平均有 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));


眼眸繁星
浏览 205回答 1
1回答

富国沪深

有很多方法可以做到。一个例子:文件: broadcast.jsrequire('dotenv').config();const twilio = require('twilio')(&nbsp; process.env.TWILIO_ACCOUNT_SID,&nbsp; process.env.TWILIO_AUTH_TOKEN);const template = "Hello {name}, test message";(async () {&nbsp;&nbsp; const records = [&nbsp; &nbsp; {number: "+1234567890", name: "Someone Someonesky"},&nbsp; &nbsp; {number: "+1234567891", name: "NotSomeone Someonesky"},&nbsp; &nbsp; ...&nbsp; ];&nbsp; const chunkSize = 99; // let's not overflow concurrency limit: 100&nbsp; for (let i = 0, j = records.length; i < j; i += chunkSize) {&nbsp; &nbsp; await Promise.all(&nbsp; &nbsp; &nbsp; records&nbsp; &nbsp; &nbsp; &nbsp; .slice(i, i + chunkSize)&nbsp; &nbsp; &nbsp; &nbsp; .map(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; record => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const {number, name} = record;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // copying template to variable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const body = template.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // replacing placeholder {name} in template&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // with name value from object&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body = body.replace(/\{name\}/g, name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return twillio.messages.create({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to: number,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from: process.env.TWILIO_NUMBER,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; );&nbsp; }})();文件: .envTWILIO_ACCOUNT_SID=some-sidTWILIO_AUTH_TOKEN=some-token&nbsp;TWILIO_NUMBER=+3333333安装依赖:npm i --save twilionpm i --save dotenv跑:node broadcast.js
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答