Guest User

Untitled

a guest
Aug 21st, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. /**
  2. * @author Allen Watson
  3. * @version 1.0
  4. * @since 1.0
  5. *
  6. * Used to handle communication with slybroadcast Servers
  7. */
  8. public with sharing class callout {
  9.  
  10. public String Username;
  11. public String Password;
  12.  
  13. public callout(){
  14. Username = '{!$Credential.UserName}';
  15. Password = '{!$Credential.Password}';
  16. }
  17.  
  18.  
  19. /**
  20. * Sends a voice message request to slybroadcast
  21. *
  22. * @param phone - The phone number that is to receive the voice message.
  23. * @param audioRecord - The voice message that is to be sent.
  24. * @param cdate - The date and time the voice message should be sent.
  25. * @param callerID - The phone number that you want to send the voice message from.
  26. * @param mobileOnly - Do you want to send the voice message to mobile phones only?
  27. * @return Boolean - return value relays success or failure to the parent.
  28. * @since 1.0
  29. */
  30. public Boolean sendRequest(String phone, String audioRecord, String cdate, String callerID, String mobileOnly)
  31. {
  32. // remove username and password as it is handled by named credential
  33. HttpRequest req = new HttpRequest();
  34. HttpResponse res = new HttpResponse();
  35. Http http = new Http();
  36. req.setMethod('POST'); // Method Type
  37. req.setEndpoint('callout:SlyUserCred/vmb.php'); // Server Url
  38. req.setBody('c_uid='+Username+
  39. '&c_password='+Password+ '&c_phone=' + EncodingUtil.urlEncode(phone, 'UTF-8') +
  40. '&c_record_audio=' + EncodingUtil.urlEncode(audioRecord, 'UTF-8') + '&c_callerID=' +
  41. EncodingUtil.urlEncode(callerID,'UTF-8') + '&c_date=' + EncodingUtil.urlEncode(cdate, 'UTF-8') +
  42. '&c_audio='+ EncodingUtil.urlEncode('wav', 'UTF-8') + '&mobile_only=' + EncodingUtil.urlEncode(mobileOnly, 'UTF-8')); // Request Parameters
  43. try
  44. {
  45. //processing the response from slybroadcast.
  46. res = http.send(req);
  47. system.debug(res.getBody());
  48. if(res.getBody() != null)
  49. {
  50. system.debug(res.getBody());
  51. return true;
  52. }
  53. return true;
  54.  
  55. }
  56. catch(Exception e)
  57. {
  58. System.debug('error: '+ e);
  59. return false;
  60. }
  61. }
  62.  
  63. /**
  64. * Use to ask the slybroadcast severs for a list of stored audio file names.
  65. *
  66. * @return List<String> - returns a list of all the audiofile accociated with a slybroadcast account.
  67. * @since 1.0
  68. */
  69. public List<String> getAudioFiles()
  70. {
  71. // remove username and password as it is handled by named credential
  72. List<String> rtnString = new List<String>();
  73.  
  74. //Creates HttpRequest and builds the request.
  75. HttpRequest req = new HttpRequest();
  76. HttpResponse res = new HttpResponse();
  77. Http http = new Http();
  78. req.setMethod('POST'); // Method Type
  79. req.setEndpoint('callout:SlyUserCred/vmb.aflist.php'); // Server Url
  80. req.setBody('c_uid='+Username+
  81. '&c_password='+Password+
  82. '&c_method=' + EncodingUtil.urlEncode('get_audio_list', 'UTF-8'));
  83.  
  84. try
  85. {
  86. //Processing the response from slybroadcast
  87. res = http.send(req);
  88. system.debug(res.getBody());
  89. if(res.getBody() != null)
  90. {
  91. //Splitting up the audioRecords into a list
  92. String unsplitString = res.getBody();
  93. List<String> firstSplitString = unsplitString.split('n');
  94. List<String> secondSplitString = new List<String>();
  95. for(String s : firstSplitString)
  96. {
  97. secondSplitString = s.split('\|');
  98. String temp = secondSplitString[1];
  99. secondSplitString[1] = temp.remove('"');
  100. rtnString.add(secondSplitString[1]);
  101. }
  102. return rtnString;
  103. }
  104. return rtnString;
  105. }
  106. catch(Exception e)
  107. {
  108. rtnString.add('error: '+ e);
  109. return rtnString;
  110. }
  111. }
  112. //used to verify that the slybroadcast account that the user entered is a valid one.
  113. public Boolean validateAccount()
  114. {
  115. // remove username and password as it is handled by named credential
  116. Boolean isValid = false;
  117. HttpRequest req = new HttpRequest();
  118. HttpResponse res = new HttpResponse();
  119. Http http = new Http();
  120. req.setMethod('POST'); // Method Type
  121. req.setEndpoint('callout:SlyUserCred/vmb.php'); // Server Url
  122. req.setBody('c_uid='+Username+
  123. '&c_password='+Password+
  124. '&c_option=user_verify'); // Request Parameters
  125. system.debug('CAL116: '+req.getBody());
  126. try
  127. {
  128. res = http.send(req);
  129. system.debug('Res: '+res.getBody());
  130. if(res.getBody() == 'OK')
  131. {
  132. isValid = true;
  133. }else
  134. {
  135. //throw new CalloutException(res.getBody());
  136. }
  137.  
  138. }
  139. catch(Exception e)
  140. {
  141. throw e;
  142. }
  143. return isValid;
  144. }
  145. }
  146.  
  147. public with sharing class WelcomeController {
  148. public callout Broadcaster;
  149.  
  150. public WelcomeController(){
  151. Broadcaster = new callout();
  152. //System.debug('Got Here');
  153. }
  154.  
  155. public PageReference ValidateSlybroadcastAccount(){
  156. PageReference ValidationResult = Page.SlybroadcastAcctValidVFP;
  157. Boolean isValid = Broadcaster.validateAccount();
  158. if(!isValid) ValidationResult = Page.SlybroadcastAcctInvalidVFP;
  159. System.debug('Is Slybroadcast Account Valid: '+isValid);
  160. return ValidationResult;
  161. }
  162.  
  163. public PageReference ReturnToWelcome(){
  164. return Page.SlybroadcastWelcomeVFP;
  165. }
  166. }
  167.  
  168. <apex:page controller="WelcomeController">
  169. <apex:pageBlock title="Welcome to Slybroadcast." tabStyle="Message__c">
  170. <apex:form >
  171. <apex:pageBlockSection title="Account Validation" collapsible="true">
  172. <apex:commandLink action="/0XU/e?retURL=%2Fudd%2FExternalDataUserAuth%2FlistExternalDataUserAuth.apexp" value="Add Slybroadcast Credentials to Salesforce."/>
  173. <apex:commandLink action="{!ValidateSlybroadcastAccount}" value="Validate Slybroadcast Credentials."/>
  174. </apex:pageBlockSection>
  175. <apex:pageBlockSection title="More" collapsible="true">
  176. <apex:commandLink action="https://drive.google.com/drive/folders/11X2d6Ml1249BVFMkPfaPd9VlHs4CgZfG?usp=sharing" value="Visual Aids and more instructions."/>
  177. </apex:pageBlockSection>
  178. </apex:form>
  179. </apex:pageBlock>
Add Comment
Please, Sign In to add comment