@tiledesk/tiledesk-tybot-connector 0.1.89 → 0.1.91

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,10 +5,19 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ ### 0.1.91
9
+ - Added control for attribute_value null in DirWhatsappByAttribute
10
+
11
+ ### 0.1.90
12
+ - Added support for JSON/Number/string parameters in /intent[] invocation
13
+ - Added support for JSON/Number/string parameters in attributes.payload invocation
14
+ - Added DirWhatsappByAttribute
15
+ - Test external process.env (PASSED)
16
+
8
17
  ### 0.1.89
9
18
  - DirWebRequest: fixed sending data: 'null'
10
19
  - Improved MockBotsDataSource.js to support search also by intent_id
11
- - Added test for qapla WebRequest
20
+ - Added test for Qapla WebRequest
12
21
 
13
22
  ### 0.1.88
14
23
  - Added language parse with accept-language-parser. Now user_language has just the first language ISO code
package/index.js CHANGED
@@ -299,7 +299,8 @@ async function updateRequestVariables(chatbot, message, projectId, requestId) {
299
299
  // const value = all_parameters[key];
300
300
  const value_type = typeof value;
301
301
  //if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
302
- await chatbot.addParameter(key, String(value));
302
+ //await chatbot.addParameter(key, String(value));
303
+ await chatbot.addParameter(key, value);
303
304
  }
304
305
  }
