@tiledesk/tiledesk-tybot-connector 0.2.23 → 0.2.25
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 +8 -0
- package/index.js +5 -0
- package/models/TiledeskChatbot.js +11 -0
- package/models/TiledeskChatbotUtil.js +11 -1
- package/package.json +1 -1
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +7 -0
- package/tiledeskChatbotPlugs/directives/DirGptTask.js +5 -2
- package/tiledeskChatbotPlugs/directives/DirQapla.js +191 -0
- package/tiledeskChatbotPlugs/directives/Directives.js +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,14 @@
|
|
|
5
5
|
available on:
|
|
6
6
|
▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
|
|
7
7
|
|
|
8
|
+
### v0.2.25
|
|
9
|
+
- added support for native connect to block
|
|
10
|
+
- added TiledeskChatbotUtil.addConnectAction()
|
|
11
|
+
|
|
12
|
+
### v0.2.24
|
|
13
|
+
- Added Qapla action
|
|
14
|
+
- Fixed bug: the 3.5 turbo model was always used in GptTask action
|
|
15
|
+
|
|
8
16
|
### v0.2.23
|
|
9
17
|
- Fixed CaptureUserReply. Added Check for missing goToIntent
|
|
10
18
|
- When channelName == "chat21" it will be renamed in "web"
|
package/index.js
CHANGED
|
@@ -147,6 +147,11 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
147
147
|
projectId: projectId,
|
|
148
148
|
log: log
|
|
149
149
|
});
|
|
150
|
+
if (log) {console.log("MESSAGE CONTAINS:", message.text);}
|
|
151
|
+
// if (message.text === "\\\\start") { // patch for the misleading \\start training phrase
|
|
152
|
+
// if (log) {console.log("forced conversion of \\\\start /start");}
|
|
153
|
+
// message.text = "/start";
|
|
154
|
+
// }
|
|
150
155
|
await TiledeskChatbotUtil.updateRequestAttributes(chatbot, message, projectId, requestId);
|
|
151
156
|
await TiledeskChatbotUtil.updateConversationTranscript(chatbot, message);
|
|
152
157
|
|
|
@@ -477,6 +477,17 @@ class TiledeskChatbot {
|
|
|
477
477
|
};
|
|
478
478
|
|
|
479
479
|
let static_bot_answer;
|
|
480
|
+
if (answerObj.actions) {
|
|
481
|
+
const actions_length = answerObj.actions.length;
|
|
482
|
+
TiledeskChatbotUtil.addConnectAction(answerObj);
|
|
483
|
+
if (this.log) {
|
|
484
|
+
const new_actions_length = answerObj.actions.length;
|
|
485
|
+
if (new_actions_length > actions_length) {
|
|
486
|
+
console.log("Added connect to block action. All actions now:", JSON.stringify(answerObj.actions));
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
480
491
|
if (answerObj.actions && answerObj.actions.length > 0) {
|
|
481
492
|
static_bot_answer = { // actions workflow will be executed
|
|
482
493
|
actions: answerObj.actions
|
|
@@ -527,7 +527,17 @@ class TiledeskChatbotUtil {
|
|
|
527
527
|
});
|
|
528
528
|
}
|
|
529
529
|
return directives;
|
|
530
|
-
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
static addConnectAction(reply) {
|
|
533
|
+
// console.log("reply foraddConnectAction:", reply);
|
|
534
|
+
if (reply && reply.attributes && reply.attributes.nextBlockAction) {
|
|
535
|
+
if (reply.actions) {
|
|
536
|
+
reply.actions.push(reply.attributes.nextBlockAction);
|
|
537
|
+
// console.log("actions are:", reply.actions)
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
531
541
|
|
|
532
542
|
}
|
|
533
543
|
|
package/package.json
CHANGED
|
@@ -30,6 +30,7 @@ const { DirWebRequestV2 } = require('./directives/DirWebRequestV2');
|
|
|
30
30
|
const { DirCode } = require('./directives/DirCode');
|
|
31
31
|
const { DirWhatsappByAttribute } = require('./directives/DirWhatsappByAttribute');
|
|
32
32
|
const { DirAskGPT } = require('./directives/DirAskGPT');
|
|
33
|
+
const { DirQapla } = require('./directives/DirQapla');
|
|
33
34
|
|
|
34
35
|
const { TiledeskChatbot } = require('../models/TiledeskChatbot');
|
|
35
36
|
const { DirIfOnlineAgents } = require('./directives/DirIfOnlineAgents');
|
|
@@ -594,6 +595,12 @@ class DirectivesChatbotPlug {
|
|
|
594
595
|
this.process(next_dir);
|
|
595
596
|
});
|
|
596
597
|
}
|
|
598
|
+
else if (directive_name === Directives.QAPLA) {
|
|
599
|
+
new DirQapla(context).execute(directive, async () => {
|
|
600
|
+
let next_dir = await this.nextDirective(this.directives);
|
|
601
|
+
this.process(next_dir);
|
|
602
|
+
})
|
|
603
|
+
}
|
|
597
604
|
else {
|
|
598
605
|
//console.log("Unhandled Post-message Directive:", directive_name);
|
|
599
606
|
let next_dir = await this.nextDirective(this.directives);
|
|
@@ -21,6 +21,7 @@ class DirGptTask {
|
|
|
21
21
|
let action;
|
|
22
22
|
if (directive.action) {
|
|
23
23
|
action = directive.action;
|
|
24
|
+
console.log("-----> GPT TASK ACTION: \n", action);
|
|
24
25
|
}
|
|
25
26
|
else {
|
|
26
27
|
console.error("Incorrect directive: ", JSON.stringify(directive));
|
|
@@ -34,6 +35,8 @@ class DirGptTask {
|
|
|
34
35
|
|
|
35
36
|
async go(action, callback) {
|
|
36
37
|
if (this.log) { console.log("GptTask action:", JSON.stringify(action)); }
|
|
38
|
+
console.log("GptTask action:", JSON.stringify(action));
|
|
39
|
+
console.log("GptTask action model:", action.model);
|
|
37
40
|
if (!this.tdcache) {
|
|
38
41
|
console.error("Error: DirGptTask tdcache is mandatory");
|
|
39
42
|
callback();
|
|
@@ -94,7 +97,7 @@ class DirGptTask {
|
|
|
94
97
|
}
|
|
95
98
|
} else if (callback) {
|
|
96
99
|
if (this.log) {
|
|
97
|
-
console.log("Get KnowledgeBase resbody:", resbody);
|
|
100
|
+
console.log("Get KnowledgeBase settings resbody:", resbody);
|
|
98
101
|
console.log("gptkey: ", resbody.gptkey);
|
|
99
102
|
}
|
|
100
103
|
|
|
@@ -106,7 +109,7 @@ class DirGptTask {
|
|
|
106
109
|
} else {
|
|
107
110
|
|
|
108
111
|
let json = {
|
|
109
|
-
"model":
|
|
112
|
+
"model": action.model,
|
|
110
113
|
"messages": [
|
|
111
114
|
{
|
|
112
115
|
"role": "user",
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
const axios = require("axios").default;
|
|
2
|
+
const { TiledeskChatbot } = require("../../models/TiledeskChatbot");
|
|
3
|
+
const { Filler } = require("../Filler");
|
|
4
|
+
let https = require("https");
|
|
5
|
+
require('dotenv').config();
|
|
6
|
+
|
|
7
|
+
class DirQapla {
|
|
8
|
+
|
|
9
|
+
constructor(context) {
|
|
10
|
+
if (!context) {
|
|
11
|
+
throw new Error('context object is mandatory');
|
|
12
|
+
}
|
|
13
|
+
this.context = context;
|
|
14
|
+
this.tdcache = this.context.tdcache;
|
|
15
|
+
this.requestId = this.context.requestId;
|
|
16
|
+
this.log = context.log;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
execute(directive, callback) {
|
|
20
|
+
if (this.log) { console.log("DirQapla directive: ", directive); }
|
|
21
|
+
let action;
|
|
22
|
+
if (directive.action) {
|
|
23
|
+
action = directive.action;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
console.error("DirQapla Incorrect directive: ", JSON.stringify(directive));
|
|
27
|
+
callback();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
this.go(action, () => {
|
|
31
|
+
callback();
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async go(action, callback) {
|
|
36
|
+
if (this.log) { console.log("DirQapla action:", JSON.stringify(action)); }
|
|
37
|
+
if (!this.tdcache) {
|
|
38
|
+
console.error("Error: DirQapla tdcache is mandatory");
|
|
39
|
+
callback();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let requestVariables = null;
|
|
44
|
+
requestVariables =
|
|
45
|
+
await TiledeskChatbot.allParametersStatic(
|
|
46
|
+
this.tdcache, this.requestId
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
if (this.log) {
|
|
50
|
+
const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
|
|
51
|
+
for (const [key, value] of Object.entries(all_parameters)) {
|
|
52
|
+
if (this.log) { console.log("DirQapla request parameter:", key, "value:", value, "type:", typeof value) }
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let tracking_number = await this.context.chatbot.getParameter(action.trackingNumber);
|
|
57
|
+
if (this.log) {console.log("DirQapla tracking number: ", tracking_number); }
|
|
58
|
+
|
|
59
|
+
if (!tracking_number) {
|
|
60
|
+
console.error("DirQapla ERROR - tracking number is undefined or null");
|
|
61
|
+
callback();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const qapla_base_url = process.env.QAPLA_ENDPOINT || "https://api.qapla.it/1.2"
|
|
65
|
+
// https://api.qapla.it/1.2/getShipment/?apiKey=3b9839c954168e861f2b63d79920ec3a3ff92aab674de9f3930df60b8a40c495&trackingNumber=123456
|
|
66
|
+
if (this.log) { console.log("DirQapla QaplaEndpoint URL: ", qapla_base_url); }
|
|
67
|
+
const QAPLA_HTTPREQUEST = {
|
|
68
|
+
url: qapla_base_url + "/getShipment/",
|
|
69
|
+
headers: {
|
|
70
|
+
'Content-Type': 'application/json'
|
|
71
|
+
},
|
|
72
|
+
params: {
|
|
73
|
+
apiKey: action.apiKey,
|
|
74
|
+
trackingNumber: tracking_number
|
|
75
|
+
},
|
|
76
|
+
method: "GET"
|
|
77
|
+
}
|
|
78
|
+
if (this.log) { console.log("DirQapla QAPLA_HTTPREQUEST", QAPLA_HTTPREQUEST); }
|
|
79
|
+
|
|
80
|
+
this.#myrequest(
|
|
81
|
+
QAPLA_HTTPREQUEST, async (err, resbody) => {
|
|
82
|
+
if (err) {
|
|
83
|
+
if (callback) {
|
|
84
|
+
console.error("(httprequest) DirQapla getShipment err:", err);
|
|
85
|
+
callback();
|
|
86
|
+
}
|
|
87
|
+
} else if (callback) {
|
|
88
|
+
if (this.log) { console.log("DirQapla getShipment resbody: ", resbody); }
|
|
89
|
+
|
|
90
|
+
let status = null;;
|
|
91
|
+
let result;
|
|
92
|
+
let error;
|
|
93
|
+
|
|
94
|
+
if (resbody.getShipment &&
|
|
95
|
+
resbody.getShipment.shipments &&
|
|
96
|
+
resbody.getShipment.shipments[0] &&
|
|
97
|
+
resbody.getShipment.shipments[0].status &&
|
|
98
|
+
resbody.getShipment.shipments[0].status.qaplaStatus &&
|
|
99
|
+
resbody.getShipment.shipments[0].status.qaplaStatus.status) {
|
|
100
|
+
status = resbody.getShipment.shipments[0].status.qaplaStatus.status;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
result = resbody.getShipment.result;
|
|
104
|
+
error = resbody.getShipment.error;
|
|
105
|
+
await this.#assignAttributes(action, status, result, error);
|
|
106
|
+
callback();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
async #assignAttributes(action, status, result, error) {
|
|
115
|
+
if (this.log) {
|
|
116
|
+
console.log("DirQapla assignAttributes action:", action)
|
|
117
|
+
console.log("DirQapla assignAttributes status:", status)
|
|
118
|
+
console.log("DirQapla assignAttributes result:", result)
|
|
119
|
+
console.log("DirQapla assignAttributes error:", error)
|
|
120
|
+
}
|
|
121
|
+
if (this.context.tdcache) {
|
|
122
|
+
if (action.assignStatusTo) {
|
|
123
|
+
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignStatusTo, status);
|
|
124
|
+
}
|
|
125
|
+
if (action.assignResultTo && result) {
|
|
126
|
+
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignResultTo, result);
|
|
127
|
+
}
|
|
128
|
+
if (action.assignErrorTo) {
|
|
129
|
+
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignErrorTo, error);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Debug log
|
|
133
|
+
if (this.log) {
|
|
134
|
+
const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
|
|
135
|
+
for (const [key, value] of Object.entries(all_parameters)) {
|
|
136
|
+
if (this.log) { console.log("DirQapla request parameter:", key, "value:", value, "type:", typeof value) }
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
#myrequest(options, callback) {
|
|
143
|
+
if (this.log) {
|
|
144
|
+
console.log("API URL:", options.url);
|
|
145
|
+
console.log("** Options:", JSON.stringify(options));
|
|
146
|
+
}
|
|
147
|
+
let axios_options = {
|
|
148
|
+
url: options.url,
|
|
149
|
+
method: options.method,
|
|
150
|
+
params: options.params,
|
|
151
|
+
headers: options.headers
|
|
152
|
+
}
|
|
153
|
+
if (options.json !== null) {
|
|
154
|
+
axios_options.data = options.json
|
|
155
|
+
}
|
|
156
|
+
if (this.log) {
|
|
157
|
+
console.log("axios_options:", JSON.stringify(axios_options));
|
|
158
|
+
}
|
|
159
|
+
if (options.url.startsWith("https:")) {
|
|
160
|
+
const httpsAgent = new https.Agent({
|
|
161
|
+
rejectUnauthorized: false,
|
|
162
|
+
});
|
|
163
|
+
axios_options.httpsAgent = httpsAgent;
|
|
164
|
+
}
|
|
165
|
+
axios(axios_options)
|
|
166
|
+
.then((res) => {
|
|
167
|
+
if (this.log) {
|
|
168
|
+
console.log("Response for url:", options.url);
|
|
169
|
+
console.log("Response headers:\n", JSON.stringify(res.headers));
|
|
170
|
+
}
|
|
171
|
+
if (res && res.status == 200 && res.data) {
|
|
172
|
+
if (callback) {
|
|
173
|
+
callback(null, res.data);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
if (callback) {
|
|
178
|
+
callback(new Error("Response status is not 200"), null);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
})
|
|
182
|
+
.catch((error) => {
|
|
183
|
+
// console.error("An error occurred:", JSON.stringify(error.data));
|
|
184
|
+
if (callback) {
|
|
185
|
+
callback(error, null);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
module.exports = { DirQapla }
|
|
@@ -30,6 +30,7 @@ class Directives {
|
|
|
30
30
|
static GPT_TASK = "gpt_task";
|
|
31
31
|
static FORM = "form";
|
|
32
32
|
static CAPTURE_USER_REPLY = "capture_user_reply";
|
|
33
|
+
static QAPLA = 'qapla';
|
|
33
34
|
|
|
34
35
|
// static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
|
|
35
36
|
// static WHEN_OFFLINE_HOURS = "whenofflinehours"; // DEPRECATED // adds a message on top of the original message when offline hours opts: --replace
|