Guest User

Untitled

a guest
Jan 25th, 2016
110
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @RestResource(urlMapping='/textlead/*')
  2. global with sharing class TextLeadRestfulReceiver {
  3.  
  4. @HttpGet
  5. global static String init() {
  6. String payload = ' -d "grant_type=password"' +
  7. ' -d "client_id=ID"' +
  8. ' -d "client_secret=secret"' +
  9. ' -d "username=here@there.com.staging"' +
  10. ' -d "password=whateverTOKEN"';
  11.  
  12. HttpRequest req = new HttpRequest();
  13. req.setMethod('GET');
  14. req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
  15. req.setHeader('Content-Type','application/x-www-form-urlencoded');
  16. req.setHeader('Content-Length', String.valueOf(payload.length()));
  17. req.setHeader('X-PrettyPrint', '1');
  18. req.setBody(payload);
  19.  
  20. Http binding = new Http();
  21. HttpResponse res = binding.send(req);
  22.  
  23. System.debug(String.valueOf(res));
  24.  
  25. return String.valueOf(res);
  26. }
  27.  
  28. global static String saveText() {
  29. String ACCOUNT_SID = 'what';
  30. String AUTH_TOKEN = 'ever';
  31.  
  32. TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
  33.  
  34. RestRequest request = RestContext.request;
  35. request.addHeader('Authorization', 'Bearer');
  36. String body = 'Nada.';
  37. String leadNumber = request.requestURI.substring(request.requestURI.lastIndexOf('/') + 1);
  38. List<Lead> result = [SELECT Id, FirstName, MobilePhone
  39. FROM Lead WHERE MobilePhone=:leadNumber];
  40.  
  41. for (TwilioMessage message: client.getAccount().getMessages().getPageData()) {
  42. body = message.getBody();
  43. }
  44.  
  45. for (Lead l: result) {
  46. Task task = new Task(WhoId = l.Id, OwnerId = l.OwnerId, Description = body, ActivityDate = Date.today(),
  47. Subject = 'Text received from ' + l.FirstName + ' on ' + leadNumber);
  48. insert task;
  49. }
  50. return body;
  51. }
  52. }
  53.  
  54. String ACCOUNT_SID = 'AXXXXXXXXXXXXXXXXX';
  55. String AUTH_TOKEN = 'YYYYYYYYYYYYYYYYYY';
  56. TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
  57.  
  58. for (TwilioSms message : client.getAccount().getSmsMessages().getPageData()) {
  59. System.debug(message.getBody());
  60. }
  61.  
  62. @RestResource(urlMapping = '/textlead/')
  63. global with sharing class TextLeadReceiver {
  64.  
  65. @HttpGet
  66. global static String TextLeadReceiver() {
  67. RestRequest req = RestContext.request;
  68. RestResponse res = RestContext.response;
  69. String ACCOUNT_SID = 'what';
  70. String AUTH_TOKEN = 'ever';
  71. TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
  72.  
  73. String subject = 'Text received from {0} on {1}';
  74. String phone = 'No phone';
  75. String body = 'Nada.';
  76.  
  77. req.addHeader('Accept', 'application/json');
  78.  
  79. try {
  80. for (TwilioMessage message: client.getAccount().getMessages().getPageData()) {
  81. body = message.getBody();
  82. phone = message.getFrom();
  83. System.debug('SMS body: ' + body);
  84. System.debug('SMS phone: ' + phone);
  85. }
  86.  
  87. List<Lead> leads = [SELECT Id, FirstName, MobilePhone, Phone, Phone_2__c
  88. FROM Lead WHERE MobilePhone = :phone
  89. OR Phone = :phone OR Phone_2__c = :phone];
  90.  
  91. for (Lead l: leads) {
  92. List<String> args = new List<String>(new String[] { l.FirstName, phone });
  93. String newSubject = String.format(subject, args);
  94. Task task = new Task(WhoId = l.Id, OwnerId = l.OwnerId, Description = body,
  95. ActivityDate = Date.today(), Subject = newSubject);
  96. insert task;
  97. }
  98. System.debug('SMS saved: ' + body);
  99.  
  100. } catch(System.CalloutException e) {
  101. System.debug('SMS error: ' + e);
  102. }
  103. return body;
  104. }
  105. }
  106.  
  107. @RestResource(urlMapping = '/textlead/')
  108. global with sharing class TextLeadReceiver {
  109.  
  110. @HttpGet
  111. global static String TextLeadReceiver() {
  112.  
  113. Public with sharing class TextLeadReceiver {
  114.  
  115. Public String TextLeadReceiver() {
  116.  
  117. ApexPages.currentPage().getParameters().get('paramName');
RAW Paste Data