@tiledesk/tiledesk-tybot-connector 0.2.108 → 0.2.110

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,8 +17,15 @@ available on:
17
17
  - Added flowError on JSONCondition when result = null
18
18
  - Added fix on Filler -->
19
19
 
20
+ # v0.2.110 -> test
21
+ - Added DirMoveToUnassigned
22
+ - Added DirConnectBlock
23
+
24
+ # v0.2.109 -> test
25
+ - Added DirMoveToUnassigned
26
+ - Fixed bug: GptTask doesn't work properly with trascript
27
+
20
28
  # v0.2.108 -> test
21
- - Added DirUnassign
22
29
  - Added chatbot_id
23
30
  - Added support for citations in AskGPTV2
24
31
  - Added support Json type in GPT Task
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.108",
3
+ "version": "0.2.110",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -706,6 +706,13 @@ class DirectivesChatbotPlug {
706
706
  this.process(next_dir);
707
707
  });
708
708
  }
709
+ else if (directive_name === Directives.CONNECT_BLOCK) {
710
+ // console.log(".....DirIntent")
711
+ new DirConnectBlock(context).execute(directive, async () => {
712
+ let next_dir = await this.nextDirective(this.directives);
713
+ this.process(next_dir);
714
+ });
715
+ }
709
716
  else {
710
717
  //console.log("Unhandled Post-message Directive:", directive_name);
711
718
  let next_dir = await this.nextDirective(this.directives);
@@ -0,0 +1,202 @@
1
+ //const { ExtApi } = require('../../ExtApi.js');
2
+ let axios = require('axios');
3
+ let https = require("https");
4
+ const { v4: uuidv4 } = require('uuid');
5
+ const ms = require('minimist-string');
6
+
7
+ class DirConnectBlock {
8
+ constructor(context) {
9
+ if (!context) {
10
+ throw new Error('context object is mandatory.');
11
+ }
12
+ this.context = context;
13
+ this.API_ENDPOINT = context.TILEDESK_APIURL,
14
+ this.TILEBOT_ENDPOINT = context.TILEBOT_ENDPOINT;
15
+ this.supportRequest = context.supportRequest;
16
+ this.token = context.token;
17
+ this.log = context.log;
18
+ // let context = {
19
+ // projectId: projectId,
20
+ // token: token,
21
+ // supportRequest: supportRequest,
22
+ // requestId: supportRequest.request_id,
23
+ // TILEDESK_APIURL: API_URL,
24
+ // TILEBOT_ENDPOINT: TILEBOT_ENDPOINT,
25
+ // departmentId: depId,
26
+ // tdcache: tdcache,
27
+ // tdclient: tdclient,
28
+ // log: true
29
+ // }
30
+ }
31
+
32
+ execute(directive, callback) {
33
+ let action;
34
+ if (directive.action) {
35
+ action = directive.action;
36
+ }
37
+ else {
38
+ console.error("Incorrect DirConnectBlock directive:", directive);
39
+ callback();
40
+ return;
41
+ }
42
+ this.go(action, () => {
43
+ callback();
44
+ });
45
+ }
46
+
47
+ go(action, callback) {
48
+ const intentName = action.intentName;
49
+ const projectId = this.supportRequest.id_project;
50
+ const requestId = this.supportRequest.request_id;
51
+ const botId = this.supportRequest.bot_id;
52
+ let intent_command;
53
+ if (intentName) {
54
+ intent_command = "/" + intentName;
55
+ }
56
+ else {
57
+ callback();
58
+ return;
59
+ }
60
+
61
+ let intent_command_request = {
62
+ "payload": {
63
+ "_id": uuidv4(),
64
+ "senderFullname": "_tdinternal",
65
+ "type": "text",
66
+ "sender": "_tdinternal",
67
+ "recipient": requestId,
68
+ "text": intent_command,
69
+ "id_project": projectId,
70
+ "request": {
71
+ "request_id": requestId,
72
+ "id_project": projectId
73
+ }
74
+ },
75
+ "token": this.token
76
+ }
77
+ if (this.log) {console.log("move to intent message:", intent_command_request);}
78
+ let TILEBOT_ENDPOINT;
79
+ if (this.TILEBOT_ENDPOINT) {
80
+ TILEBOT_ENDPOINT = this.TILEBOT_ENDPOINT;
81
+ }
82
+ else {
83
+ TILEBOT_ENDPOINT = `${this.API_ENDPOINT}/modules/tilebot`
84
+ }
85
+ this.sendMessageToBot(TILEBOT_ENDPOINT, intent_command_request, botId, () => {
86
+ callback();
87
+ });
88
+ }
89
+
90
+ static intentDirectiveFor(intent, json_params) {
91
+ let string_params = null;
92
+ if (json_params) {
93
+ try {
94
+ string_params = JSON.stringify(json_params);
95
+ }
96
+ catch (error) {
97
+ console.error("Error stringigying JSON PARAMS", json_params);
98
+ }
99
+ }
100
+ if (string_params != null) {
101
+ intent += string_params
102
+ }
103
+ let intentDirective = {
104
+ action: {
105
+ intentName: intent
106
+ }
107
+ }
108
+ return intentDirective;
109
+ }
110
+
111
+ static fullIntentDirectiveFor(intent, json_params) {
112
+ let string_params = JSON.stringify(params);
113
+ let intentDirective = {
114
+ action: {
115
+ intentName: intent
116
+ }
117
+ }
118
+ return intentDirective;
119
+ }
120
+
121
+ /**
122
+ * A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
123
+ * /${TILEBOT_ROUTE}/ext/${botId}
124
+ *
125
+ * @param {Object} message. The message to send
126
+ * @param {string} botId. Tiledesk botId
127
+ * @param {string} token. User token
128
+ */
129
+ sendMessageToBot(CHATBOT_ENDPOINT, message, botId, callback) {
130
+ // const jwt_token = this.fixToken(token);
131
+ const url = `${CHATBOT_ENDPOINT}/ext/${botId}`;
132
+ // console.log("sendMessageToBot URL", url);
133
+ const HTTPREQUEST = {
134
+ url: url,
135
+ headers: {
136
+ 'Content-Type' : 'application/json'
137
+ },
138
+ json: message,
139
+ method: 'POST'
140
+ };
141
+ this.myrequest(
142
+ HTTPREQUEST,
143
+ function(err, resbody) {
144
+ if (err) {
145
+ if (callback) {
146
+ callback(err);
147
+ }
148
+ }
149
+ else {
150
+ if (callback) {
151
+ callback(null, resbody);
152
+ }
153
+ }
154
+ }, false
155
+ );
156
+ }
157
+
158
+ myrequest(options, callback, log) {
159
+ if (this.log) {
160
+ console.log("API URL:", options.url);
161
+ console.log("** Options:", JSON.stringify(options));
162
+ }
163
+ let axios_options = {
164
+ url: options.url,
165
+ method: options.method,
166
+ data: options.json,
167
+ params: options.params,
168
+ headers: options.headers
169
+ }
170
+ if (options.url.startsWith("https:")) {
171
+ const httpsAgent = new https.Agent({
172
+ rejectUnauthorized: false,
173
+ });
174
+ axios_options.httpsAgent = httpsAgent;
175
+ }
176
+ axios(axios_options)
177
+ .then((res) => {
178
+ if (this.log) {
179
+ console.log("Response for url:", options.url);
180
+ console.log("Response headers:\n", JSON.stringify(res.headers));
181
+ }
182
+ if (res && res.status == 200 && res.data) {
183
+ if (callback) {
184
+ callback(null, res.data);
185
+ }
186
+ }
187
+ else {
188
+ if (callback) {
189
+ callback(TiledeskClient.getErr({message: "Response status not 200"}, options, res), null, null);
190
+ }
191
+ }
192
+ })
193
+ .catch( (error) => {
194
+ console.error("An error occurred:", error);
195
+ if (callback) {
196
+ callback(error, null, null);
197
+ }
198
+ });
199
+ }
200
+ }
201
+
202
+ module.exports = { DirIntent };
@@ -171,6 +171,7 @@ class DirGptTask {
171
171
  json.messages.push(message)
172
172
  }
173
173
  })
174
+ json.messages.push({ role: "user", content: filled_question });
174
175
  } else {
175
176
  let message = { role: "user", content: filled_question };
176
177
  json.messages.push(message);
@@ -52,6 +52,7 @@ class Directives {
52
52
  static CONTACT_UPDATE = "leadupdate";
53
53
  static CLEAR_TRANSCRIPT = "clear_transcript";
54
54
  static MOVE_TO_UNASSIGNED = "move_to_unassigned";
55
+ static CONNECT_BLOCK = "connect_block";
55
56
 
56
57
  // static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
57
58
  // static WHEN_OFFLINE_HOURS = "whenofflinehours"; // DEPRECATED // adds a message on top of the original message when offline hours opts: --replace