Jonathan Wold
Jonathan Wold

Reputation: 807

Creating an SMS group forwarder in Twilio with NodeJS

Here's what I'm trying to accomplish:

At a high-level, the idea seems straight forward enough. My programming / syntax skills are rusty, though, and I'd love some help.

I'm using Twilio Functions, and I've been able to send and receive messages successfully. Now I am stuck on how to carry this idea of iterating through a group.

Here's what I've written so far:

var groupmembers = {

    jonathan:{
        name: 'Jonathan',
        number: '+0000000000'
    },
    joshua:{
        name: 'Joshua',
        number: '+1110000000'
    }

}

exports.handler = function(context, event, callback) {

  // Set some values for later use
  this.fromNumber = event.From
  this.body = event.Body || ''

  let twiml = new Twilio.twiml.MessagingResponse();

  groupmembers.forEach(function(member) {

    // Skip sending if it's the same number
    if (member.number === this.fromNumber ) {
        return;
    }

    // Otherwise, let's send a mesage!
    twiml.message("Hello World").to( member.number );

    callback(null, twiml);
  });

};

The issues I believe I have:

Thank you for any and all feedback and for pointing me in the right direction!

Upvotes: 3

Views: 112

Answers (2)

Jonathan Wold
Jonathan Wold

Reputation: 807

With more searching and the point in the right direction from Slava I was able to figure it out! Here's the complete code:

/**
 * Represents a search trough an array.
 * @function search
 * @param {Array} array - The array you wanna search trough
 * @param {string} key - The key to search for
 * @param {string} [prop] - The property name to find it in
 * Props: https://stackoverflow.com/a/33097318/315818
 */

function search(array, key, prop){
    // Optional, but fallback to key['name'] if not selected
    prop = (typeof prop === 'undefined') ? 'name' : prop;    

    for (var i=0; i < array.length; i++) {
        if (array[i][prop] === key) {
            return array[i];
        }
    }
}

var groupmembers = [
    {
        name: 'Jonathan',
        number: '+000000000'
    },
    {
        name: 'Joshua',
        number: '+111111111'
    }
];

exports.handler = function(context, event, callback) {

  let twiml = new Twilio.twiml.MessagingResponse();

  // Search for the group member that matches the sender number
  let sender = search(groupmembers, event.From, 'number');

  // Now, loop through each of the group members
  groupmembers.forEach(function(member) {

    // Skip sending if it's the same number
    if (member.number === event.From ) {
        return;
    }

    // Now, forward on the message to the group member, using the sender's name 
    twiml.message(`${sender.name}: ${event.Body}`, {
        to: member.number
    });

  });
  // Loop ended 

  callback(null, twiml);

};

Upvotes: 3

Slava Knyazev
Slava Knyazev

Reputation: 6081

The mistake you have is pretty simple. groupmembers is an object, you want an array.

You may want something akin to this instead:

var groupmembers = [{
        name: 'Jonathan',
        number: '+0000000000'
    },
    {
        name: 'Joshua',
        number: '+1110000000'
    }]

Apart from that, it looks to me as okay.

Upvotes: 3

Related Questions