@tiledesk/tiledesk-tybot-connector 0.2.601-rc1 → 0.3.1

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 (73) hide show
  1. package/CHANGELOG.md +387 -1
  2. package/ExtApi.js +6 -7
  3. package/Logger.js +74 -0
  4. package/TdCache.js +81 -176
  5. package/TdCache_v3.js +261 -0
  6. package/TiledeskExpression.js +7 -3
  7. package/TiledeskServices/AIService.js +43 -0
  8. package/TiledeskServices/utils.js +99 -0
  9. package/index.js +305 -50
  10. package/logs/app.log +279 -0
  11. package/models/IntentsMachineFactory.js +5 -2
  12. package/models/MockBotsDataSource.js +19 -11
  13. package/models/TiledeskChatbot.js +97 -79
  14. package/models/TiledeskChatbotConst.js +12 -17
  15. package/models/TiledeskChatbotUtil.js +359 -109
  16. package/models/TiledeskIntentsMachine.js +1 -1
  17. package/models/faqKbService.js +1 -1
  18. package/package.json +8 -6
  19. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +172 -106
  20. package/tiledeskChatbotPlugs/Filler.js +13 -2
  21. package/tiledeskChatbotPlugs/TildeskContextForCodeOrchestrator.js +8 -0
  22. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +13 -7
  23. package/tiledeskChatbotPlugs/directives/DirAddTags.js +374 -0
  24. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +476 -0
  25. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +16 -19
  26. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +221 -34
  27. package/tiledeskChatbotPlugs/directives/DirAssign.js +0 -11
  28. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +11 -21
  29. package/tiledeskChatbotPlugs/directives/DirAssistant.js +728 -0
  30. package/tiledeskChatbotPlugs/directives/DirBrevo.js +353 -0
  31. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +3 -30
  32. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +22 -0
  33. package/tiledeskChatbotPlugs/directives/DirClose.js +16 -3
  34. package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
  35. package/tiledeskChatbotPlugs/directives/DirCondition.js +0 -26
  36. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +182 -0
  37. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +121 -0
  38. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -8
  39. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +11 -1
  40. package/tiledeskChatbotPlugs/directives/DirDepartment.js +15 -6
  41. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +17 -6
  42. package/tiledeskChatbotPlugs/directives/DirForm.js +12 -2
  43. package/tiledeskChatbotPlugs/directives/DirGptTask.js +83 -38
  44. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +4 -7
  45. package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -8
  46. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +14 -27
  47. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +278 -0
  48. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +147 -51
  49. package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +125 -0
  50. package/tiledeskChatbotPlugs/directives/DirIntent.js +8 -37
  51. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +5 -26
  52. package/tiledeskChatbotPlugs/directives/DirMessage.js +19 -17
  53. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +135 -0
  54. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +20 -87
  55. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +59 -0
  56. package/tiledeskChatbotPlugs/directives/DirQapla.js +6 -9
  57. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +17 -7
  58. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +17 -7
  59. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +11 -2
  60. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +135 -21
  61. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +163 -0
  62. package/tiledeskChatbotPlugs/directives/DirReply.js +53 -9
  63. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +347 -0
  64. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +13 -23
  65. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +247 -0
  66. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +202 -15
  67. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +13 -4
  68. package/tiledeskChatbotPlugs/directives/DirWait.js +21 -4
  69. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -2
  70. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +166 -103
  71. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +2 -60
  72. package/tiledeskChatbotPlugs/directives/Directives.js +16 -1
  73. /package/tiledeskChatbotPlugs/directives/{DirOfflineHours.js → DEPRECATED_DirOfflineHours.js} +0 -0
