How to get SMS request via twilio

2019-09-09 14:32发布

问题:

I have a class that can access the site url in the salesforce. The site url "https://somesalesforce.com/smsToApex" and same as the twilio account sms URL but this class can't called. I have reference this document.The twilio SMS url correct are not how to check?The Twilio account SMS url and salesforce site url are same as of now.Whenever SMS is came via twilio then automatically Case is createin the sandbox.But here not happened.I doing correct producer are not?.Can someone please help me.How to resolve this problem.I have checked the twilio sms url like this "https://somesalesforce.com/service/apexrest/smsToApex".which url I will give both Salesforce site url, Twilio SMS url.

@RestResource(urlMapping='/smsToApex')
global class smsToApex 
{   
    Static TwilioAccount account = TwilioAPI.getDefaultAccount();         
    @HttpPost
    global static void incomingSMS() 
    {        
        // This will error out with System.LimitException if we would exceed
        // our daily email limit
        Messaging.reserveSingleEmailCapacity(1);

        String expectedSignature = RestContext.request.headers.get('X-Twilio-Signature');
        system.debug('ES' + expectedSignature);
        String url = 'https://' + RestContext.request.headers.get('Host') + '/services/apexrest' + RestContext.request.requestURI;
        Map <String, String> params = RestContext.request.params;
       system.debug('smsToApex========>'+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;
        }       
        RestContext.response.responseBody = Blob.valueOf('ok');        
        String caseFrom = params.get('From');
         String caseTo = params.get('To');
        String   caseBody = params.get('Body');          
          System.debug('Step 4 smsToApex caseFrom==>'+caseFrom);
          System.debug('Step 5 smsToApex caseTo===>'+caseTo);
          System.debug('Step 6 smsToApex caseBody===>'+caseBody);          
           Case ca = new Case();
            ca.Subject = 'Test smsToApex caseFrom'+caseFrom;
            ca.Description = 'Test smsToApex caseBody'+caseBody+','+caseTo;
            ca.Origin = 'Phone';
            INSERT ca;                  
     }                       
}

回答1:

A few things to check...

  • Can you post to that URL via something like Postman? You should at least get an error if you pass in some JSON...
  • Is the message making it to SF and failing there, or never making it, which would indicate a problem with the URL/REST Class in SFDC
  • Check the Twilio SMS logs - you should see the result in the SMS logs either success or failure with the error code.
  • Did you set the Twilio creds in the Cusom Setting in SF? It uses them for the signature check.
  • What do the SFDC debug logs show you? You can view the debug logs for a Sites user if you set it up in Monitor - Debug and search for the Site User.