MasterScrat
MasterScrat

Reputation: 7366

Twilio: SMS not sent

At the end of a call, I want to send an SMS with a link to the recording of the conversation to the caller and to a control number.

I have two TwiML bins. The first one handles dialing and recording, then directs the call to the second bin. This works well.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial record="record-from-ringing-dual" timeLimit="600" action="https://handler.twilio.com/twiml/XXXXXXXXXXXXXXX">
    <Number>{{Digits}}</Number>
  </Dial>
</Response>

Here's the second bin:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Sms from="+XXXXXXXXX" to="{{From}}">Here's the recording of your call: {{RecordingUrl}}</Sms>
    <Sms from="+XXXXXXXXX" to="+YYYYYYYYYY">Call recorded from {{From}} to {{To}}.</Sms>
</Response>

From the log, I see the TwiML bin was successfully called. Status code is 200. The {{From}} number was correctly replaced.

However, no SMS is sent. No SMS appears in the SMS log. No SMS is received if I do a call myself. Despite that, no error message appear in the call log or in the debugger.

Upvotes: 0

Views: 181

Answers (1)

philnash
philnash

Reputation: 73029

Twilio developer evangelist here.

As we've discussed in your other question, <Sms> uses the deprecated SMS/Messages resource under the hood. Therefore it doesn't come with good logging.

I recommend using the Messages resource of the REST API to send messages instead. You can't do that with a TwiML Bin, but you can with Twilio Functions.

In the config section for Functions make sure the "Enable ACCOUNT_SID and AUTH_TOKEN" check box is ticked. Create a new Function in your Twilio console and enter the following code (replace the placeholders for real numbers):

exports.handler = function(context, event, callback) {
  const client = context.getTwilioClient();

  const message1 = client.messages.create({
    from: YOUR_TWILIO_NUMBER,
    to: event.From,
    body: `Here's the recording of your call: ${event.RecordingUrl}`
  });

  const message2 = client.messages.create({
    from: YOUR_TWILIO_NUMBER,
    to: THAT_OTHER_NUMBER,
    body: `Call recorded from ${event.From} to ${event.To}.`
  })

  Promise.all([message1, message2]).then(() => { 
    let twiml = new Twilio.twiml.VoiceResponse();
    callback(null, twiml);  
  }, err => {
    callback(err);
  })
};

Give the function a path and then copy the whole URL and use it as the action attribute in the <Dial> in your first TwiML Bin.

Let me know if that helps at all.

Upvotes: 1

Related Questions