305
306
  catch(err) {
@@ -181,9 +181,25 @@ class TiledeskChatbot {
181
181
  console.log("Got a reply (faq) by Intent name:", JSON.stringify(faq));}
182
182
  try {
183
183
  if (intent.parameters) {
184
+ if (this.log) {
185
+ for (const [key, value] of Object.entries(intent.parameters)) {
186
+ console.log(`* Attribute from intent: '${key}' => ${value}`);
187
+ }
188
+ }
184
189
  for (const [key, value] of Object.entries(intent.parameters)) {
185
- if (this.log) {console.log(`Adding parameter from intent => ${key}: ${value}`);}
186
- this.addParameter(key, value);
190
+ if (this.log) {console.log(`Adding attribute from intent invocation /intentName{} => ${key}: ${value}`);}
191
+ // const parameter_typeof_key = "_tdTypeOf:" + key;
192
+ // console.log("intent[parameter_typeof_key]",parameter_typeof_key, intent.parameters[parameter_typeof_key]);
193
+ // if (intent.parameters[parameter_typeof_key] === "object") {
194
+ // const parameter_value_string = JSON.stringify(value)
195
+ // console.log("Adding parameter as string:", parameter_value_string)
196
+ this.addParameter(key, value);
197
+ //this.addParameter(parameter_typeof_key, intent[parameter_typeof_key]);
198
+ // }
199
+ // else { // TODO: support the other data types
200
+ // this.addParameter(key, value);
201
+ // }
202
+
187
203
  }
188
204
  }
189
205
  reply = await this.execIntent(faq, message, lead);
@@ -504,19 +520,56 @@ class TiledeskChatbot {
504
520
  }
505
521
 
506
522
  async addParameter(parameter_name, parameter_value) {
507
- //await this.tdcache.hset("tilebot:requests:" + requestId + ":parameters", parameter_name, parameter_value);
508
523
  await TiledeskChatbot.addParameterStatic(this.tdcache, this.requestId, parameter_name, parameter_value);
509
524
  }
510
525
 
511
526
  async deleteParameter(parameter_name) {
512
- //await this.tdcache.hset("tilebot:requests:" + requestId + ":parameters", parameter_name, parameter_value);
513
527
  await TiledeskChatbot.deleteParameterStatic(this.tdcache, this.requestId, parameter_name);
514
528
  }
515
529
 
516
530
  static async addParameterStatic(_tdcache, requestId, parameter_name, parameter_value) {
517
- // const parameter_key = "tilebot:requests:" + requestId + ":parameters";
518
531
  const parameter_key = TiledeskChatbot.requestCacheKey(requestId) + ":parameters";
519
- await _tdcache.hset(parameter_key, parameter_name, parameter_value);
532
+ const parameter_value_s = JSON.stringify(parameter_value);
533
+ await _tdcache.hset(parameter_key, parameter_name, parameter_value_s);
534
+ }
535
+
536
+ async allParameters() {
537
+ return await TiledeskChatbot.allParametersStatic(this.tdcache, this.requestId);
538
+ }
539
+
540
+ static async allParametersStatic(_tdcache, requestId) {
541
+ // const parameters_key = "tilebot:requests:" + requestId + ":parameters";
542
+ const attributes__as_string_map = await _tdcache.hgetall(
543
+ TiledeskChatbot.requestCacheKey(requestId) + ":parameters");
544
+ let attributes_native_values = {};
545
+ for (const [key, value] of Object.entries(attributes__as_string_map)) {
546
+ attributes_native_values[key] = JSON.parse(value);
547
+ }
548
+ return attributes_native_values;
549
+ }
550
+
551
+ async allParametersInstance(_tdcache, requestId) {
552
+ // const parameters_key = "tilebot:requests:" + requestId + ":parameters";
553
+ return await _tdcache.hgetall(
554
+ TiledeskChatbot.requestCacheKey(requestId) + ":parameters");
555
+ }
556
+
557
+ static async getParameterStatic(_tdcache, requestId, key) {
558
+ // const parameters_key = "tilebot:requests:" + requestId + ":parameters";
559
+ let value = await _tdcache.hget(
560
+ TiledeskChatbot.requestCacheKey(requestId) + ":parameters", key);
561
+ try {
562
+ value = JSON.parse(value);
563
+ }
564
+ catch(error) {
565
+ console.log("Error parsing to JSON an Attribute:", error);
566
+ }
567
+ return value;
568
+ }
569
+
570
+ static async deleteParameterStatic(_tdcache, requestId, paramName) {
571
+ return await _tdcache.hdel(
572
+ TiledeskChatbot.requestCacheKey(requestId) + ":parameters", paramName);
520
573
  }
521
574
 
522
575
  static async checkStep(_tdcache, requestId, max_steps, log) {
@@ -573,33 +626,6 @@ class TiledeskChatbot {
573
626
  return await _tdcache.get(parameter_key);
574
627
  }
575
628
 
576
- async allParameters() {
577
- return await TiledeskChatbot.allParametersStatic(this.tdcache, this.requestId);
578
- }
579
-
580
- static async allParametersStatic(_tdcache, requestId) {
581
- // const parameters_key = "tilebot:requests:" + requestId + ":parameters";
582
- return await _tdcache.hgetall(
583
- TiledeskChatbot.requestCacheKey(requestId) + ":parameters");
584
- }
585
-
586
- async allParametersInstance(_tdcache, requestId) {
587
- // const parameters_key = "tilebot:requests:" + requestId + ":parameters";
588
- return await _tdcache.hgetall(
589
- TiledeskChatbot.requestCacheKey(requestId) + ":parameters");
590
- }
591
-
592
- static async getParameterStatic(_tdcache, requestId, key) {
593
- // const parameters_key = "tilebot:requests:" + requestId + ":parameters";
594
- return await _tdcache.hget(
595
- TiledeskChatbot.requestCacheKey(requestId) + ":parameters", key);
596
- }
597
-
598
- static async deleteParameterStatic(_tdcache, requestId, paramName) {
599
- return await _tdcache.hdel(
600
- TiledeskChatbot.requestCacheKey(requestId) + ":parameters", paramName);
601
- }
602
-
603
629
  static requestCacheKey(requestId) {
604
630
  const request_key = "tilebot:requests:" + requestId;
605
631
  return request_key;
@@ -23,13 +23,34 @@ class TiledeskChatbotUtil {
23
23
  // console.log("json_string (params)", json_string);
24
24
  try {
25
25
  intent.parameters = JSON.parse(json_string);
26
+ // if (intent.parameters) {
27
+ // for (const [key, value] of Object.entries(intent.parameters)) {
28
+ // console.log("Checking type of:", key, value);
29
+ // if (typeof value === "object") {
30
+ // console.log("Checking type of is object:", key);
31
+ // intent.parameters["_tdTypeOf:" + key] = "object";
32
+ // }
33
+ // else if (typeof value === "string") {
34
+ // console.log("Checking type of is string:", key);
35
+ // intent.parameters["_tdTypeOf:" + key] = "string";
36
+ // }
37
+ // else if (typeof value === "number") {
38
+ // console.log("Checking type of is number:", key);
39
+ // intent.parameters["_tdTypeOf:" + key] = "number";
40
+ // }
41
+ // else if (typeof value === "boolean") {
42
+ // console.log("Checking type of is boolean:", key);
43
+ // intent.parameters["_tdTypeOf:" + key] = "boolean";
44
+ // }
45
+ // }
46
+ // }
47
+
26
48
  }
27
49
  catch (err) {
28
50
  console.log("error on intent.parameters = JSON.parse(json_string)", err);
29
- }
51
+ }
30
52
  }
31
53
  return intent;
32
-
33
54
  }
34
55
 
35
56
  static chooseRandomReply(message) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.1.89",
3
+ "version": "0.1.91",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -27,6 +27,7 @@ const { DirAssign } = require('./directives/DirAssign');
27
27
  const { DirSetAttribute } = require('./directives/DirSetAttribute');
28
28
  const { DirWebRequest } = require('./directives/DirWebRequest');
29
29
  const { DirCode } = require('./directives/DirCode');
30
+ const { DirWhatsappByAttribute } = require('./directives/DirWhatsappByAttribute');
30
31
 
31
32
  const { TiledeskChatbot } = require('../models/TiledeskChatbot');
32
33
  const { DirIfOnlineAgents } = require('./directives/DirIfOnlineAgents');
@@ -501,6 +502,12 @@ class DirectivesChatbotPlug {
501
502
  }
502
503
  });
