Clowerweb

Twilio-Jquery

Sep 12th, 2018
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2. // Show error messages on the page
  3. // @param array errorList - errors to show
  4. function showSmsErrors(errorList) {
  5.   if(errorList) {
  6.     // open an errors list
  7.     var errors = '<ul class="errors">';
  8.  
  9.     // create a list item for each error
  10.     errorList.forEach(function(error) {
  11.       errors += '<li>' + error + '</li>';
  12.     });
  13.  
  14.     // close the errors list
  15.     errors += '</ul>';
  16.  
  17.     // display errors list
  18.     $('#settings-test-sms-status').html('<strong>The following errors occurred:</strong>' + errors);
  19.   }
  20. }
  21.  
  22. // when they click the "Send a test button"
  23. $('#settings-sms-test').on('click', function(e) {
  24.   // prevent form submit
  25.   e.preventDefault();
  26.  
  27.   // craft csrf token
  28.   var csrf    = $('[name="CRAFT_CSRF_TOKEN"]').val();
  29.   // hidden action field (default: /actions/text-it3/textit/test-sms)
  30.   var action  = '/actions/text-it3/textit/test-sms';
  31.   // "Twilio Number" field
  32.   var from    = $('#settings-from').val();
  33.   // "Send Message To" field
  34.   var to      = $('#settings-to').val();
  35.   // "Message prefix" field
  36.   var prefix  = $('#settings-msgPrefix').val();
  37.   // "Message postfix" field
  38.   var postfix = $('#settings-msgPostfix').val();
  39.   // Twilio SID field
  40.   var sid     = $('#settings-sid').val();
  41.   // Twilio auth token field
  42.   var auth    = $('#settings-authToken').val();
  43.   // the querystring to send
  44.   var data    = '';
  45.  
  46.   // build the querystring. values must be uri encoded
  47.   data += 'CRAFT_CSRF_TOKEN=' + encodeURIComponent(csrf);    // craft csrf token
  48.   data += '&action='          + encodeURIComponent(action);  // action (default: /actions/text-it3/textit/test-sms)
  49.   data += '&from='            + encodeURIComponent(from);    // "Twilio Number" field
  50.   data += '&to='              + encodeURIComponent(to);      // "Send Message To" field
  51.   data += '&prefix='          + encodeURIComponent(prefix);  // "Message prefix" field
  52.   data += '&postfix='         + encodeURIComponent(postfix); // "Message postfix" field
  53.   data += '&sid='             + encodeURIComponent(sid);     // Twilio SID field
  54.   data += '&auth='            + encodeURIComponent(auth);    // Twilio auth token field
  55.  
  56.   // send the ajax post
  57.   $.ajax({
  58.     type: 'POST',
  59.     // all Craft post requests must be sent to /
  60.     // with an action in the querystring of pluginHandle/Controller/methodName
  61.     url : action,
  62.     data: data,
  63.     // send a header saying this is url encoded form data
  64.     headers: {'Content-Type': 'application/x-www-form-urlencoded'},
  65.     // successfully sent the post
  66.     success: function(result) {
  67.       console.log(result);
  68.  
  69.       // parse the response as json
  70.       var res = JSON.parse(result);
  71.  
  72.       // if the message sent successfully
  73.       if(res.success) {
  74.         // show the success message
  75.         $('#settings-test-sms-status').text('A test message was sent successfully to: ' + to + ' ');
  76.         // there were errors
  77.       } else {
  78.         showSmsErrors(res.errors);
  79.       }
  80.     },
  81.     statusCode: {
  82.       500: function() {
  83.         showSmsErrors(['An unknown error occurred while trying to send the SMS. Please ensure all settings are correct.']);
  84.       }
  85.     }
  86.   }); // end ajax post
  87. }); // end "Send a test text" click event
  88. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment