@tiledesk/tiledesk-tybot-connector 0.2.139-rc4 → 0.2.139-rc5

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,11 +17,8 @@ available on:
17
17
  - Added flowError on JSONCondition when result = null
18
18
  - Added fix on Filler -->
19
19
 
20
- # v0.2.139-rc3
21
- - Improved ReplaceBotV2 with slug option
22
-
23
- # v0.2.139-rc1
24
- - Improved ReplaceBotV2
20
+ # v0.2.139-rc5
21
+ - Added ReplaceBotV3 action
25
22
 
26
23
  # v0.2.138
27
24
  - Bug-fixed: hiddenMessage not blocked
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.139-rc4",
3
+ "version": "0.2.139-rc5",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -53,6 +53,7 @@ const { DirClearTranscript } = require('./directives/DirClearTranscript');
53
53
  const { DirMoveToUnassigned } = require('./directives/DirMoveToUnassigned');
54
54
  const { DirAddTags } = require('./directives/DirAddTags');
55
55
  const { DirSendWhatsapp } = require('./directives/DirSendWhatsapp');
56
+ const { DirReplaceBotV3 } = require('./directives/DirReplaceBotV3');
56
57
 
57
58
  class DirectivesChatbotPlug {
58
59
 
@@ -489,6 +490,12 @@ class DirectivesChatbotPlug {
489
490
  this.process(next_dir);
490
491
  });
491
492
  }