503
504
  }
505
+ else if (directive_name === Directives.WHATSAPP_ATTRIBUTE) {
506
+ new DirWhatsappByAttribute(context).execute(directive, async (stop) => {
507
+ let next_dir = await this.nextDirective(this.directives);
508
+ this.process(next_dir);
509
+ });
510
+ }
504
511
  else {
505
512
  //console.log("Unhandled Post-message Directive:", directive_name);
506
513
  let next_dir = await this.nextDirective(this.directives);
@@ -66,10 +66,11 @@ class DirCode {
66
66
  // console.log("script_context.tiledeskVars:", script_context.tiledeskVars);
67
67
  for (const [key, value] of Object.entries(script_context.tiledeskVars.ops.set)) {
68
68
  // await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, key, value);
69
- await variablesManager.set(key, value);
69
+ // await variablesManager.set(key, value);
70
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, key, value);
70
71
  }
71
72
  // if (this.log) {
72
- let newvars_set = await variablesManager.all();
73
+ // let newvars_set = await variablesManager.all();
73
74
  // console.log("newvars_set:", newvars_set);
74
75
  // }
75
76
  for (const [key, value] of Object.entries(script_context.tiledeskVars.ops.del)) {
@@ -75,6 +75,7 @@ class DirReply {
75
75
  if (this.log) {console.log("message before filters:", JSON.stringify(message));}
76
76
  if (message.attributes && message.attributes.commands) {
77
77
  if (this.log) {console.log("filterOnVariables...on commands", JSON.stringify(message.attributes.commands));}
78
+ if (this.log) {console.log("filterOnVariables...on attributes", requestAttributes);}
78
79
  // TiledeskChatbotUtil.filterOnVariables(message.attributes.commands, requestAttributes);
79
80
  TiledeskChatbotUtil.filterOnVariables(message, requestAttributes);
80
81
  }
@@ -0,0 +1,123 @@
1
+ const axios = require("axios").default;
2
+ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
+
4
+ // const whatsapp_api_url = "https://tiledesk-whatsapp-app-pre.giovannitroisi3.repl.co/ext"
5
+ let whatsapp_api_url;
6
+
7
+ class DirWhatsappByAttribute {
8
+
9
+ constructor(context) {
10
+ if (!context) {
11
+ throw new Error('context object is mandatory');
12
+ }
13
+ this.context = context;
14
+ this.log = context.log;
15
+ }
16
+
17
+ execute(directive, callback) {
18
+ console.log("\n\nwhatsapp by attribtues action (execute) - directive: ", directive);
19
+ let action;
20
+ if (directive.action) {
21
+ action = directive.action;
22
+ }
23
+ else {
24
+ console.error("Incorrect directive: ", JSON.stringify(directive));
25
+ callback();
26
+ return;
27
+ }
28
+ this.go(action, () => {
29
+ callback();
30
+ })
31
+ }
32
+
33
+ async go(action, callback) {
34
+ if (this.log) {
35
+ console.log("whatsapp by attributes action: ", JSON.stringify(action))
36
+ }
37
+
38
+ if (process.env.API_URL) {
39
+ whatsapp_api_url = process.env.API_URL + "/modules/whatsapp";
40
+ console.log("(Tilebot) DirWhatsappByAttribute whatsapp_api_url: ", whatsapp_api_url);
41
+ } else {
42
+ console.error("(Tilebot) ERROR Missing whatsapp_api_url. Unable to use action WhatsApp By Attributes");
43
+ callback();
44
+ return;
45
+ }
46
+
47
+ if (action.attributeName) {
48
+ if (this.log) { console.log("whatsapp attributeName:", action.attributeName); }
49
+ let attribute_value = null;
50
+ if (this.context.tdcache) {
51
+
52
+ const attribute_value = await TiledeskChatbot.getParameterStatic(this.context.tdcache, this.context.requestId, action.attributeName)
53
+ if (this.log) { console.log("attribute_value:", JSON.stringify(attribute_value)); }
54
+
55
+ if (attribute_value == null) {
56
+ console.error("(Tilebot) attribute_value is undefined");
57
+ callback();
58
+ return;
59
+ }
60
+
61
+ const URL = whatsapp_api_url + '/tiledesk/broadcast';
62
+ const HTTPREQUEST = {
63
+ url: URL,
64
+ headers: {
65
+ 'Content-Type': 'application/json'
66
+ },
67
+ json: attribute_value,
68
+ method: 'POST'
69
+ };
70
+ let promise = new Promise((resolve, reject) => {
71
+ DirWhatsappByAttribute.myrequest(
72
+ HTTPREQUEST,
73
+ function (err, resbody) {
74
+ if (err) {
75
+ if (callback) {
76
+ callback(err);
77
+ }
78
+ reject(err);
79
+ }
80
+ else {
81
+ if (callback) {
82
+ callback(null, resbody);
83
+ }
84
+ console.log("(tybot) broadcast sent: ", resbody);
85
+ resolve(resbody);
86
+ }
87
+ }, true);
88
+ })
89
+ return promise;
90
+ }
91
+ }
92
+ }
93
+
94
+ // HTTP REQUEST
95
+ static async myrequest(options, callback, log) {
96
+ console.log("my request execution")
97
+ return await axios({
98
+ url: options.url,
99
+ method: options.method,
100
+ data: options.json,
101
+ params: options.params,
102
+ headers: options.headers
103
+ }).then((res) => {
104
+ if (res && res.status == 200 && res.data) {
105
+ if (callback) {
106
+ callback(null, res.data);
107
+ }
108
+ }
109
+ else {
110
+ if (callback) {
111
+ callback(TiledeskClient.getErr({ message: "Response status not 200" }, options, res), null, null);
112
+ }
113
+ }
114
+ }).catch((err) => {
115
+ console.error("(tybot request) An error occured: ", err);
116
+ if (callback) {
117
+ callback(err, null, null);
118
+ }
119
+ })
120
+ }
121
+ }
122
+
123
+ module.exports = { DirWhatsappByAttribute }
@@ -24,6 +24,7 @@ class Directives {
24
24
  static REPLY = 'reply';
25
25
  static RANDOM_REPLY = 'randomreply';
26
26
  static CODE = 'code';
27
+ static WHATSAPP_ATTRIBUTE = 'whatsapp_attribute';
27
28
 
28
29
  // static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
29
30
  // static WHEN_OFFLINE_HOURS = "whenofflinehours"; // DEPRECATED // adds a message on top of the original message when offline hours opts: --replace