简体   繁体   English

如何使用nodejs将SMS消息分别(一次一次)发送给多个用户?

[英]How to send the SMS message to multiple users separately (Not at a time) using nodejs?

I want to send the SMS to multiple users using node.js code.what is the procedure to be followed? 我想使用node.js代码将SMS发送给多个用户。要遵循的程序是什么? I had little bit experience to send the sms to single user. 我几乎没有将短信发送给单个用户的经验。 But I don't know how to send the sms to multiple users . 但是我不知道如何发送短信到多个用户 I have enclosed the code to send SMS to the single user. 我已经附上了将短信发送给单个用户的代码。 But before 6 Months these code works fine. 但是在6个月之前,这些代码可以正常工作。 But now it doesn't work . 但是现在不行了 Can anyone provide the solution for this two problem? 谁能为这两个问题提供解决方案?

 var accountSid = 'cnsjs'; // Your Account SID from www.twilio.com/console var authToken = 'chdcbhh'; // Your Auth Token from www.twilio.com/console var twilio = require('twilio'); var client = new twilio(accountSid, authToken); client.messages.create({ body: 'Hello hai I am vignesh Ravi . I sent the message from twilio account', to: '+121331161616', // Text this number from: '+18597401144' // From a valid Twilio number }).then(function(message) { console.log("Messages sent to 1213311161616"); }); 

if you want to send multiple messages at the same time you need to use Promise.all . 如果要同时发送多个消息,则需要使用Promise.all I have updated your code with something that i think will work for you i created it using bluebird.js because i am familiar with it and it has kickass tools. 我已经更新了您认为适合您的代码,我使用bluebird.js创建了它,因为我对此很熟悉,并且它具有kickass工具。

tri this: 三:

var accountSid = 'cnsjs'; // Your Account SID from www.twilio.com/console
var authToken = 'chdcbhh';   // Your Auth Token from www.twilio.com/console
var Promise = require('bluebird');

var twilio = require('twilio');
var client = new twilio(accountSid, authToken);

var promises = [];

// add all the different send promises to this array like following

promises.push(
    client.messages.create({
        body: 'Hello hai I am vignesh Ravi . I sent the message from twilio account',
        to:   '+121331161616',  // Text this number
        from: '+18597401144' // From a valid Twilio number
    })
)

var result = {
    sent: [],
    failed: []
};

var finalPromise = Promise.all(promises.map(function (p) {
    return Promise.resolve(p).reflect(); // resolve will convert it to bluebird promise and reflect will make it always successful
})).each(function (inspection) {
    if (inspection.isFulfilled()) { //this means the send was successful
        result.sent.push(inspection.value());        
    } else { //this means there was an error 
        result.failed.push(inspection.reason());
    }
}).then(function () {
    return result;
})

finalPromise.then(function (res) {
    console.log(JSON.stringify(res))
})

you might have to fix the code a bit before it starts to work as you intend it 您可能需要先修正一下代码,然后才能按预期工作

Edit 编辑

i made small changes to code where i am calling the collective promise at the end. 我在代码末尾进行了集体更改的地方做了一些小的更改。 give it a try and let me know if it works 试试看,让我知道它是否有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM