all files / lib/ ThreadSettings.js

100% Statements 0/0
100% Branches 0/0
100% Functions 0/0
100% Lines 0/0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                                                                                                                                                                                                 
class GreetingText {
  constructor(text) {
    this.text = text;
 
    return {
      setting_type: 'greeting',
      greeting: {
        text: this.text,
      },
    };
  }
}
 
class GetStartedButton {
  constructor(payload) {
    this.payload = payload;
 
    return {
      setting_type: 'call_to_actions',
      thread_state: 'new_thread',
      call_to_actions: [
        {
          payload: this.payload,
        },
      ],
    };
  }
}
 
class PersistentMenuItem {
  constructor({ type, title, url = '', payload = '' }) {
    const types = [
      'web_url',
      'postback',
    ];
 
    if (types.indexOf(type) === -1) {
      throw new Error('Invalid type provided.');
    }
 
    if (title.length > 30) {
      throw new Error('Title cannot be longer 30 characters.');
    }
 
    if (payload && payload.length > 1000) {
      throw new Error('Payload cannot be longer 1000 characters.');
    }
 
    if (type === 'web_url' && !url) {
      throw new Error('`url` must be supplied for `web_url` type menu items.');
    }
 
    if (type === 'postback' && !payload) {
      throw new Error('`payload` must be supplied for `postback` type menu items.');
    }
 
    this.type = type;
    this.title = title;
    this.url = url;
    this.payload = payload;
 
    const res = {
      type: this.type,
      title: this.title,
    };
 
    if (this.url && this.type === 'web_url') {
      res.url = this.url;
    }
 
    if (this.payload && this.type === 'postback') {
      res.payload = this.payload;
    }
 
    return res;
  }
}
 
class PersistentMenu {
  constructor(menuItems) {
    if (!Array.isArray(menuItems)) {
      throw new Error('You must pass an array of PersistentMenuItem objects.');
    }
 
    if (menuItems.length > 5) {
      throw new Error('You cannot have more than 5 menu items.');
    }
 
    this.menuItems = menuItems;
 
    return {
      setting_type: 'call_to_actions',
      thread_state: 'existing_thread',
      call_to_actions: this.menuItems,
    };
  }
}
 
export {
  GreetingText,
  GetStartedButton,
  PersistentMenuItem,
  PersistentMenu,
};