1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | const flat_1 = require("flat");
|
19 | const merge = require("lodash.merge");
|
20 | const forOwn = require("lodash.forown");
|
21 |
|
22 |
|
23 |
|
24 | function buttonForCommand(buttonSpec, command, parameters = {}) {
|
25 | const params = mergeParameters(command, parameters);
|
26 | const id = command.toLocaleLowerCase();
|
27 | const action = chatButtonFrom(buttonSpec, { id });
|
28 | action.command = {
|
29 | id,
|
30 | name: command,
|
31 | parameters: params,
|
32 | };
|
33 | return action;
|
34 | }
|
35 | exports.buttonForCommand = buttonForCommand;
|
36 |
|
37 |
|
38 |
|
39 | function menuForCommand(selectSpec, command, parameterName, parameters = {}) {
|
40 | const params = mergeParameters(command, parameters);
|
41 | const id = command.toLocaleLowerCase();
|
42 | const action = chatMenuFrom(selectSpec, { id, parameterName });
|
43 | action.command = {
|
44 | id,
|
45 | name: command,
|
46 | parameters: params,
|
47 | parameterName,
|
48 | };
|
49 | return action;
|
50 | }
|
51 | exports.menuForCommand = menuForCommand;
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function mergeParameters(command, parameters) {
|
57 |
|
58 | if (typeof command !== "string" && typeof command !== "function") {
|
59 | const newParameters = merge(command, parameters);
|
60 | return flat_1.flatten(newParameters);
|
61 | }
|
62 | return flat_1.flatten(parameters);
|
63 | }
|
64 | exports.mergeParameters = mergeParameters;
|
65 | function chatButtonFrom(action, command) {
|
66 | if (!command.id) {
|
67 | throw new Error(`Please provide a valid non-empty command id`);
|
68 | }
|
69 | const button = {
|
70 | text: action.text,
|
71 | type: "button",
|
72 | name: `automation-command::${command.id}`,
|
73 | };
|
74 | forOwn(action, (v, k) => {
|
75 | button[k] = v;
|
76 | });
|
77 | return button;
|
78 | }
|
79 | function chatMenuFrom(action, command) {
|
80 | if (!command.id) {
|
81 | throw new Error("SelectableIdentifiableInstruction must have id set");
|
82 | }
|
83 | if (!command.parameterName) {
|
84 | throw new Error("SelectableIdentifiableInstruction must have parameterName set");
|
85 | }
|
86 | const select = {
|
87 | text: action.text,
|
88 | type: "select",
|
89 | name: `automation-command::${command.id}`,
|
90 | };
|
91 | if (typeof action.options === "string") {
|
92 | select.data_source = action.options;
|
93 | }
|
94 | else if (action.options.length > 0) {
|
95 | const first = action.options[0];
|
96 | if (first.value) {
|
97 |
|
98 | select.options = action.options;
|
99 | }
|
100 | else {
|
101 |
|
102 | select.option_groups = action.options;
|
103 | }
|
104 | }
|
105 | forOwn(action, (v, k) => {
|
106 | if (k !== "options") {
|
107 | select[k] = v;
|
108 | }
|
109 | });
|
110 | return select;
|
111 | }
|
112 |
|
\ | No newline at end of file |