Guest User

Untitled

a guest
Oct 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. /**
  2. * This adapter handles the configuration from AppData, check first if we have stored values in localstorage and it hasn't expired.
  3. * @param {object} appData
  4. */
  5. function updateConfigurationFromAppData(appData){
  6. return function(chatBot){
  7. chatBot.subscriptions.onReady(function(next) {
  8. var newLabels={};
  9. var botConfig={};
  10. //check if localstorage exists
  11. if (typeof(Storage) !== "undefined") {
  12. newLabels = JSON.parse(localStorage.getItem('labelsAppData'));
  13. if(newLabels == null){ //Check if we previously saved the configuration in localstorage
  14. return appDatatoLabels(chatBot,appData,true);
  15. }else {
  16. now = new Date().getTime().toString();
  17. if(now < newLabels.expiration){ //Check the expiration saved inside the localstorage(30 minutes)
  18. botConfig.labels = newLabels;
  19. chatBot.actions.updateConfiguration(botConfig);
  20. return Promise.resolve();
  21. }
  22. return appDatatoLabels(chatBot,appData);
  23. }
  24. } else {
  25. return appDatatoLabels(chatBot,appData);
  26. }
  27. });
  28. };
  29. }
  30. /** Performs the appData request, transform labels especial characters and saves this labels in the localstorage with 30 minutes expiration
  31. *
  32. * @param {object} chatBot
  33. * @param {object} appData
  34. * @param {boolean} useLocalstorage
  35. */
  36. function appDatatoLabels(chatBot,appData,useLocalstorage=false){
  37. var newLabels = {};
  38. var botConfig = {};
  39. //Perform AppData api request
  40. return chatBot.api.getAppData(appData).then(({data})=>{
  41. var labelsAppData = data.results[0].value;
  42. //We need to parse all characters found with "_" to "-" since appData doesn't accept special characters such as "-", and the labels are defined using them.
  43. for (var property in labelsAppData) {
  44. newKey = property.replace(/_/g, "-"); // parser ...
  45. newLabels[newKey] = labelsAppData[property];
  46. }
  47. botConfig.labels = newLabels;
  48. //perform updateConfiguration action,that will update the labels
  49. chatBot.actions.updateConfiguration(botConfig);
  50. botConfig.labels.expiration = new Date().getTime()+1800000; //sets the expiration in 30 minutes
  51. //Saves the labels in LocalStorage.
  52. if(useLocalstorage){
  53. localStorage.setItem('labelsAppData',JSON.stringify(botConfig.labels));
  54. }
  55. });
  56. }
Add Comment
Please, Sign In to add comment