camel
camel

Reputation: 1473

Twilio Node JS - filter sms per phone number

I would like to filter sms per phone number and date the SMS was sent using REST API, however the output of the following code is not available outside of client.messages.each() block. Please advise how I can use the latest sms code sent to the filtered number:

const filterOpts = {
  to: '+13075550185',
  dateSent: moment().utc().format('YYYY-MM-DD')
};
let pattern = /([0-9]{1,})$/;
let codeCollection = [];
client.messages.each(filterOpts, (record) => {
  codeCollection.push(record.body.match(pattern)[0]);
  console.log(record.body.match(pattern)[0], record.dateSent);
 });
 console.log(codeCollection,'I get an empty array here');//how to get 
 the latest sms and use it 
 doSomethingWithSMS(codeCollection[0]);

Upvotes: 0

Views: 365

Answers (3)

philnash
philnash

Reputation: 73057

Twilio developer evangelist here.

The each function doesn't actually return a Promise. You can run a callback function after each has completed streaming results by passing it into the options as done like this:

const codeCollection = [];
const pattern = /([0-9]{1,})$/;

const filterOpts = {
  to: '+13075550185',
  dateSent: moment().utc().format('YYYY-MM-DD'),
  done: (err) => {
    if (err) { console.error(err); return; }
    console.log(codeCollection);
    doSomethingWithSMS(codeCollection[0]);
  }
};

client.messages.each(filterOpts, (record) => {
  codeCollection.push(record.body.match(pattern)[0]);
  console.log(record.body.match(pattern)[0], record.dateSent);
 });

Let me know if that helps at all.

Upvotes: 1

Jake T.
Jake T.

Reputation: 4378

messages.each() is running asynchronously, so your main thread moves on to the next call while the client.messages() stuff runs on a background thread. So, nothing has been pushed to codeCollection by the time you've tried to access it. You need to somehow wait for the each() to finish before moving on. Twilio client uses backbone style promises, so you can just add another .then() link to the chain, like below. You could also use a library like async which lets you use await to write asynchronous code in a more linear looking fashion.

const filterOpts = {
  to: '+13075550185',
  dateSent: moment().utc().format('YYYY-MM-DD')
};
let pattern = /([0-9]{1,})$/;
let codeCollection = [];
client.messages.each(filterOpts, (record) => {
    codeCollection.push(record.body.match(pattern)[0]);
    console.log(record.body.match(pattern)[0], record.dateSent);
}).then(
    function() {
        console.log(codeCollection,'I get an empty array here');
        if( codeCollection.count > 0 ) doSomethingWithSMS(codeCollection[0]);
    }
);

Upvotes: 0

db2791
db2791

Reputation: 1110

Do you have access to the length of the array of messages? If so, you can do something like this

const filterOpts = {
  to: '+13075550185',
  dateSent: moment().utc().format('YYYY-MM-DD')
};
let pattern = /([0-9]{1,})$/;
let codeCollection = [];
var i = 0
client.messages.each(filterOpts, (record) => {
    if (i < messages.length){
        codeCollection.push(record.body.match(pattern)[0]);
        console.log(record.body.match(pattern)[0], record.dateSent);
        i++;
    else {
        nextFunction(codeCollection);
    }
 });

function nextFunction(codeCollection){
     console.log(codeCollection,'I get an empty array here');
     doSomethingWithSMS(codeCollection[0]);
}

Upvotes: 0

Related Questions