2

There is a requirement to integrate SF with Twilio for sending messages on mobile,which i was able to achieve. I would like to track the status of the sent message using the parameter: '&StatusCallback= EncodingUtil.urlEncode(' https://cs5.salesforce.com/services/apexrest/Postback');

What should the method of the REST webservices have in its POST method? If Twilio is posting back the status, then what should the POST method of the webservices do? Not able to get a idea on this, please help..

1

Here is an example of a class doing that update using the callback URL: https://github.com/britishboyindc/TextToCase_Twilio_Salesforce/blob/master/src/classes/smsResponse.cls

@RestResource(urlMapping='/smsResponse')
global class smsResponse {

    @HttpPost
    global static void incomingSMSResponse() {

        String expectedSignature = RestContext.request.headers.get('X-Twilio-Signature');
        String url = 'https://' + RestContext.request.headers.get('Host') + '/services/apexrest' + RestContext.request.requestURI;
        Map <String, String> params = RestContext.request.params;
        system.debug('Return Response' + params);

       // Validate signature

        if (!TwilioAPI.getDefaultClient().validateRequest(expectedSignature, url, params)) {
            RestContext.response.statusCode = 403;
            RestContext.response.responseBody = Blob.valueOf('Failure! Rcvd '+expectedSignature+'\nURL '+url+'\nHeaders'+RestContext.request.headers);
            return;
        }

        SMS_Message_Tracking__c smstrack = new SMS_Message_Tracking__c();
        smstrack.SID__c = params.get('SmsSid');
        smstrack.Status__c = params.get('SmsStatus');
        upsert smstrack SID__c;

     }
}
| improve this answer | |
  • I am extremely thankful to you BritishBoyinDC!It worked like a charm! – SF Learner Oct 19 '15 at 22:30
  • that works perfect, but for some reason in the debug of twilio console i get a warning: Error - 11200 it seems for me twilio expects soemthing after they do the API in the endpoint? – manza Jul 17 '19 at 2:27
1

Please find this SO link with a detailed explanation. Hope this helps.

If you specify a StatusCallback URL when you make the request to send an SMS, Twilio will make a request to the callback URL you provided after the message has finished processing, with the parameters either SmsStatus=sent or SmsStatus=failed. You can use this information to do more processing on the SMS message. There's more information here: http://www.twilio.com/docs/api/rest/sending-sms#post-parameters-optional

Alternately, if you hang on to the SMS Message Sid, you should be able to query the API for the message and get the status in the response. So if the sid is SM123, making a GET request to https://api.twilio.com/2010-04-01/Accounts/AC123/SMS/Messages/SM123.json should return a object with the status of the SMS Message.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.