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..

2

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;

     }
}
9
  • 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
  • @BritishBoyinDC, I've almost created same kind of Rest Service and didn't even get in to this class after successful first send sms request, then I refer this and modified my Post method but still didn't get into the post method. Do we need to setup anything in Twilio site or in SF like public site to reach the Twilio response to the post method? I am developing and testing this in a scratch org. – Hariprasath Jan 8 at 15:17
  • @Hariprasath So yes - if it is an non authenticated REST endpoint, you need to setup a public site, and then expose this apex class on the site, which you can then use as the webhook endpoint in Twilio. But worth noting SF has changed the security behind sites recently, so you'd need this class to run without sharing to ensure it keeps working – BritishBoyinDC Jan 8 at 15:25
  • 1
    Finally could able to test the whole flow by removing the signature validation part in the Post method. But need to figure it out to validate the signature. Thanks for the whole support @BritishBoyinDC. – Hariprasath Jan 9 at 6:59
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.

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.