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

@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.
来源:https://stackoverflow.com/questions/24365142/how-to-get-sms-request-via-twilio