@@ -0,0 +1,374 @@
1
+ const axios = require("axios").default;
2
+ const { TiledeskChatbot } = require("../../models/TiledeskChatbot");
3
+ const { Filler } = require("../Filler");
4
+ let https = require("https");
5
+ const { DirIntent } = require("./DirIntent");
6
+ const { TiledeskChatbotConst } = require("../../models/TiledeskChatbotConst");
7
+ const { TiledeskChatbotUtil } = require("../../models/TiledeskChatbotUtil");
8
+ const req = require("express/lib/request");
9
+ const { update } = require("../../models/faq");
10
+ const { TiledeskClient } = require("@tiledesk/tiledesk-client");
11
+ require('dotenv').config();
12
+
13
+ class DirAddTags {
14
+
15
+ constructor(context) {
16
+ if (!context) {
17
+ throw new Error('context object is mandatory');
18
+ }
19
+ this.context = context;
20
+ this.chatbot = this.context.chatbot;
21
+ this.tdcache = this.context.tdcache;
22
+ this.requestId = this.context.requestId;
23
+ this.API_ENDPOINT = this.context.API_ENDPOINT;
24
+ this.log = context.log;
25
+
26
+ this.tdClient = new TiledeskClient({
27
+ projectId: this.context.projectId,
28
+ token: this.context.token,
29
+ APIURL: this.API_ENDPOINT,
30
+ APIKEY: "___",
31
+ log: this.log
32
+ });
33
+ }
34
+
35
+ execute(directive, callback) {
36
+ if (this.log) { console.log("AddTags directive: ", directive); }
37
+ let action;
38
+ if (directive.action) {
39
+ action = directive.action;
40
+ }
41
+ else {
42
+ console.error("Incorrect directive: ", JSON.stringify(directive));
43
+ callback();
44
+ return;
45
+ }
46
+ this.go(action, (stop) => {
47
+ callback(stop);
48
+ })
49
+ }
50
+
51
+ async go(action, callback) {
52
+ if (this.log) { console.log("(DirAddTags) action:", JSON.stringify(action)); }
53
+ if (!this.tdcache) {
54
+ console.error("Error: (DirAddTags) tdcache is mandatory");
55
+ callback();
56
+ return;
57
+ }
58
+
59
+ // default value
60
+ let target = "request";
61
+ let pushToList = false;
62
+
63
+ target = action.target
64
+ pushToList = action.pushToList
65
+
66
+ if (!action.tags || action.tags === '') {
67
+ console.error("Error: (DirAddTags) tags attribute is mandatory")
68
+ await this.chatbot.addParameter("flowError", "Add tags Error: tags attribute is mandatory");
69
+ callback();
70
+ return;
71
+ }
72
+
73
+ let requestVariables = null;
74
+ requestVariables =
75
+ await TiledeskChatbot.allParametersStatic(
76
+ this.tdcache, this.requestId
77
+ )
78
+
79
+ const filler = new Filler();
80
+ const filled_tags = filler.fill(action.tags, requestVariables);
81
+
82
+ if (this.log) {
83
+ console.log("(DirAddTags) tags: ", filled_tags);
84
+ }
85
+
86
+ // let request = await this.tdClient.getRequestById(this.requestId);
87
+ // if (this.log) { console.log('(DirAddTags) request detail: ', request) }
88
+ // if(!request){
89
+ // if (this.log) { console.log("(DirAddTags) - request not found for request_id:", this.requestId); }
90
+ // callback();
91
+ // return;
92
+ // }
93
+
94
+ /** use case: CONVERSATION */
95
+ if(target === 'request'){
96
+
97
+ let newTags = filled_tags.split(',').filter(tag => tag !== '').map(el => el.trim())
98
+
99
+ if(action.pushToList){
100
+ newTags.forEach(async (tag) => {
101
+ let tags = await this.addNewTag(tag)
102
+ if(!tags){
103
+ callback();
104
+ return;
105
+ }
106
+ })
107
+ }
108
+
109
+ if (this.log) { console.log('(DirAddTags) UPDATE request with newTags', newTags) }
110
+ let updatedRequest = await this.updateRequestWithTags(newTags)
111
+ if(!updatedRequest){
112
+ callback();
113
+ return;
114
+ }
115
+
116
+ }
117
+
118
+ /** use case: LEAD */
119
+ if(target === 'lead'){
120
+ let newTags = filled_tags.split(',').filter(tag => tag !== '').map(el => el.trim())
121
+
122
+ let request = await this.tdClient.getRequestById(this.requestId);
123
+ if (this.log) { console.log('(DirAddTags) request detail: ', request) }
124
+ if(!request){
125
+ if (this.log) { console.log("(DirAddTags) - request not found for request_id:", this.requestId); }
126
+ callback();
127
+ return;
128
+ }
129
+
130
+ if(action.pushToList){
131
+ newTags.forEach(async (tag) => {
132
+ let tags = await this.addNewTag(tag)
133
+ if(!tags){
134
+ callback();
135
+ return;
136
+ }
137
+ })
138
+ }
139
+
140
+ if (this.log) { console.log('(DirAddTags) UPDATE lead with newTags', newTags) }
141
+ let updatedLead = await this.updateLeadWithTags(request.lead._id, newTags)
142
+ if(!updatedLead){
143
+ callback();
144
+ return;
145
+ }
146
+ }
147
+
148
+ callback();
149
+ return;
150
+
151
+
152
+ }
153
+
154
+ async convertToJson(data) {
155
+
156
+ return new Promise((resolve) => {
157
+ let json = null;
158
+ try {
159
+ json = JSON.parse(data);
160
+ resolve(json)
161
+ } catch (err) {
162
+ resolve(data)
163
+ }
164
+ })
165
+
166
+ }
167
+
168
+ async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
169
+ let trueIntentDirective = null;
170
+ if (trueIntent) {
171
+ trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
172
+ }
173
+ let falseIntentDirective = null;
174
+ if (falseIntent) {
175
+ falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
176
+ }
177
+ if (result === true) {
178
+ if (trueIntentDirective) {
179
+ this.intentDir.execute(trueIntentDirective, () => {
180
+ if (callback) {
181
+ callback();
182
+ }
183
+ })
184
+ }
185
+ else {
186
+ if (this.log) { console.log("No trueIntentDirective specified"); }
187
+ if (callback) {
188
+ callback();
189
+ }
190
+ }
191
+ }
192
+ else {
193
+ if (falseIntentDirective) {
194
+ this.intentDir.execute(falseIntentDirective, () => {
195
+ if (callback) {
196
+ callback();
197
+ }
198
+ });
199
+ }
200
+ else {
201
+ if (this.log) { console.log("No falseIntentDirective specified"); }
202
+ if (callback) {
203
+ callback();
204
+ }
205
+ }
206
+ }
207
+ }
208
+
209
+ async #assignAttributes(action, answer) {
210
+ if (this.log) {
211
+ console.log("assignAttributes action:", action)
212
+ console.log("assignAttributes answer:", answer)
213
+ }
214
+ if (this.context.tdcache) {
215
+ if (action.assignReplyTo && answer) {
216
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReplyTo, answer);
217
+ }
218
+ // Debug log
219
+ if (this.log) {
220
+ const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
221
+ for (const [key, value] of Object.entries(all_parameters)) {
222
+ if (this.log) { console.log("(DirAddTags) request parameter:", key, "value:", value, "type:", typeof value) }
223
+ }
224
+ }
225
+ }
226
+ }
227
+
228
+ #myrequest(options, callback) {
229
+ if (this.log) {
230
+ console.log("API URL:", options.url);
231
+ console.log("** Options:", JSON.stringify(options));
232
+ }
233
+ let axios_options = {
234
+ url: options.url,
235
+ method: options.method,
236
+ params: options.params,
237
+ headers: options.headers
238
+ }
239
+ if (options.json !== null) {
240
+ axios_options.data = options.json
241
+ }
242
+ if (this.log) {
243
+ console.log("axios_options:", JSON.stringify(axios_options));
244
+ }
245
+ if (options.url.startsWith("https:")) {
246
+ const httpsAgent = new https.Agent({
247
+ rejectUnauthorized: false,
248
+ });
249
+ axios_options.httpsAgent = httpsAgent;
250
+ }
251
+ axios(axios_options)
252
+ .then((res) => {
253
+ if (this.log) {
254
+ console.log("Response for url:", options.url);
255
+ console.log("Response headers:\n", JSON.stringify(res.headers));
256
+ }
257
+ if (res && res.status == 200 && res.data) {
258
+ if (callback) {
259
+ callback(null, res.data);
260
+ }
261
+ }
262
+ else {
263
+ if (callback) {
264
+ callback(new Error("Response status is not 200"), null);
265
+ }
266
+ }
267
+ })
268
+ .catch((error) => {
269
+ if (callback) {
270
+ callback(error, null);
271
+ }
272
+ });
273
+ }
274
+
275
+ async addNewTag(tag){
276
+ return new Promise((resolve, reject)=> {
277
+ const HTTPREQUEST = {
278
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/tags",
279
+ headers: {
280
+ 'Content-Type': 'application/json',
281
+ 'Authorization': 'JWT ' + this.context.token
282
+ },
283
+ method: "POST",
284
+ json: {
285
+ tag: tag,
286
+ color: '#f0806f'
287
+ }
288
+ }
289
+ this.#myrequest(
290
+ HTTPREQUEST, async (err, resbody) => {
291
+ if (err) {
292
+ console.error("(httprequest) DirAddTags add tags to list err: ", err);
293
+ resolve(true)
294
+ } else {
295
+ if (resbody) {
296
+ resolve(true)
297
+ } else {
298
+ resolve(false)
299
+ }
300
+ }
301
+ }
302
+ )
303
+ })
304
+ }
305
+
306
+
307
+ async updateRequestWithTags(tags) {
308
+ return new Promise((resolve) => {
309
+ let json = []
310
+ let filteredTags = tags.map((tag) => ({tag: tag, color: '#f0806f'}))
311
+ json.push(...filteredTags)
312
+ if (this.log) {
313
+ console.log('(httprequest) DirAddTags updateRequestWithTags tags--> ', json)
314
+ }
315
+ const HTTPREQUEST = {
316
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/requests/" + this.requestId + '/tag',
317
+ headers: {
318
+ 'Content-Type': 'application/json',
319
+ 'Authorization': 'JWT ' + this.context.token
320
+ },
321
+ method: "PUT",
322
+ json: json
323
+ }
324
+
325
+ this.#myrequest(
326
+ HTTPREQUEST, async (err, resbody) => {
327
+ if (err) {
328
+ console.error("(httprequest) DirAddTags patch request with new tags err: ", err);
329
+ resolve(true)
330
+ } else {
331
+ if (resbody) {
332
+ resolve(resbody)
333
+ } else {
334
+ resolve(false)
335
+ }
336
+ }
337
+ }
338
+ )
339
+ })
340
+ }
341
+
342
+ async updateLeadWithTags(lead_id, tags) {
343
+ return new Promise((resolve) => {
344
+ const HTTPREQUEST = {
345
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/leads/" + lead_id + '/tag',
346
+ headers: {
347
+ 'Content-Type': 'application/json',
348
+ 'Authorization': 'JWT ' + this.context.token
349
+ },
350
+ method: "PUT",
351
+ json: tags
352
+ }
353
+
354
+ this.#myrequest(
355
+ HTTPREQUEST, async (err, resbody) => {
356
+ if (err) {
357
+ console.error("(httprequest) DirAddTags put lead with new tags err: ", err);
358
+ resolve(true)
359
+ } else {
360
+ if (resbody) {
361
+ resolve(resbody)
362
+ } else {
363
+ resolve(false)
364
+ }
365
+ }
366
+ }
367
+ )
368
+ })
369
+ }
370
+
371
+
372
+ }
373
+
374
+ module.exports = { DirAddTags }