@tiledesk/tiledesk-tybot-connector 0.2.130 → 0.2.131-rc2

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.
package/CHANGELOG.md CHANGED
@@ -17,6 +17,13 @@ available on:
17
17
  - Added flowError on JSONCondition when result = null
18
18
  - Added fix on Filler -->
19
19
 
20
+ # v0.2.131-rc2
21
+ - changed API_URL with API_ENDPOINT
22
+
23
+ # v0.2.131-rc1
24
+ - added: AUDIO_RECORD vxml action as reply_v2 actionType
25
+ - added: DirAddTags action
26
+
20
27
  # v0.2.130
21
28
  - (TiledeskChatbotUtil) fix process.env.API_ENDPOINT => process.env.API_URL
22
29
  - index.js /block => added "token": "NO-TOKEN"
package/index.js CHANGED
@@ -30,7 +30,7 @@ const { DirectivesChatbotPlug } = require('./tiledeskChatbotPlugs/DirectivesChat
30
30
  let mongoose = require('mongoose');
31
31
  // const { Directives } = require('./tiledeskChatbotPlugs/directives/Directives.js');
32
32
  const { TiledeskChatbotUtil } = require('./models/TiledeskChatbotUtil.js'); //require('@tiledesk/tiledesk-chatbot-util');
33
- let APIURL = null;
33
+ let API_ENDPOINT = null;
34
34
  let staticBots;
35
35
 
36
36
  router.post('/ext/:botid', async (req, res) => {
@@ -160,7 +160,7 @@ router.post('/ext/:botid', async (req, res) => {
160
160
  botId: botId,
161
161
  bot: bot,
162
162
  token: token,
163
- APIURL: APIURL,
163
+ APIURL: API_ENDPOINT,
164
164
  APIKEY: "___",
165
165
  tdcache: tdcache,
166
166
  requestId: requestId,
@@ -206,7 +206,7 @@ router.post('/ext/:botid', async (req, res) => {
206
206
  directives: directives,
207
207
  chatbot: chatbot,
208
208
  supportRequest: message.request,
209
- TILEDESK_API_ENDPOINT: APIURL,
209
+ API_ENDPOINT: API_ENDPOINT,
210
210
  TILEBOT_ENDPOINT:process.env.TYBOT_ENDPOINT,
211
211
  token: token,
212
212
  log: log,
@@ -232,7 +232,7 @@ router.post('/ext/:botid', async (req, res) => {
232
232
  reply.attributes.splits = true;
233
233
  reply.attributes.markbot = true;
234
234
  reply.attributes.fillParams = true;
235
- let extEndpoint = `${APIURL}/modules/tilebot/`;
235
+ let extEndpoint = `${API_ENDPOINT}/modules/tilebot/`;
236
236
  if (process.env.TYBOT_ENDPOINT) {
237
237
  extEndpoint = `${process.env.TYBOT_ENDPOINT}`;
238
238
  }
@@ -264,7 +264,7 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
264
264
  const tdclient = new TiledeskClient({
265
265
  projectId: projectId,
266
266
  token: token,
267
- APIURL: APIURL,
267
+ APIURL: API_ENDPOINT,
268
268
  APIKEY: "___",
269
269
  log: false
270
270
  });
@@ -309,10 +309,10 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
309
309
  }
310
310
  if (log) {
311
311
  console.log("/ext request....", JSON.stringify(request));
312
- console.log("/ext APIURL....", APIURL);
312
+ console.log("/ext API_ENDPOINT....", API_ENDPOINT);
313
313
  console.log("/ext process.env.TYBOT_ENDPOINT....", process.env.TYBOT_ENDPOINT);
314
314
  }
315
- 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});
315
+ let directivesPlug = new DirectivesChatbotPlug({supportRequest: request, API_ENDPOINT: API_ENDPOINT, TILEBOT_ENDPOINT:process.env.TYBOT_ENDPOINT, token: token, log: log, HELP_CENTER_API_ENDPOINT: process.env.HELP_CENTER_API_ENDPOINT, cache: tdcache});
316
316
  // let directivesPlug = null;
317
317
  // PIPELINE-EXT
318
318
  // if (log) {console.log("answer to process:", JSON.stringify(answer));}
@@ -542,7 +542,7 @@ router.post('/echobot', (req, res) => {
542
542
  const tdclient = new TiledeskClient({
543
543
  projectId: projectId,
544
544
  token: token,
545
- APIURL: APIURL,
545
+ APIURL: API_ENDPOINT,
546
546
  APIKEY: "___",
547
547
  log: false
548
548
  });
@@ -619,8 +619,8 @@ async function startApp(settings, completionCallback) {
619
619
  throw new Error("settings.API_ENDPOINT is mandatory id no settings.bots.");
620
620
  }
621
621
  else {
622
- APIURL = settings.API_ENDPOINT;
623
- console.log("(Tilebot) settings.API_ENDPOINT:", APIURL);
622
+ API_ENDPOINT = settings.API_ENDPOINT;
623
+ console.log("(Tilebot) settings.API_ENDPOINT:", API_ENDPOINT);
624
624
  }
625
625
 
626
626
  if (settings.REDIS_HOST && settings.REDIS_PORT) {
@@ -730,7 +730,7 @@ async function checkRequest(request_id, id_project) {
730
730
  function sendMessageToBot(TILEBOT_ENDPOINT, message, botId, callback) {
731
731
  // const jwt_token = this.fixToken(token);
732
732
  if (!TILEBOT_ENDPOINT) {
733
- TILEBOT_ENDPOINT = `${APIURL}/modules/tilebot`
733
+ TILEBOT_ENDPOINT = `${API_ENDPOINT}/modules/tilebot`
734
734
  }
735
735
  const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
736
736
  console.log("sendMessageToBot URL", url);
@@ -517,10 +517,13 @@ class TiledeskChatbotUtil {
517
517
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
518
518
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
519
519
  }
520
- if (process.env.TILEDESK_API) {
521
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
522
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
523
- }
520
+ /**
521
+ * RefactoringCheck: can this be deleted?
522
+ */
523
+ // if (process.env.TILEDESK_API) {
524
+ // await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
525
+ // await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
526
+ // }
524
527
 
525
528
  if (process.env.API_URL) {
526
529
  await chatbot.addParameter(TiledeskChatbotConst.API_BASE_URL, process.env.API_URL);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.130",
3
+ "version": "0.2.131-rc2",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -52,7 +52,8 @@ const { DirIfOnlineAgentsV2 } = require('./directives/DirIfOnlineAgentsV2');
52
52
  const { DirContactUpdate } = require('./directives/DirContactUpdate');
53
53
  const { DirClearTranscript } = require('./directives/DirClearTranscript');
54
54
  const { DirMoveToUnassigned } = require('./directives/DirMoveToUnassigned');
55
- const { DirWhatsappStatic, DirSendWhatsapp } = require('./directives/DirSendWhatsapp');
55
+ const { DirAddTags } = require('./directives/DirAddTags');
56
+ const { DirSendWhatsapp } = require('./directives/DirSendWhatsapp');
56
57
 
57
58
  class DirectivesChatbotPlug {
58
59
 
@@ -64,7 +65,7 @@ class DirectivesChatbotPlug {
64
65
 
65
66
  constructor(config) {
66
67
  this.supportRequest = config.supportRequest;
67
- this.API_URL = config.TILEDESK_API_ENDPOINT;
68
+ this.API_ENDPOINT = config.API_ENDPOINT;
68
69
  this.TILEBOT_ENDPOINT = config.TILEBOT_ENDPOINT;
69
70
  this.token = config.token;
70
71
  this.log = config.log;
@@ -124,7 +125,7 @@ class DirectivesChatbotPlug {
124
125
  // console.log("supportRequest is:", JSON.stringify(supportRequest))
125
126
 
126
127
  const token = this.token;
127
- const API_URL = this.API_URL;
128
+ const API_ENDPOINT = this.API_ENDPOINT;
128
129
  const TILEBOT_ENDPOINT = this.TILEBOT_ENDPOINT;
129
130
 
130
131
  // const requestId = supportRequest.request_id
@@ -141,7 +142,7 @@ class DirectivesChatbotPlug {
141
142
  tdclient = new TiledeskClient({
142
143
  projectId: projectId,
143
144
  token: token,
144
- APIURL: API_URL,
145
+ APIURL: API_ENDPOINT,
145
146
  APIKEY: "___",
146
147
  log: this.log
147
148
  });
@@ -158,7 +159,7 @@ class DirectivesChatbotPlug {
158
159
  supportRequest: supportRequest,
159
160
  reply: this.reply,
160
161
  requestId: supportRequest.request_id,
161
- TILEDESK_APIURL: API_URL,
162
+ API_ENDPOINT: API_ENDPOINT,
162
163
  TILEBOT_ENDPOINT: TILEBOT_ENDPOINT,
163
164
  departmentId: depId,
164
165
  tdcache: tdcache,
@@ -348,6 +349,13 @@ class DirectivesChatbotPlug {
348
349
  this.process(next_dir);
349
350
  });
350
351
  }
352
+ else if (directive_name === Directives.AUDIO_RECORD) {
353
+ // console.log("...DirReply");
354
+ new DirReply(context).execute(directive, async () => {
355
+ let next_dir = await this.nextDirective(this.directives);
356
+ this.process(next_dir);
357
+ });
358
+ }
351
359
  else if (directive_name === Directives.RANDOM_REPLY) {
352
360
  // console.log("...DirRandomReply");
353
361
  new DirRandomReply(context).execute(directive, async () => {
@@ -712,14 +720,12 @@ class DirectivesChatbotPlug {
712
720
  });
713
721
  }
714
722
  else if (directive_name === Directives.CLEAR_TRANSCRIPT) {
715
- console.log("...CLEAR_TRANSCRIPT");
716
723
  new DirClearTranscript(context).execute(directive, async () => {
717
724
  let next_dir = await this.nextDirective(this.directives);
718
725
  this.process(next_dir);
719
726
  });
720
727
  }
721
728
  else if (directive_name === Directives.MOVE_TO_UNASSIGNED) {
722
- console.log("...MOVE_TO_UNASSIGNED");
723
729
  new DirMoveToUnassigned(context).execute(directive, async () => {
724
730
  let next_dir = await this.nextDirective(this.directives);
725
731
  this.process(next_dir);
@@ -732,6 +738,20 @@ class DirectivesChatbotPlug {
732
738
  this.process(next_dir);
733
739
  });
734
740
  }
741
+ else if (directive_name === Directives.ADD_TAGS) {
742
+ // console.log(".....DirAddTags")
743
+ new DirAddTags(context).execute(directive, async (stop) => {
744
+ if (context.log) { console.log("GPTTask stop?", stop);}
745
+ if (stop == true) {
746
+ if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
747
+ this.theend();
748
+ }
749
+ else {
750
+ let next_dir = await this.nextDirective(this.directives);
751
+ this.process(next_dir);
752
+ }
753
+ });
754
+ }
735
755
  else {
736
756
  //console.log("Unhandled Post-message Directive:", directive_name);
737
757
  let next_dir = await this.nextDirective(this.directives);
@@ -748,7 +768,7 @@ class DirectivesChatbotPlug {
748
768
  }
749
769
  const supportRequest = this.supportRequest;
750
770
  const token = this.token;
751
- const API_URL = this.API_URL;
771
+ const API_ENDPOINT = this.API_ENDPOINT;
752
772
  // const requestId = supportRequest.request_id
753
773
  // let depId;
754
774
  // if (supportRequest.department && supportRequest.department._id) {
@@ -758,7 +778,7 @@ class DirectivesChatbotPlug {
758
778
  const tdclient = new TiledeskClient({
759
779
  projectId: projectId,
760
780
  token: token,
761
- APIURL: API_URL,
781
+ APIURL: API_ENDPOINT,
762
782
  APIKEY: "___",
763
783
  log: false
764
784
  });
@@ -0,0 +1,369 @@
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 { rejects } = require("assert");
10
+ const { update } = require("../../models/faq");
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.tdClient = this.context.tdclient;
24
+ this.API_ENDPOINT = this.context.API_ENDPOINT;
25
+ // this.intentDir = new DirIntent(context);
26
+ this.log = context.log;
27
+ // this.log = true;
28
+ }
29
+
30
+ execute(directive, callback) {
31
+ if (this.log) { console.log("AddTags directive: ", directive); }
32
+ let action;
33
+ if (directive.action) {
34
+ action = directive.action;
35
+ }
36
+ else {
37
+ console.error("Incorrect directive: ", JSON.stringify(directive));
38
+ callback();
39
+ return;
40
+ }
41
+ this.go(action, (stop) => {
42
+ callback(stop);
43
+ })
44
+ }
45
+
46
+ async go(action, callback) {
47
+ if (this.log) { console.log("(DirAddTags) action:", JSON.stringify(action)); }
48
+ if (!this.tdcache) {
49
+ console.error("Error: (DirAddTags) tdcache is mandatory");
50
+ callback();
51
+ return;
52
+ }
53
+
54
+ // default value
55
+ let target = "request";
56
+ let pushToList = false;
57
+
58
+ target = action.target
59
+ pushToList = action.pushToList
60
+
61
+ if (!action.tags || action.tags === '') {
62
+ console.error("Error: (DirAddTags) tags attribute is mandatory")
63
+ await this.chatbot.addParameter("flowError", "Add tags Error: tags attribute is mandatory");
64
+ callback();
65
+ return;
66
+ }
67
+
68
+ let requestVariables = null;
69
+ requestVariables =
70
+ await TiledeskChatbot.allParametersStatic(
71
+ this.tdcache, this.requestId
72
+ )
73
+
74
+ const filler = new Filler();
75
+ const filled_tags = filler.fill(action.tags, requestVariables);
76
+
77
+ if (this.log) {
78
+ console.log("(DirAddTags) tags: ", filled_tags);
79
+ }
80
+
81
+ // let request = await this.tdClient.getRequestById(this.requestId);
82
+ // if (this.log) { console.log('(DirAddTags) request detail: ', request) }
83
+ // if(!request){
84
+ // if (this.log) { console.log("(DirAddTags) - request not found for request_id:", this.requestId); }
85
+ // callback();
86
+ // return;
87
+ // }
88
+
89
+ /** use case: CONVERSATION */
90
+ if(target === 'request'){
91
+
92
+ let newTags = filled_tags.split(',').filter(tag => tag !== '').map(el => el.trim())
93
+
94
+ if(action.pushToList){
95
+ newTags.forEach(async (tag) => {
96
+ let tags = await this.addNewTag(tag)
97
+ if(!tags){
98
+ callback();
99
+ return;
100
+ }
101
+ })
102
+ }
103
+
104
+ if (this.log) { console.log('(DirAddTags) UPDATE request with newTags', newTags) }
105
+ let updatedRequest = await this.updateRequestWithTags(newTags)
106
+ if(!updatedRequest){
107
+ callback();
108
+ return;
109
+ }
110
+
111
+ }
112
+
113
+ /** use case: LEAD */
114
+ if(target === 'lead'){
115
+ let newTags = filled_tags.split(',').filter(tag => tag !== '').map(el => el.trim())
116
+
117
+ let request = await this.tdClient.getRequestById(this.requestId);
118
+ if (this.log) { console.log('(DirAddTags) request detail: ', request) }
119
+ if(!request){
120
+ if (this.log) { console.log("(DirAddTags) - request not found for request_id:", this.requestId); }
121
+ callback();
122
+ return;
123
+ }
124
+
125
+ if(action.pushToList){
126
+ newTags.forEach(async (tag) => {
127
+ let tags = await this.addNewTag(tag)
128
+ if(!tags){
129
+ callback();
130
+ return;
131
+ }
132
+ })
133
+ }
134
+
135
+ if (this.log) { console.log('(DirAddTags) UPDATE lead with newTags', newTags) }
136
+ let updatedLead = await this.updateLeadWithTags(request.lead._id, newTags)
137
+ if(!updatedLead){
138
+ callback();
139
+ return;
140
+ }
141
+ }
142
+
143
+ callback();
144
+ return;
145
+
146
+
147
+ }
148
+
149
+ async convertToJson(data) {
150
+
151
+ return new Promise((resolve) => {
152
+ let json = null;
153
+ try {
154
+ json = JSON.parse(data);
155
+ resolve(json)
156
+ } catch (err) {
157
+ resolve(data)
158
+ }
159
+ })
160
+
161
+ }
162
+
163
+ async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
164
+ let trueIntentDirective = null;
165
+ if (trueIntent) {
166
+ trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
167
+ }
168
+ let falseIntentDirective = null;
169
+ if (falseIntent) {
170
+ falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
171
+ }
172
+ if (result === true) {
173
+ if (trueIntentDirective) {
174
+ this.intentDir.execute(trueIntentDirective, () => {
175
+ if (callback) {
176
+ callback();
177
+ }
178
+ })
179
+ }
180
+ else {
181
+ if (this.log) { console.log("No trueIntentDirective specified"); }
182
+ if (callback) {
183
+ callback();
184
+ }
185
+ }
186
+ }
187
+ else {
188
+ if (falseIntentDirective) {
189
+ this.intentDir.execute(falseIntentDirective, () => {
190
+ if (callback) {
191
+ callback();
192
+ }
193
+ });
194
+ }
195
+ else {
196
+ if (this.log) { console.log("No falseIntentDirective specified"); }
197
+ if (callback) {
198
+ callback();
199
+ }
200
+ }
201
+ }
202
+ }
203
+
204
+ async #assignAttributes(action, answer) {
205
+ if (this.log) {
206
+ console.log("assignAttributes action:", action)
207
+ console.log("assignAttributes answer:", answer)
208
+ }
209
+ if (this.context.tdcache) {
210
+ if (action.assignReplyTo && answer) {
211
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReplyTo, answer);
212
+ }
213
+ // Debug log
214
+ if (this.log) {
215
+ const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
216
+ for (const [key, value] of Object.entries(all_parameters)) {
217
+ if (this.log) { console.log("(DirAddTags) request parameter:", key, "value:", value, "type:", typeof value) }
218
+ }
219
+ }
220
+ }
221
+ }
222
+
223
+ #myrequest(options, callback) {
224
+ if (this.log) {
225
+ console.log("API URL:", options.url);
226
+ console.log("** Options:", JSON.stringify(options));
227
+ }
228
+ let axios_options = {
229
+ url: options.url,
230
+ method: options.method,
231
+ params: options.params,
232
+ headers: options.headers
233
+ }
234
+ if (options.json !== null) {
235
+ axios_options.data = options.json
236
+ }
237
+ if (this.log) {
238
+ console.log("axios_options:", JSON.stringify(axios_options));
239
+ }
240
+ if (options.url.startsWith("https:")) {
241
+ const httpsAgent = new https.Agent({
242
+ rejectUnauthorized: false,
243
+ });
244
+ axios_options.httpsAgent = httpsAgent;
245
+ }
246
+ axios(axios_options)
247
+ .then((res) => {
248
+ if (this.log) {
249
+ console.log("Response for url:", options.url);
250
+ console.log("Response headers:\n", JSON.stringify(res.headers));
251
+ }
252
+ if (res && res.status == 200 && res.data) {
253
+ if (callback) {
254
+ callback(null, res.data);
255
+ }
256
+ }
257
+ else {
258
+ if (callback) {
259
+ callback(new Error("Response status is not 200"), null);
260
+ }
261
+ }
262
+ })
263
+ .catch((error) => {
264
+ if (callback) {
265
+ callback(error, null);
266
+ }
267
+ });
268
+ }
269
+
270
+ async addNewTag(tag){
271
+ return new Promise((resolve, rejects)=> {
272
+ const HTTPREQUEST = {
273
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/tags",
274
+ headers: {
275
+ 'Content-Type': 'application/json',
276
+ 'Authorization': 'JWT ' + this.context.token
277
+ },
278
+ method: "POST",
279
+ json: {
280
+ tag: tag,
281
+ color: '#f0806f'
282
+ }
283
+ }
284
+ this.#myrequest(
285
+ HTTPREQUEST, async (err, resbody) => {
286
+ if (err) {
287
+ console.error("(httprequest) DirAddTags add tags to list err: ", err);
288
+ resolve(true)
289
+ } else {
290
+ if (resbody) {
291
+ resolve(true)
292
+ } else {
293
+ resolve(false)
294
+ }
295
+ }
296
+ }
297
+ )
298
+ })
299
+ }
300
+
301
+
302
+ async updateRequestWithTags(tags) {
303
+ return new Promise((resolve) => {
304
+ let json = []
305
+ let filteredTags = tags.map((tag) => ({tag: tag, color: '#f0806f'}))
306
+ json.push(...filteredTags)
307
+ if (this.log) {
308
+ console.log('(httprequest) DirAddTags updateRequestWithTags tags--> ', json)
309
+ }
310
+ const HTTPREQUEST = {
311
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/requests/" + this.requestId + '/tag',
312
+ headers: {
313
+ 'Content-Type': 'application/json',
314
+ 'Authorization': 'JWT ' + this.context.token
315
+ },
316
+ method: "PUT",
317
+ json: json
318
+ }
319
+
320
+ this.#myrequest(
321
+ HTTPREQUEST, async (err, resbody) => {
322
+ if (err) {
323
+ console.error("(httprequest) DirAddTags patch request with new tags err: ", err);
324
+ resolve(true)
325
+ } else {
326
+ if (resbody) {
327
+ resolve(resbody)
328
+ } else {
329
+ resolve(false)
330
+ }
331
+ }
332
+ }
333
+ )
334
+ })
335
+ }
336
+
337
+ async updateLeadWithTags(lead_id, tags) {
338
+ return new Promise((resolve) => {
339
+ const HTTPREQUEST = {
340
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/leads/" + lead_id + '/tag',
341
+ headers: {
342
+ 'Content-Type': 'application/json',
343
+ 'Authorization': 'JWT ' + this.context.token
344
+ },
345
+ method: "PUT",
346
+ json: tags
347
+ }
348
+
349
+ this.#myrequest(
350
+ HTTPREQUEST, async (err, resbody) => {
351
+ if (err) {
352
+ console.error("(httprequest) DirAddTags put lead with new tags err: ", err);
353
+ resolve(true)
354
+ } else {
355
+ if (resbody) {
356
+ resolve(resbody)
357
+ } else {
358
+ resolve(false)
359
+ }
360
+ }
361
+ }
362
+ )
363
+ })
364
+ }
365
+
366
+
367
+ }
368
+
369
+ module.exports = { DirAddTags }
@@ -15,6 +15,7 @@ class DirAskGPT {
15
15
  this.tdcache = this.context.tdcache;
16
16
  this.requestId = this.context.requestId;
17
17
  this.intentDir = new DirIntent(context);
18
+ this.API_ENDPOINT = this.context.API_ENDPOINT;
18
19
  this.log = context.log;
19
20
  }
20
21
 
@@ -88,17 +89,13 @@ class DirAskGPT {
88
89
  const filler = new Filler();
89
90
  const filled_question = filler.fill(action.question, requestVariables);
90
91
 
91
- const server_base_url = process.env.API_ENDPOINT || process.env.API_URL;
92
92
  const kb_endpoint = process.env.KB_ENDPOINT;
93
- if (this.log) {
94
- console.log("DirAskGPT ApiEndpoint URL: ", server_base_url);
95
- console.log("DirAskGPT KbEndpoint URL: ", kb_endpoint);
96
- }
93
+ if (this.log) { console.log("DirAskGPT KbEndpoint URL: ", kb_endpoint); }
97
94
 
98
- let key = await this.getKeyFromIntegrations(server_base_url);
95
+ let key = await this.getKeyFromIntegrations();
99
96
  if (!key) {
100
97
  if (this.log) { console.log("DirAskGPT - Key not found in Integrations. Searching in kb settings..."); }
101
- key = await this.getKeyFromKbSettings(server_base_url);
98
+ key = await this.getKeyFromKbSettings();
102
99
  }
103
100
 
104
101
  if (!key) {
@@ -120,7 +117,7 @@ class DirAskGPT {
120
117
  }
121
118
 
122
119
  if (publicKey === true) {
123
- let keep_going = await this.checkQuoteAvailability(server_base_url);
120
+ let keep_going = await this.checkQuoteAvailability();
124
121
  if (keep_going === false) {
125
122
  if (this.log) { console.log("DirAskGPT - Quota exceeded for tokens. Skip the action")}
126
123
  callback();
@@ -167,7 +164,7 @@ class DirAskGPT {
167
164
 
168
165
  // if (publicKey === true) {
169
166
  // let token_usage = resbody.usage.total_tokens;
170
- // this.updateQuote(server_base_url, token_usage);
167
+ // this.updateQuote(token_usage);
171
168
  // }
172
169
 
173
170
  if (trueIntent) {
@@ -305,11 +302,11 @@ class DirAskGPT {
305
302
  });
306
303
  }
307
304
 
308
- async getKeyFromIntegrations(server_base_url) {
305
+ async getKeyFromIntegrations() {
309
306
  return new Promise((resolve) => {
310
307
 
311
308
  const INTEGRATIONS_HTTPREQUEST = {
312
- url: server_base_url + "/" + this.context.projectId + "/integration/name/openai",
309
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/openai",
313
310
  headers: {
314
311
  'Content-Type': 'application/json',
315
312
  'Authorization': 'JWT ' + this.context.token
@@ -337,11 +334,11 @@ class DirAskGPT {
337
334
  })
338
335
  }
339
336
 
340
- async getKeyFromKbSettings(server_base_url) {
337
+ async getKeyFromKbSettings() {
341
338
  return new Promise((resolve) => {
342
339
 
343
340
  const KB_HTTPREQUEST = {
344
- url: server_base_url + "/" + this.context.projectId + "/kbsettings",
341
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/kbsettings",
345
342
  headers: {
346
343
  'Content-Type': 'application/json',
347
344
  'Authorization': 'JWT ' + this.context.token