@tiledesk/tiledesk-tybot-connector 0.2.86 → 0.2.90

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
@@ -5,6 +5,21 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ # v0.2.90
9
+ - Added voice flow attributes: dnis, callId, ani
10
+
11
+ # v0.2.89
12
+ - Added convertToNumber to operations in setAttribute
13
+ - Added setAttributeV2 test
14
+
15
+ # v0.2.88
16
+ - Added DirContactUpdate (aka LeadUpdate). No test case added for LeadUpdate. Only testable with a validation test.
17
+ - Fixed: Attribute parameters userFullname & userEmail now are able to "bypass" the request.lead not correctly updating on the same conversation. If updated in the flow (through LeadUpdate action), they will maintain their own value through the current conversation flow.
18
+
19
+ # v0.2.87
20
+ - Fixed DirAssistant empty error {}
21
+ - Added to DirAssistant "lastMessageData" attribute (The message content original JSON structure useful to get annotations in messages, message type etc.)
22
+
8
23
  # v0.2.86
9
24
  - Added to ifOnlineAgents Action: ignoreProjectWideOperatingHours = action.ignoreOperatingHours;
10
25
  - Updated ifOnlineAgents with get available agents with 'raw' option
@@ -218,6 +218,10 @@ class TiledeskExpression {
218
218
  name: "roundAsNumber",
219
219
  applyPattern: "TiledeskMath.round(Number(#1))"
220
220
  },
221
+ "convertToNumber": {
222
+ name: "convertToNumber",
223
+ applyPattern: "Number(#1)"
224
+ },
221
225
  "JSONparse": {
222
226
  name: "JSONparse",
223
227
  applyPattern: "JSON.parse(String(#1))"
@@ -436,6 +436,9 @@ class TiledeskChatbotUtil {
436
436
  if (fullName.trim() === chatbot_name) {
437
437
  fullName = "bot:" + fullName;
438
438
  }
439
+ else {
440
+ fullName = "user:" + fullName;
441
+ }
439
442
  return "<" + fullName + ">";
440
443
  }
441
444
  }
