@tiledesk/tiledesk-tybot-connector 0.1.60 → 0.1.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/index.js +5 -1
  3. package/models/MockBotsDataSource.js +10 -1
  4. package/models/TiledeskChatbot.js +2 -2
  5. package/models/TiledeskChatbotUtil.js +121 -0
  6. package/package.json +1 -1
  7. package/test/chatbot_util_test.js +161 -0
  8. package/test/conversation-form-test.js +45 -45
  9. package/test/json_condition-actions_bot.js +7 -12
  10. package/test/json_condition-conversation_test.js +1 -1
  11. package/test/json_condition-with-intent-params-conversation_test.js +282 -0
  12. package/test/json_condition-with-intent-params_bot.js +159 -0
  13. package/test/web_request-bot.js +133 -0
  14. package/test/web_request-conversation_test.js +236 -0
  15. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +121 -98
  16. package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +1 -1
  17. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +7 -7
  18. package/tiledeskChatbotPlugs/directives/DirDepartment.js +1 -1
  19. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +10 -18
  20. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +4 -2
  21. package/tiledeskChatbotPlugs/directives/DirIntent.js +23 -1
  22. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +8 -6
  23. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +37 -22
  24. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +109 -0
  25. package/tiledeskChatbotPlugs/directives/DirReply.js +18 -3
  26. package/tiledeskChatbotPlugs/directives/Directives.js +1 -0
  27. /package/test/{disable_input_text_directive_test.js → DEPRECATED-disable_input_text_directive_test.js_} +0 -0
package/CHANGELOG.md CHANGED
@@ -5,6 +5,26 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ ### 0.1.63
9
+ - Added stopOnConditionMet to DirIfOnlineAgents
10
+ - Removed deprecated Directive Directives.WHEN_OFFLINE_HOURS
11
+ - Removed deprecated Directive Directives.DISABLE_INPUT_TEXT
12
+ - Removed deprecated Directive Directives.WHEN_OPEN
13
+ - Removed deprecated Directive Directives.WHEN_CLOSED
14
+ - Removed deprecated Directive Directives.IF_AGENTS
15
+ - Removed deprecated Directive Directives.IF_NO_AGENTS
16
+ - Added support for intent parameters in DirJSONCondition DirIfOnlineAgents DirIfOpeningHours
17
+ - Added JSONCondition with intent parameters test
18
+
19
+ ### 0.1.63 - online
20
+ - Added support for depId-from-supportRequest in DirMoveToAgent. This fixes no-handoff in test-it-out and from Dialogflow connector
21
+
22
+ ### 0.1.62
23
+ - Added TiledeskChatbotUtil.fillCommandAttachments() replacing vars in commands.button.link
24
+
25
+ ### 0.1.61
26
+ - Added DirRandomReply, TiledeskChatbotUtil.chooseRandomReply()
27
+
8
28
  ### 0.1.60
9
29
  - fixed: get department_id from message.request instead of message.attributes
10
30
  - added JSONOperationToExpression evaluator in TiledeskExpression
package/index.js CHANGED
@@ -390,7 +390,11 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
390
390
  id_project: projectId
391
391
  }
392
392
  }
393
- // if (log) {console.log("request....", JSON.stringify(request));}
393
+ if (log) {
394
+ console.log("/ext request....", JSON.stringify(request));
395
+ console.log("/ext APIURL....", APIURL);
396
+ console.log("/ext process.env.TYBOT_ENDPOINT....", process.env.TYBOT_ENDPOINT);
397
+ }
394
398
  let directivesPlug = new DirectivesChatbotPlug({supportRequest: request, TILEDESK_API_ENDPOINT: APIURL, TILEBOT_ENDPOINT:process.env.TYBOT_ENDPOINT, token: token, log: log, HELP_CENTER_API_ENDPOINT: process.env.HELP_CENTER_API_ENDPOINT, cache: tdcache});
395
399
  // PIPELINE-EXT
396
400
  // if (log) {console.log("answer to process:", JSON.stringify(answer));}
@@ -64,7 +64,16 @@ class MockBotsDataSource {
64
64
  faq = await this.getByIntentDisplayName(botId, key);
65
65
  // console.log("faq found in datasource..:", JSON.stringify(faq));
66
66
  }
67
- return faq;
67
+ // clones the faq to avoid modifying original object
68
+ // console.log("faq is:", faq)
69
+ let json_faq;
70
+ if (faq !== null && faq !== undefined) {
71
+ let string_faq = JSON.stringify(faq)
72
+ // console.log("string faq is:", string_faq)
73
+ json_faq = JSON.parse(string_faq);
74
+ // console.log("json faq is:", json_faq)
75
+ }
76
+ return json_faq;
68
77
  }
