0
votes

I am using Twilio Studio for to collect messages for our small church. When someone records a voicemail, I am using a function to send the recording URL to the group members. Here is what that function looks like currently:

var groupmembers = [
{
    name: 'Person1',
    number: '+11111111111'
},
{
    name: 'Person2',
    number: '+11111111111'
}
];

exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.MessagingResponse();
  groupmembers.forEach(function(member) {

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

  callback(null, twiml);
};

This gives me a '12200 Schema validation warning' with the detail: Invalid content was found starting with element 'Message'. One of '{Play

I'm fairly sure the issue is because I am trying to send an SMS during a call but I am not sure how to update my TWIML or Studio flow to accommodate for this.

Any help is appreciated!

1

1 Answers

0
votes

you need the REST API rather then TwiML in this case. You could use code similar to what is shown below to do this.

Make sure to check the box below, under Function Config for your Function,

enter image description here

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

  const twilioClient = context.getTwilioClient();

  let groupMembers = [
    {
        name: 'Person1',
        number: '+14701111111'
    },
    {
        name: 'Person2',
        number: '+18082222222'
    },
    {
        name: 'Person3',
        number: '+18021111111'
    }
  ];    

  function sendSMS(member) {
    return twilioClient.messages.create({
      from: '+13054444444',
      to: member.number,
      body: 'Hello World!'
    });
  }

  let promises = [];

  groupMembers.forEach((member) => {
  console.log(member);
  promises.push(sendSMS(member));
  });

  Promise.all(promises)
    .then((values) => {
      console.log(values);
      callback(null,"Success");
    })
    .catch(error => {
      console.log(error);
      callback("Failure");
    });
};