Advertisement
Guest User

Untitled

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