1 | class ButtonSet {
|
2 | constructor() {
|
3 | this._buttons = [];
|
4 | }
|
5 |
|
6 | add({text, data, url, event}) {
|
7 | if (!data && !url) {
|
8 | throw Error('Must provide a url or data i.e. {data: null} or {url: \'https://facebook.com\'}');
|
9 | }
|
10 |
|
11 | this._buttons.push({text: text || 'Button', event, data, url});
|
12 | return this;
|
13 | }
|
14 |
|
15 | toJSON() {
|
16 | const buttons = [];
|
17 | for (const button of this._buttons) {
|
18 | if (button.url) {
|
19 | buttons.push({type: 'web_url', url: button.url, title: button.text});
|
20 | } else if (button.data) {
|
21 | const payload = JSON.stringify({data: button.data, event: button.event});
|
22 | buttons.push({type: 'postback', payload, title: button.text});
|
23 | }
|
24 | }
|
25 |
|
26 | return buttons;
|
27 | }
|
28 |
|
29 | get length() {
|
30 | return this._buttons.length;
|
31 | }
|
32 | }
|
33 |
|
34 | export default ButtonSet;
|