493
+ else if (directive_name === Directives.REPLACE_BOT_V3) {
494
+ new DirReplaceBotV3(context).execute(directive, async () => {
495
+ let next_dir = await this.nextDirective(this.directives);
496
+ this.process(next_dir);
497
+ });
498
+ }
492
499
  else if (directive_name === Directives.WAIT) {
493
500
  // console.log("........ DirWait");
494
501
  new DirWait(context).execute(directive, async () => {
@@ -2,9 +2,6 @@ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
2
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
3
  const { Filler } = require('../Filler');
4
4
 
5
- const axios = require("axios").default;
6
- let https = require("https");
7
-
8
5
  class DirReplaceBotV2 {
9
6
 
10
7
  constructor(context) {
@@ -55,132 +52,30 @@ class DirReplaceBotV2 {
55
52
  );
56
53
  const filler = new Filler();
57
54
  botName = filler.fill(botName, variables);
58
-
59
- let data = {};
60
- if (action.nameAsSlug && action.nameAsSlug === true) {
61
- data.slug = botName;
62
- } else {
63
- data.name = botName;
64
- }
65
-
66
- const HTTPREQUEST = {
67
- url: this.API_ENDPOINT + "/" + this.context.projectId + "/requests/" + this.requestId + "/replace",
68
- headers: {
69
- 'Content-Type': 'application/json',
70
- 'Authorization': 'JWT ' + this.context.token
71
- },
72
- json: data,
73
- method: 'PUT'
74
- }
75
-
76
- this.#myrequest(
77
- HTTPREQUEST, async (err, resbody) => {
78
- if (err) {
79
- console.log("DirReplaceBot error: ", err);
80
- if (callback) {
81
- callback();
82
- return;
55
+ this.tdClient.replaceBotByName(this.requestId, botName, () => {
56
+ if (blockName) {
57
+ if (this.log) {console.log("Sending hidden /start message to bot in dept");}
58
+ const message = {
59
+ type: "text",
60
+ text: "/" + blockName,
61
+ attributes : {
62
+ subtype: "info"
83
63
  }
84
64
  }
85
-
86
- if (this.log) { console.log("DirReplaceBot replace resbody: ", resbody) };
87
- if (blockName) {
88
- if (this.log) { console.log("Sending hidden /start message to bot in dept"); }
89
- const message = {
90
- type: "text",
91
- text: "/" + blockName,
92
- attributes: {
93
- subtype: "info"
65
+ this.tdClient.sendSupportMessage(
66
+ this.requestId,
67
+ message, (err) => {
68
+ if (err) {
69
+ console.error("Error sending hidden message:", err.message);
94
70
  }
95
- }
96
- this.tdClient.sendSupportMessage(
97
- this.requestId,
98
- message, (err) => {
99
- if (err) {
100
- console.error("Error sending hidden message:", err.message);
101
- }
102
- if (this.log) { console.log("Hidden message sent."); }
103
- callback();
104
- });
105
- }
106
- else {
107
- callback();
108
- }
71
+ if (this.log) {console.log("Hidden message sent.");}
72
+ callback();
73
+ });
109
74
  }
110
- )
111
-
112
- // this.tdClient.replaceBotByName(this.requestId, botName, () => {
113
- // if (blockName) {
114
- // if (this.log) {console.log("Sending hidden /start message to bot in dept");}
115
- // const message = {
116
- // type: "text",
117
- // text: "/" + blockName,
118
- // attributes : {
119
- // subtype: "info"
120
- // }
121
- // }
122
- // this.tdClient.sendSupportMessage(
123
- // this.requestId,
124
- // message, (err) => {
125
- // if (err) {
126
- // console.error("Error sending hidden message:", err.message);
127
- // }
128
- // if (this.log) {console.log("Hidden message sent.");}
129
- // callback();
130
- // });
131
- // }
132
- // else {
133
- // callback();
134
- // }
135
- // });
136
- }
137
-
138
- #myrequest(options, callback) {
139
- if (this.log) {
140
- console.log("API URL:", options.url);
141
- console.log("** Options:", JSON.stringify(options));
142
- }
143
- let axios_options = {
144
- url: options.url,
145
- method: options.method,
146
- params: options.params,
147
- headers: options.headers
148
- }
149
- if (options.json !== null) {
150
- axios_options.data = options.json
151
- }
152
- if (this.log) {
153
- console.log("axios_options:", JSON.stringify(axios_options));
154
- }
155
- if (options.url.startsWith("https:")) {
156
- const httpsAgent = new https.Agent({
157
- rejectUnauthorized: false,
158
- });
159
- axios_options.httpsAgent = httpsAgent;
160
- }
161
- axios(axios_options)
162
- .then((res) => {
163
- if (this.log) {
164
- console.log("Response for url:", options.url);
165
- console.log("Response headers:\n", JSON.stringify(res.headers));
166
- }
167
- if (res && res.status == 200 && res.data) {
168
- if (callback) {
169
- callback(null, res.data);
170
- }
171
- }
172
- else {
173
- if (callback) {
174
- callback(new Error("Response status is not 200"), null);
175
- }
176
- }
177
- })
178
- .catch((error) => {
179
- console.error("(DirAskGPT) Axios error: ", JSON.stringify(error));
180
- if (callback) {
181
- callback(error, null);
182
- }
183
- });
75
+ else {
76
+ callback();
77
+ }
78
+ });
184
79
  }
185
80
  }
186
81
 
@@ -0,0 +1,161 @@
1
+ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
+ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
+ const { Filler } = require('../Filler');
4
+
5
+ const axios = require("axios").default;
6
+
7
+ class DirReplaceBotV3 {
8
+
9
+ constructor(context) {
10
+ if (!context) {
11
+ throw new Error('context object is mandatory.');
12
+ }
13
+ this.context = context;
14
+ this.requestId = context.requestId;
15
+ this.log = context.log;
16
+
17
+ this.API_ENDPOINT = context.API_ENDPOINT;
18
+ this.tdClient = new TiledeskClient({
19
+ projectId: this.context.projectId,
20
+ token: this.context.token,
21
+ APIURL: this.API_ENDPOINT,
22
+ APIKEY: "___",
23
+ log: this.log
24
+ });
25
+ }
26
+
27
+ execute(directive, callback) {
28
+ if (this.log) {console.log("Replacing bot");}
29
+ let action;
30
+ if (directive.action) {
31
+ action = directive.action;
32
+ }
33
+ else {
34
+ console.error("DirReplaceBot Incorrect directive: ", JSON.stringify(directive));
35
+ callback();
36
+ return;
37
+ }
38
+ this.go(action, () => {
39
+ callback();
40
+ })
41
+ }
42
+
43
+ async go(action, callback) {
44
+ let botName = action.botName;
45
+ let botSlug = action.botSlug;
46
+ let useSlug = action.useSlug;
47
+ let blockName = action.blockName;
48
+ let variables = null;
49
+ variables =
50
+ await TiledeskChatbot.allParametersStatic(
51
+ this.context.tdcache, this.context.requestId
52
+ );
53
+ const filler = new Filler();
54
+ botName = filler.fill(botName, variables);
55
+ botSlug = filler.fill(botSlug, variables);
56
+
57
+ let data = {};
58
+ if (useSlug && useSlug === true) {
59
+ data.slug = botSlug;
60
+ } else {
61
+ data.name = botName;
62
+ }
63
+
64
+ const HTTPREQUEST = {
65
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/requests/" + this.requestId + "/replace",
66
+ headers: {
67
+ 'Content-Type': 'application/json',
68
+ 'Authorization': 'JWT ' + this.context.token
69
+ },
70
+ json: data,
71
+ method: 'PUT'
72
+ }
73
+
74
+ this.#myrequest(
75
+ HTTPREQUEST, async (err, resbody) => {
76
+ if (err) {
77
+ console.log("DirReplaceBot error: ", err);
78
+ if (callback) {
79
+ callback();
80
+ return;
81
+ }
82
+ }
83
+
84
+ if (this.log) { console.log("DirReplaceBot replace resbody: ", resbody) };
85
+ if (blockName) {
86
+ if (this.log) { console.log("Sending hidden /start message to bot in dept"); }
87
+ const message = {
88
+ type: "text",
89
+ text: "/" + blockName,
90
+ attributes: {
91
+ subtype: "info"
92
+ }
93
+ }
94
+ this.tdClient.sendSupportMessage(
95
+ this.requestId,
96
+ message, (err) => {
97
+ if (err) {
98
+ console.error("Error sending hidden message:", err.message);
99
+ }
100
+ if (this.log) { console.log("Hidden message sent."); }
101
+ callback();
102
+ });
103
+ }
104
+ else {
105
+ callback();
106
+ }
107
+ }
108
+ )
109
+ }
110
+
111
+ #myrequest(options, callback) {
112
+ if (this.log) {
113
+ console.log("API URL:", options.url);
114
+ console.log("** Options:", JSON.stringify(options));
115
+ }
116
+ let axios_options = {
117
+ url: options.url,
118
+ method: options.method,
119
+ params: options.params,
120
+ headers: options.headers
121
+ }
122
+ if (options.json !== null) {
123
+ axios_options.data = options.json
124
+ }
125
+ if (this.log) {
126
+ console.log("axios_options:", JSON.stringify(axios_options));
127
+ }
128
+ if (options.url.startsWith("https:")) {
129
+ const httpsAgent = new https.Agent({
130
+ rejectUnauthorized: false,
131
+ });
132
+ axios_options.httpsAgent = httpsAgent;
133
+ }
134
+ axios(axios_options)
135
+ .then((res) => {
136
+ if (this.log) {
137
+ console.log("Response for url:", options.url);
138
+ console.log("Response headers:\n", JSON.stringify(res.headers));
139
+ }
140
+ if (res && res.status == 200 && res.data) {
141
+ if (callback) {
142
+ callback(null, res.data);
143
+ }
144
+ }
145
+ else {
146
+ if (callback) {
147
+ callback(new Error("Response status is not 200"), null);
148
+ }
149
+ }
150
+ })
151
+ .catch((error) => {
152
+ console.error("(DirAskGPT) Axios error: ", JSON.stringify(error));
153
+ if (callback) {
154
+ callback(error, null);
155
+ }
156
+ });
157
+ }
158
+
159
+ }
160
+
161
+ module.exports = { DirReplaceBotV3 };
@@ -31,6 +31,7 @@ class Directives {
31
31
  static FORM = "form";
32
32
  static CAPTURE_USER_REPLY = "capture_user_reply";
33
33
  static REPLACE_BOT_V2 = "replacebotv2";
34
+ static REPLACE_BOT_V3 = "replacebotv3";
34
35
  /**** AI ****/
35
36
  static ASK_GPT = "askgpt";
36
37
  static ASK_GPT_V2 = "askgptv2";