@@ -594,14 +597,26 @@ class TiledeskChatbotUtil {
594
597
  // await chatbot.addParameter("lastUserDocumentType", null);
595
598
  // }
596
599
  if (message && message.request && message.request.lead) {
597
- console.log("lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)
598
- if (message.request.lead.email) {
599
- await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, message.request.lead.email);
600
+ if (chatbot.log) {console.log("lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)}
601
+ let currentLeadEmail = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY);
602
+ if (chatbot.log) {console.log("You lead email from attributes:", currentLeadEmail, "message.request.lead.email:", message.request.lead.email);}
603
+ if (message.request.lead.email && !currentLeadEmail) {
604
+ // worth saving
605
+ if (chatbot.log) {console.log("worth saving email. lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)}
606
+ try {
607
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, message.request.lead.email);
608
+ }
609
+ catch(error) {
610
+ console.error("Error on setting userEmail:", error);
611
+ }
600
612
  }
601
- if (message.request.lead.fullname && !message.request.lead.fullname.startsWith("guest#")) {
613
+ let currentLeadName = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
614
+ if (chatbot.log) {console.log("You lead name from attributes:", currentLeadName, "message.request.lead.fullname:", message.request.lead.fullname)}
615
+ // if (message.request.lead.fullname && message.request.lead.fullname.startsWith("guest#") && !currentLeadName) {
602
616
  // if (message.request.lead.fullname) {
617
+ if (message.request.lead.fullname && !currentLeadName) {
603
618
  // worth saving
604
- console.log("worth saving. lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)
619
+ if (chatbot.log) {console.log("worth saving fullname. lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)}
605
620
  try {
606
621
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, message.request.lead.fullname);
607
622
  }
@@ -700,7 +715,7 @@ class TiledeskChatbotUtil {
700
715
  const value_type = typeof value;
701
716
  //if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
702
717
  //await chatbot.addParameter(key, String(value));
703
- console.log("Adding from message.attributes:", key, "->", value);
718
+ // console.log("Adding from message.attributes:", key, "->", value);
704
719
  await chatbot.addParameter(key, value);
705
720
  }
706
721
  }
@@ -708,6 +723,17 @@ class TiledeskChatbotUtil {
708
723
  console.error("Error importing message payload in request variables:", err);
709
724
  }
710
725
  }
726
+
727
+ // voice-vxml attributes
728
+ if (message.attributes.dnis) {
729
+ await chatbot.addParameter("dnis", message.attributes.dnis);
730
+ }
731
+ if (message.attributes.callId) {
732
+ await chatbot.addParameter("callId", message.attributes.callId);
733
+ }
734
+ if (message.attributes.ani) {
735
+ await chatbot.addParameter("ani", message.attributes.ani);
736
+ }
711
737
  }
712
738
 
713
739
  const _bot = chatbot.bot; // aka FaqKB
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.86",
3
+ "version": "0.2.90",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -170,6 +170,7 @@ class DirAssistant {
170
170
  // process.exit(0);
171
171
  if (lastMessage !== null) {
172
172
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, assignResultTo, lastMessage);
173
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, "lastMessageData", messages.data[0].content); // content is an array, see on this source end for messages structure example, including content. Ex get annotation[0]: content[0].text.annotations[0]
173
174
  if (trueIntent) {
174
175
  await this.#executeCondition(true, trueIntent, null, falseIntent, null);
175
176
  callback(true);
@@ -624,11 +625,11 @@ class DirAssistant {
624
625
  data = err.response.data;
625
626
  }
626
627
  callback(
627
- null, {
628
+ {
628
629
  status: status,
629
630
  data: data,
630
631
  error: errorMessage
631
- }
632
+ }, data
632
633
  );
633
634
  }
634
635
  });
@@ -642,4 +643,88 @@ class DirAssistant {
642
643
 
643
644
 
644
645
 
645
- module.exports = { DirAssistant };
646
+ module.exports = { DirAssistant };
647
+
648
+ // Messages list response example
649
+
650
+ /*
651
+ {
652
+ "object": "list",
653
+ "data": [
654
+ {
655
+ "id": "msg_FfKaNU82uBYQU9gANFkKJ5Wi",
656
+ "object": "thread.message",
657
+ "created_at": 1721681044,
658
+ "assistant_id": null,
659
+ "thread_id": "thread_fN0rAdyJlPmN9uteMP0yWsCl",
660
+ "run_id": null,
661
+ "role": "user",
662
+ "content": [
663
+ {
664
+ "type": "text",
665
+ "text": {
666
+ "value": "vendete sedie di altezza superiore o uguale a 50 cm?",
667
+ "annotations": []
668
+ }
669
+ }
670
+ ],
671
+ "file_ids": [],
672
+ "metadata": {}
673
+ },
674
+ {
675
+ "id": "msg_Ddxnqi7M9vvLdS9YYO4FHjVt",
676
+ "object": "thread.message",
677
+ "created_at": 1721680934,
678
+ "assistant_id": "asst_qNjiwCVxo3kL2mnN1QyP50Zb",
679
+ "thread_id": "thread_fN0rAdyJlPmN9uteMP0yWsCl",
680
+ "run_id": "run_k8mPIrZPnsO0hAiezD9y2f1t",
681
+ "role": "assistant",
682
+ "content": [
683
+ {
684
+ "type": "text",
685
+ "text": {
686
+ "value": "Una delle best practices raccomandate per garantire un alto livello di sicurezza informatica per la linea di prodotti \"boss\" è la seguente:\n\n- Aggiornare i dispositivi con l'ultima versione del firmware disponibile. È possibile consultare il portale KSA per verificare la disponibilità degli aggiornamenti【6:0†source】.\n\nSe hai altri dubbi o necessiti di ulteriori informazioni, non esitare a chiedere!",
687
+ "annotations": [
688
+ {
689
+ "type": "file_citation",
690
+ "text": "【6:0†source】",
691
+ "start_index": 305,
692
+ "end_index": 317,
693
+ "file_citation": {
694
+ "file_id": "file-dwR6qSwVUIrhImd9espzExGw",
695
+ "quote": ""
696
+ }
697
+ }
698
+ ]
699
+ }
700
+ }
701
+ ],
702
+ "file_ids": [],
703
+ "metadata": {}
704
+ },
705
+ {
706
+ "id": "msg_ng244T4mymroFWZ912r9DvWZ",
707
+ "object": "thread.message",
708
+ "created_at": 1721680931,
709
+ "assistant_id": null,
710
+ "thread_id": "thread_fN0rAdyJlPmN9uteMP0yWsCl",
711
+ "run_id": null,
712
+ "role": "user",
713
+ "content": [
714
+ {
715
+ "type": "text",
716
+ "text": {
717
+ "value": "dimmi una delle best practices che conosci",
718
+ "annotations": []
719
+ }
720
+ }
721
+ ],
722
+ "file_ids": [],
723
+ "metadata": {}
724
+ }
725
+ ],
726
+ "first_id": "msg_FfKaNU82uBYQU9gANFkKJ5Wi",
727
+ "last_id": "msg_ng244T4mymroFWZ912r9DvWZ",
728
+ "has_more": false
729
+ }
730
+ */
@@ -54,8 +54,22 @@ class DirContactUpdate {
54
54
  const filler = new Filler();
55
55
  let updateProperties = {}
56
56
  for (const [key, value] of Object.entries(contactProperties)) {
57
- updateProperties[key] = filler.fill(value, requestAttributes);
58
- if (this.log) {console.log("(DirContactUpdate) updating property:", key, "value:", value); }
57
+ let filled_value = filler.fill(value, requestAttributes);
58
+ if (this.log) {console.log("(DirContactUpdate) setting property key:",key, "with value:", value, "filled value:", filled_value); }
59
+ updateProperties[key] = filled_value;
60
+ // it's important that all the lead's properties are immediatly updated in the current flow invocation so the updated values will be available in the next actions
61
+ if (key === "fullname") {
62
+ await this.context.chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, filled_value);
63
+ if (this.log) {console.log("(DirContactUpdate) updating attribute:",TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, "with property key:", key, "and value:", filled_value); }
64
+ }
65
+ else if ( key === "email") {
66
+ await this.context.chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, filled_value);
67
+ if (this.log) {console.log("(DirContactUpdate) updating attribute:",TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, "with property key:", key, "and value:", filled_value); }
68
+ }
69
+ // else if (key === "phone") {
70
+ // static REQ_USER_PHONE_KEY = "userPhone";
71
+ // }
72
+ if (this.log) {console.log("(DirContactUpdate) updating property:", key, "value:", filled_value); }
59
73
  }
60
74
  const leadId = requestAttributes[TiledeskChatbotConst.REQ_USER_LEAD_ID_KEY];
61
75
  this.tdclient.updateLead(leadId, updateProperties, null, null, () => {
@@ -78,12 +78,12 @@ class DirRandomReply {
78
78
  message.attributes = {}
79
79
  }
80
80
  // Reserved names: userEmail, userFullname
81
- if (requestVariables['userEmail']) {
82
- message.attributes.updateUserEmail = requestVariables['userEmail'];
83
- }
84
- if (requestVariables['userFullname']) {
85
- message.attributes.updateUserFullname = requestVariables['userFullname'];
86
- }
81
+ // if (requestVariables['userEmail']) {
82
+ // message.attributes.updateUserEmail = requestVariables['userEmail'];
83
+ // }
84
+ // if (requestVariables['userFullname']) {
85
+ // message.attributes.updateUserFullname = requestVariables['userFullname'];
86
+ // }
87
87
  }
88
88
  // send!
89
89
  if (this.log) {console.log("Reply:", JSON.stringify(message))};
@@ -94,12 +94,12 @@ class DirReply {
94
94
  message.attributes = {}
95
95
  }
96
96
  // Reserved names: userEmail, userFullname
97
- if (requestAttributes['userEmail']) {
98
- message.attributes.updateUserEmail = requestAttributes['userEmail'];
99
- }
100
- if (requestAttributes['userFullname']) {
101
- message.attributes.updateUserFullname = requestAttributes['userFullname'];
102
- }
97
+ // if (requestAttributes['userEmail']) {
98
+ // message.attributes.updateUserEmail = requestAttributes['userEmail'];
99
+ // }
100
+ // if (requestAttributes['userFullname']) {
101
+ // message.attributes.updateUserFullname = requestAttributes['userFullname'];
102
+ // }
103
103
  // intent_info
104
104
  if (this.context.reply && this.context.reply.attributes && this.context.reply.attributes.intent_info) {
105
105
  message.attributes.intentName = this.context.reply.attributes.intent_info.intent_name;
@@ -234,12 +234,12 @@ class DirReplyV2 {
234
234
  message.attributes = {}
235
235
  }
236
236
  // Reserved names: userEmail, userFullname
237
- if (requestAttributes['userEmail']) {
238
- message.attributes.updateUserEmail = requestAttributes['userEmail'];
239
- }
240
- if (requestAttributes['userFullname']) {
241
- message.attributes.updateUserFullname = requestAttributes['userFullname'];
242
- }
237
+ // if (requestAttributes['userEmail']) {
238
+ // message.attributes.updateUserEmail = requestAttributes['userEmail'];
239
+ // }
240
+ // if (requestAttributes['userFullname']) {
241
+ // message.attributes.updateUserFullname = requestAttributes['userFullname'];
242
+ // }
243
243
  // intent_info
244
244
  if (this.context.reply && this.context.reply.attributes && this.context.reply.attributes.intent_info) {
245
245
  message.attributes.intentName = this.context.reply.attributes.intent_info.intent_name;
@@ -97,10 +97,28 @@ class DirSetAttributeV2 {
97
97
 
98
98
  async go(action, callback) {
99
99
  if (this.log) {console.log("(DirSetAttribute) action before filling:", JSON.stringify(action));}
100
+ if (action && !action.operation) {
101
+ if (this.log) {console.log("(SetAttributeV2) Error operation is mandatory");}
102
+ callback();
103
+ }
100
104
  if (action && action.operation && action.operation.operands) {
101
- if (this.log) {console.log("filling in setattribute...");}
105
+ if (this.log) {console.log("(SetAttributeV2) filling in setattribute...");}
102
106
  await this.fillValues(action.operation.operands);
103
107
  }
108
+ console.log("action.operation.operands.length", action.operation.operands.length);
109
+ console.log("action.operation.operands[0].type", action.operation.operands[0].type);
110
+
111
+ // FUN FACT: THIS TOOK A LOT OF EFFERT BUT IT WAS NEVER USED. YOU CAN SIMPLY CREATE A JSON ATTRIBUTE APPLYING
112
+ // JSONparse FUNCTION TO AN ATTRIBUTE.
113
+ if (action.operation.operands && action.operation.operands.length === 1 && action.operation.operands[0].type === "json") {
114
+ if (this.log) {console.log("(SetAttributeV2) setting json value...");}
115
+ console.log("(SetAttributeV2) setting json value... destination:", action.destination);
116
+ const json_value = JSON.parse(action.operation.operands[0].value);
117
+ console.log("(SetAttributeV2) json_value:", json_value);
118
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.destination, json_value);
119
+ callback();
120
+ return; // on json types no operations are permitted beyond assignment
121
+ }
104
122
  if (this.log) {console.log("filled in setattribute:", action.operation);}
105
123
  // let res = validate(action, schema);
106
124
  // if (res.errors) {
@@ -201,6 +219,44 @@ class DirSetAttributeV2 {
201
219
  console.error("Error while filling operands:", error);
202
220
  }
203
221
  }
222
+
223
+ convertOperandValues(operands) {
224
+ console.log("Converting operands:", operands);
225
+ // operation: {
226
+ // operators: ["addAsNumber", "subtractAsNumber", "divideAsNumber", "multiplyAsNumber"],
227
+ // operands: [
228
+ // {
229
+ // value: "previous",
230
+ // isVariable: true,
231
+ // type: "string"
232
+ // }
233
+ // ]
234
+ try {
235
+ operands.forEach(operand => {
236
+ if (operand.type) {
237
+ console.log("Converting operands - operand.type:", operand.type.toLowerCase());
238
+ if (operand.type.toLowerCase() === "number") {
239
+ console.log("Converting operands - number");
240
+ operand.value = Number(operand.value);
241
+ console.log("new value:", operand.value);
242
+ console.log("new value type:", typeof operand.value);
243
+ }
244
+ else if (operand.type.toLowerCase() === "json") {
245
+ console.log("Converting operands - json, value =", operand.value);
246
+ operand.value = JSON.parse(operand.value);
247
+ console.log("new value:", operand.value);
248
+ console.log("new value type:", typeof operand.value);
249
+ }
250
+ else {
251
+ console.log("Converting operands - ??");
252
+ }
253
+ }
254
+ });
255
+ }
256
+ catch(error) {
257
+ console.error("Error while converting operands:", error);
258
+ }
259
+ }
204
260
  }
205
261
 
206
262
  module.exports = { DirSetAttributeV2 };