69
78
 
70
79
  /**
@@ -580,7 +580,7 @@ class TiledeskChatbot {
580
580
  if (static_bot_answer.attributes && static_bot_answer.attributes.webhook && static_bot_answer.attributes.webhook === true) {
581
581
  const variables = await this.allParameters();
582
582
  context.variables = variables;
583
- if (this.log) {console.log("adding variables to webhook context:", context.variables);}
583
+ if (this.log) {console.log("adding variables to webhook context:", JSON.stringify(context.variables));}
584
584
  }
585
585
  const messagePipeline = new MessagePipeline(static_bot_answer, context);
586
586
  const webhookurl = bot.webhook_url;
@@ -625,7 +625,7 @@ class TiledeskChatbot {
625
625
  });
626
626
  // const parameters_key = "tilebot:requests:" + requestId + ":parameters";
627
627
  const all_parameters = await this.allParameters();//this.tdcache.hgetall(parameters_key);
628
- if (this.log) {console.log("(populatePrechatFormAndLead) parameters_key:", all_parameters);}
628
+ if (this.log) {console.log("(populatePrechatFormAndLead) parameters_key:", JSON.stringify(all_parameters));}
629
629
  if (all_parameters) {
630
630
  if (this.log) {console.log("(populatePrechatFormAndLead) userEmail:", all_parameters['userEmail']);}
631
631
  if (this.log) {console.log("(populatePrechatFormAndLead) userFullname:", all_parameters['userFullname']);}
@@ -1,4 +1,5 @@
1
1
  const { TiledeskExpression } = require('../TiledeskExpression');
2
+ const { Filler } = require('../tiledeskChatbotPlugs/Filler');
2
3
 
3
4
  class TiledeskChatbotUtil {
4
5
 
@@ -31,6 +32,105 @@ class TiledeskChatbotUtil {
31
32
 
32
33
  }
33
34
 
35
+ static chooseRandomReply(message) {
36
+
37
+ // {
38
+ // "_tdActionTitle": null,
39
+ // "_tdActionType": "randomreply",
40
+ // "attributes": {
41
+ // "disableInputMessage": false,
42
+ // "commands": [{
43
+ // "type": "wait",
44
+ // "time": 500
45
+ // }, {
46
+ // "type": "message",
47
+ // "message": {
48
+ // "type": "text",
49
+ // "text": "message1",
50
+ // "attributes": {
51
+ // "attachment": {
52
+ // "type": "template",
53
+ // "buttons": [{
54
+ // "value": "Button1",
55
+ // "type": "text",
56
+ // "target": "blank",
57
+ // "link": "",
58
+ // "action": "",
59
+ // "show_echo": true
60
+ // }]
61
+ // }
62
+ // }
63
+ // }
64
+ // }, {
65
+ // "type": "wait",
66
+ // "time": 500
67
+ // }, {
68
+ // "type": "message",
69
+ // "message": {
70
+ // "type": "text",
71
+ // "text": "message2"
72
+ // }
73
+ // }, {
74
+ // "type": "wait",
75
+ // "time": 500
76
+ // }, {
77
+ // "type": "message",
78
+ // "message": {
79
+ // "type": "image",
80
+ // "text": "message3 - image",
81
+ // "metadata": {
82
+ // "src": ""
83
+ // }
84
+ // }
85
+ // }, {
86
+ // "type": "wait",
87
+ // "time": 500
88
+ // }, {
89
+ // "type": "message",
90
+ // "message": {
91
+ // "type": "text",
92
+ // "text": "message4",
93
+ // "attributes": {
94
+ // "attachment": {
95
+ // "type": "template",
96
+ // "buttons": [{
97
+ // "value": "Button4",
98
+ // "type": "text",
99
+ // "target": "blank",
100
+ // "link": "",
101
+ // "action": "",
102
+ // "show_echo": true
103
+ // }]
104
+ // }
105
+ // }
106
+ // }
107
+ // }]
108
+ // },
109
+ // "text": "message1\r\nmessage2\r\nmessage3 - image\r\nmessage4\r\n"
110
+ // }
111
+
112
+ if (message && message.attributes && message.attributes.commands) {
113
+ let commands = message.attributes.commands;
114
+ if (commands.length %2 != 0) {
115
+ console.log("Error. commands.length cannot be an odd number");
116
+ return null;
117
+ }
118
+ const MAX_VALUE = commands.length - 1;
119
+ let random_even_index = Math.round((Math.random() * MAX_VALUE));
120
+ if (random_even_index %2 == 0){//generated number is even
121
+ random_even_index = random_even_index + 1;
122
+ }
123
+ let new_commands = [];
124
+ new_commands.push(commands[random_even_index - 1]); // pushed the wait
125
+ new_commands.push(commands[random_even_index]); // pushed the message
126
+ return new_commands;
127
+ }
128
+ else {
129
+ return null;
130
+ }
131
+
132
+ }
133
+
34
134
  // static errorMessage(message) {
35
135
  // return {
36
136
  // name: "message",
@@ -91,6 +191,27 @@ class TiledeskChatbotUtil {
91
191
  }
92
192
  }
93
193
 
194
+ static fillCommandAttachments(command, variables, log) {
195
+ if (log) {
196
+ console.log("filling command button:", JSON.stringify(command))
197
+ }
198
+ if (command.message && command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.buttons && command.message.attributes.attachment.buttons.length > 0){
199
+ let buttons = command.message.attributes.attachment.buttons;
200
+ const filler = new Filler();
201
+ buttons.forEach(button => {
202
+ if (button.link) {
203
+ button.link = filler.fill(button.link, variables);
204
+ if (log) {
205
+ console.log("button.link filled:", button.link)
206
+ }
207
+ }
208
+ });
209
+ }
210
+ else if (log) {
211
+ console.log("No attachments to fill in command")
212
+ }
213
+ }
214
+
94
215
  }
95
216
 
96
217
  module.exports = { TiledeskChatbotUtil };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.1.60",
3
+ "version": "0.1.63",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -50,5 +50,166 @@ describe('Intent name parsing', function() {
50
50
 
51
51
  });
52
52
 
53
+ describe('Random reply', function() {
54
+
55
+ // it('One random reply', async () => {
56
+ // const message = {
57
+ // "attributes": {
58
+ // "disableInputMessage": false,
59
+ // "commands": [{
60
+ // "type": "wait",
61
+ // "time": 500
62
+ // }, {
63
+ // "type": "message",
64
+ // "message": {
65
+ // "type": "text",
66
+ // "text": "message1",
67
+ // "attributes": {
68
+ // "attachment": {
69
+ // "type": "template",
70
+ // "buttons": [{
71
+ // "value": "Button1",
72
+ // "type": "text",
73
+ // "target": "blank",
74
+ // "link": "",
75
+ // "action": "",
76
+ // "show_echo": true
77
+ // }]
78
+ // }
79
+ // }
80
+ // }
81
+ // }]
82
+ // }
83
+ // }
84
+ // const rnd_commands = TiledeskChatbotUtil.chooseRandomReply(message);
85
+ // console.log("random reply:", rnd_commands);
86
+ // assert(rnd_commands.length === 2);
87
+ // assert(rnd_commands[0].type === "wait");
88
+ // assert(rnd_commands[0].time === 500);
89
+ // assert(rnd_commands[1].type === "message");
90
+ // assert(rnd_commands[1].message.type === "text");
91
+ // assert(rnd_commands[1].message.text === "message1");
92
+ // assert(rnd_commands[1].message.attributes !== null);
93
+ // assert(rnd_commands[1].message.attributes.attachment.type === "template");
94
+ // assert(rnd_commands[1].message.attributes.attachment.buttons !== null);
95
+ // assert(rnd_commands[1].message.attributes.attachment.buttons[0].value === "Button1");
96
+ // });
97
+
98
+ it('Choose one random reply in a group of four', async () => {
99
+ const message = {
100
+ "attributes": {
101
+ "commands": [{
102
+ "type": "wait",
103
+ "time": 500
104
+ }, {
105
+ "type": "message",
106
+ "message": {
107
+ "type": "text",
108
+ "text": "message1",
109
+ "attributes": {
110
+ "attachment": {
111
+ "type": "template",
112
+ "buttons": [{
113
+ "value": "Button1",
114
+ "type": "text",
115
+ "target": "blank",
116
+ "link": "",
117
+ "action": "",
118
+ "show_echo": true
119
+ }]
120
+ }
121
+ }
122
+ }
123
+ }, {
124
+ "type": "wait",
125
+ "time": 500
126
+ }, {
127
+ "type": "message",
128
+ "message": {
129
+ "type": "text",
130
+ "text": "message2"
131
+ }
132
+ }, {
133
+ "type": "wait",
134
+ "time": 500
135
+ }, {
136
+ "type": "message",
137
+ "message": {
138
+ "type": "image",
139
+ "text": "message3 - image",
140
+ "metadata": {
141
+ "src": ""
142
+ }
143
+ }
144
+ }, {
145
+ "type": "wait",
146
+ "time": 500
147
+ }, {
148
+ "type": "message",
149
+ "message": {
150
+ "type": "text",
151
+ "text": "message4",
152
+ "attributes": {
153
+ "attachment": {
154
+ "type": "template",
155
+ "buttons": [{
156
+ "value": "Button4",
157
+ "type": "text",
158
+ "target": "blank",
159
+ "link": "",
160
+ "action": "",
161
+ "show_echo": true
162
+ }]
163
+ }
164
+ }
165
+ }
166
+ }]
167
+ }
168
+ }
169
+ const rnd_commands = TiledeskChatbotUtil.chooseRandomReply(message);
170
+ // console.log("random reply:", rnd_commands);
171
+ assert(rnd_commands.length === 2);
172
+ assert(rnd_commands[0].type === "wait");
173
+ assert(rnd_commands[0].time === 500);
174
+ assert(rnd_commands[1].type === "message");
175
+ assert(rnd_commands[1].message.type === "text" || rnd_commands[1].message.type === "image");
176
+ assert(rnd_commands[1].message.text === "message1" || rnd_commands[1].message.text === "message2" || rnd_commands[1].message.text === "message3 - image" || rnd_commands[1].message.text === "message4");
177
+ });
178
+ });
179
+
180
+ describe('commands.button filler', function() {
181
+
182
+ it('commands.button: one variable in a link', async () => {
183
+ const command = {
184
+ "type": "message",
185
+ "message": {
186
+ "type": "text",
187
+ "text": "I don't know!",
188
+ "attributes": {
189
+ "attachment": {
190
+ "type": "template",
191
+ "buttons": [{
192
+ "value": "button text",
193
+ "type": "url",
194
+ "target": "blank",
195
+ "link": "http://testurl.com/${project_id}/detail"
196
+ }]
197
+ }
198
+ }
199
+ }
200
+ }
201
+ const vars = {
202
+ "project_id": "009988"
203
+ }
204
+ TiledeskChatbotUtil.fillCommandAttachments(command, vars);
205
+ // console.log("command:", JSON.stringify(command))
206
+ assert(command.message.attributes.attachment.buttons[0].value === "button text");
207
+ assert(command.message.attributes.attachment.buttons[0].type === "url");
208
+ assert(command.message.attributes.attachment.buttons[0].target === "blank");
209
+ assert(command.message.attributes.attachment.buttons[0].link === "http://testurl.com/009988/detail");
210
+ });
211
+
212
+ });
213
+
53
214
 
54
215
 
@@ -138,55 +138,55 @@ describe('Conversation1 - Form filling', async () => {
138
138
  });
139
139
  });
140
140
 
141
- it('/disable_input', (done) => {
142
- // console.log("/disable_input...");
143
- let listener;
144
- let endpointServer = express();
145
- endpointServer.use(bodyParser.json());
146
- endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
147
- // console.log("req.body....:", JSON.stringify(req.body));
148
- res.send({ success: true });
149
- const message = req.body;
141
+ // it('/disable_input', (done) => {
142
+ // // console.log("/disable_input...");
143
+ // let listener;
144
+ // let endpointServer = express();
145
+ // endpointServer.use(bodyParser.json());
146
+ // endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
147
+ // // console.log("req.body....:", JSON.stringify(req.body));
148
+ // res.send({ success: true });
149
+ // const message = req.body;
150
150
 
151
- assert(message["text"] !== "");
152
- assert(message["attributes"] !== "");
153
- assert(message["attributes"]["disableInputMessage"] === true);
151
+ // assert(message["text"] !== "");
152
+ // assert(message["attributes"] !== "");
153
+ // assert(message["attributes"]["disableInputMessage"] === true);
154
154
 
155
- listener.close(() => {
156
- // console.log('closed.');
157
- done();
158
- });
155
+ // listener.close(() => {
156
+ // // console.log('closed.');
157
+ // done();
158
+ // });
159
159
 
160
- });
160
+ // });
161
161
 
162
- listener = endpointServer.listen(10002, '0.0.0.0', function () {
163
- // console.log('endpointServer started', listener.address());
164
- // const botId = process.env.TEST_BOT_ID;
165
- // const PROJECT_ID = process.env.TEST_PROJECT_ID;
166
- // console.log("botId:", botId);
167
- // console.log("REQUEST_ID:", REQUEST_ID);
168
- let request = {
169
- "payload": {
170
- "_id": uuidv4(),
171
- "senderFullname": "guest#367e",
172
- "type": "text",
173
- "sender": "A-SENDER",
174
- "recipient": REQUEST_ID,
175
- "text": "/disable_input",
176
- "id_project": PROJECT_ID,
177
- "metadata": "",
178
- "request": {
179
- "request_id": REQUEST_ID,
180
- "id_project": PROJECT_ID
181
- }
182
- },
183
- "token": CHATBOT_TOKEN
184
- }
185
- sendMessageToBot(request, BOT_ID, CHATBOT_TOKEN, () => {
186
- // console.log("Message sent.");
187
- });
188
- });
189
- });
162
+ // listener = endpointServer.listen(10002, '0.0.0.0', function () {
163
+ // // console.log('endpointServer started', listener.address());
164
+ // // const botId = process.env.TEST_BOT_ID;
165
+ // // const PROJECT_ID = process.env.TEST_PROJECT_ID;
166
+ // // console.log("botId:", botId);
167
+ // // console.log("REQUEST_ID:", REQUEST_ID);
168
+ // let request = {
169
+ // "payload": {
170
+ // "_id": uuidv4(),
171
+ // "senderFullname": "guest#367e",
172
+ // "type": "text",
173
+ // "sender": "A-SENDER",
174
+ // "recipient": REQUEST_ID,
175
+ // "text": "/disable_input",
176
+ // "id_project": PROJECT_ID,
177
+ // "metadata": "",
178
+ // "request": {
179
+ // "request_id": REQUEST_ID,
180
+ // "id_project": PROJECT_ID
181
+ // }
182
+ // },
183
+ // "token": CHATBOT_TOKEN
184
+ // }
185
+ // sendMessageToBot(request, BOT_ID, CHATBOT_TOKEN, () => {
186
+ // // console.log("Message sent.");
187
+ // });
188
+ // });
189
+ // });
190
190
 
191
191
  it('/good_form', (done) => {
192
192
  // console.log("/good_form...");
@@ -213,8 +213,7 @@ const bot = {
213
213
  "actions": [{
214
214
  "_tdActionTitle": null,
215
215
  "_tdActionType": "jsoncondition",
216
- "jsonCondition": {
217
- "groups": [{
216
+ "groups": [{
218
217
  "type": "expression",
219
218
  "conditions": [{
220
219
  "type": "condition",
@@ -226,15 +225,13 @@ const bot = {
226
225
  "name": ""
227
226
  }
228
227
  }]
229
- }]
230
- },
228
+ }],
231
229
  "stopOnConditionMet": true,
232
230
  "trueIntent": "#d19b1b7a-7146-481d-a7d5-6b9cce8fcb50"
233
231
  }, {
234
232
  "_tdActionTitle": null,
235
233
  "_tdActionType": "jsoncondition",
236
- "jsonCondition": {
237
- "groups": [{
234
+ "groups": [{
238
235
  "type": "expression",
239
236
  "conditions": [{
240
237
  "type": "condition",
@@ -246,15 +243,13 @@ const bot = {
246
243
  "name": ""
247
244
  }
248
245
  }]
249
- }]
250
- },
246
+ }],
251
247
  "stopOnConditionMet": true,
252
248
  "trueIntent": "#16240c0b-9618-40cd-87e7-6319e5c3e392"
253
249
  }, {
254
250
  "_tdActionTitle": null,
255
251
  "_tdActionType": "jsoncondition",
256
- "jsonCondition": {
257
- "groups": [{
252
+ "groups": [{
258
253
  "type": "expression",
259
254
  "conditions": [{
260
255
  "type": "condition",
@@ -278,8 +273,8 @@ const bot = {
278
273
  "name": ""
279
274
  }
280
275
  }]
281
- }]
282
- },
276
+ }],
277
+
283
278
  "stopOnConditionMet": false,
284
279
  "trueIntent": "#98dd1994-83ef-4863-8179-07c48d2194b9"
285
280
  }],
@@ -59,7 +59,7 @@ describe('Conversation for JSONCondition test', async () => {
59
59
  let endpointServer = express();
60
60
  endpointServer.use(bodyParser.json());
61
61
  endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
62
- // console.log("...req.body:", JSON.stringify(req.body));
62
+ console.log("...req.body:", JSON.stringify(req.body));
63
63
  res.send({ success: true });
64
64
  const message = req.body;
65
65
  assert(message.attributes.commands !== null);