@tiledesk/tiledesk-tybot-connector 0.2.119 → 0.2.121

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,13 @@ available on:
17
17
  - Added flowError on JSONCondition when result = null
18
18
  - Added fix on Filler -->
19
19
 
20
- # v0.2.120 -> test
20
+ # v0.2.121 -> prod
21
+ - added check if (!this.validWebhookURL(this.webhookurl)) {... on WebhookChatbotPlug.js
22
+
23
+ # v0.2.120 -> prod
21
24
  - removed check in go() - it is already in execute() - on SetAttributeV2: if (!action) {...}
25
+ - merged Whatsapp Action
26
+ - updated "@tiledesk/tiledesk-client": "^0.10.13"
22
27
 
23
28
  # v0.2.119 -> test
24
29
  - fixed: addParameter("payload", message) => addParameter("payload", message.attributes.payload)
package/index.js CHANGED
@@ -569,8 +569,8 @@ router.post('/block/:project_id/:bot_id/:block_id', async (req, res) => {
569
569
  const bot_id = req.params['bot_id'];
570
570
  const block_id = req.params['block_id'];
571
571
  const body = req.body;
572
- console.log('/block/:project_id/:bot_id/:block_id:', project_id, "/", bot_id, "/", block_id);
573
- console.log('/block/:project_id/:bot_id/:block_id.body', body);
572
+ // console.log('/block/:project_id/:bot_id/:block_id:', project_id, "/", bot_id, "/", block_id);
573
+ // console.log('/block/:project_id/:bot_id/:block_id.body', body);
574
574
 
575
575
  // invoke block
576
576
  // unique ID for each execution
@@ -588,11 +588,11 @@ router.post('/block/:project_id/:bot_id/:block_id', async (req, res) => {
588
588
  "attributes": {
589
589
  "payload": body
590
590
  }
591
- }, token: ".."
591
+ }
592
592
  }
593
593
  console.log("sendMessageToBot()...");
594
594
  sendMessageToBot(process.env.TYBOT_ENDPOINT, request, bot_id, async () => {
595
- console.log("Async webhook message sent:\n", request);
595
+ // console.log("Async webhook message sent:\n", request);
596
596
  res.status(200).send({"success":true});
597
597
  return;
598
598
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.119",
3
+ "version": "0.2.121",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,7 +14,7 @@
14
14
  "@tiledesk/helpcenter-query-client": "^0.1.8",
15
15
  "@tiledesk/tiledesk-chatbot-client": "^0.5.30",
16
16
  "@tiledesk/tiledesk-chatbot-util": "^0.8.39",
17
- "@tiledesk/tiledesk-client": "^0.10.12",
17
+ "@tiledesk/tiledesk-client": "^0.10.13",
18
18
  "accept-language-parser": "^1.5.0",
19
19
  "axios": "^0.27.2",
20
20
  "body-parser": "^1.19.0",
@@ -52,6 +52,7 @@ const { DirIfOnlineAgentsV2 } = require('./directives/DirIfOnlineAgentsV2');
52
52
  const { DirContactUpdate } = require('./directives/DirContactUpdate');
53
53
  const { DirClearTranscript } = require('./directives/DirClearTranscript');
54
54
  const { DirMoveToUnassigned } = require('./directives/DirMoveToUnassigned');
55
+ const { DirWhatsappStatic, DirSendWhatsapp } = require('./directives/DirSendWhatsapp');
55
56
 
56
57
  class DirectivesChatbotPlug {
57
58
 
@@ -621,6 +622,17 @@ class DirectivesChatbotPlug {
621
622
  this.process(next_dir);
622
623
  });
623
624
  }
625
+ else if (directive_name === Directives.SEND_WHATSAPP) {
626
+ new DirSendWhatsapp(context).execute(directive, async (stop) => {
627
+ if (stop == true) {
628
+ if (context.log) { console.log("Stoppin Actions on:", JSON.stringify(directive));}
629
+ this.theend();
630
+ } else {
631
+ let next_dir = await this.nextDirective(this.directives);
632
+ this.process(next_dir);
633
+ }
634
+ });
635
+ }
624
636
  else if (directive_name === Directives.QAPLA) {
625
637
  new DirQapla(context).execute(directive, async (stop) => {
626
638
  if (context.log) { console.log("DirQapla stop?", stop);}
@@ -22,6 +22,10 @@ class WebhookChatbotPlug {
22
22
  if (answer.attributes && answer.attributes.webhook && answer.attributes.webhook === true) {
23
23
  if (this.log) {console.log("EXECUTING WEBHOOK URL!", this.webhookurl);}
24
24
  if (this.log) {console.log("EXECUTING WEBHOOK ON CONTEXT:", JSON.stringify(context));}
25
+ if (!this.validWebhookURL(this.webhookurl)) {
26
+ if (this.log) {console.error("(WebhookChatbotPlug) Error. Invalid webhook URL:", this.webhookurl, "on context:", JSON.stringify(context));}
27
+ pipeline.nextplug();
28
+ }
25
29
  this.execWebhook(answer, context, this.webhookurl, (err, message_from_webhook) => {
26
30
  if (this.log) {console.log("message_from_webhook:", message_from_webhook);}
27
31
  if (err) {
@@ -67,6 +71,13 @@ class WebhookChatbotPlug {
67
71
  if (this.log) {console.log("Start processing webhook...");}
68
72
  }
69
73
 
74
+ validWebhookURL(webhookurl) {
75
+ if (!webhookurl) {
76
+ return false;
77
+ }
78
+ return true;
79
+ }
80
+
70
81
  execWebhook(reply_message, context, webhookurl, callback) {
71
82
  if (this.log) {
72
83
  console.log("WEBHOOK. on context", JSON.stringify(context));
@@ -15,6 +15,7 @@ class DirAskGPTV2 {
15
15
  throw new Error('context object is mandatory');
16
16
  }
17
17
  this.context = context;
18
+ this.chatbot = context.chatbot;
18
19
  this.tdcache = this.context.tdcache;
19
20
  this.requestId = this.context.requestId;
20
21
  this.intentDir = new DirIntent(context);
@@ -28,6 +28,7 @@ class DirJSONCondition {
28
28
  // APIKEY: "___",
29
29
  // log: context.log
30
30
  // });
31
+ this.chatbot = context.chatbot;
31
32
  this.intentDir = new DirIntent(context);
32
33
  // {
33
34
  // API_ENDPOINT: context.TILEDESK_APIURL,
@@ -0,0 +1,247 @@
1
+ const axios = require("axios").default;
2
+ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
+ const { Filler } = require("../Filler");
4
+ const { DirIntent } = require("./DirIntent");
5
+
6
+ let whatsapp_api_url;
7
+
8
+ class DirSendWhatsapp {
9
+
10
+ constructor(context) {
11
+ if (!context) {
12
+ throw new Error('context object is mandatory');
13
+ }
14
+ this.context = context;
15
+ this.chatbot = context.chatbot;
16
+ this.tdcache = this.context.tdcache;
17
+ this.requestId = this.context.requestId;
18
+ this.intentDir = new DirIntent(context);
19
+ this.log = context.log;
20
+ }
21
+
22
+ execute(directive, callback) {
23
+ if (this.log) { console.log("DirWhatsappStatic directive: ", directive); }
24
+ let action;
25
+ if (directive.action) {
26
+ action = directive.action;
27
+ }
28
+ else {
29
+ console.error("Incorrect directive: ", JSON.stringify(directive));
30
+ callback();
31
+ return;
32
+ }
33
+ this.go(action, (stop) => {
34
+ callback(stop);
35
+ })
36
+ }
37
+
38
+ async go(action, callback) {
39
+
40
+ if (this.log) { console.log("DirWhatsappStatic action: ", JSON.stringify(action)) }
41
+ if (!this.tdcache) {
42
+ console.error("Error: DirAskGPT tdcache is mandatory");
43
+ callback();
44
+ return;
45
+ }
46
+
47
+ let trueIntent = action.trueIntent;
48
+ let falseIntent = action.falseIntent;
49
+
50
+ let requestVariables = null;
51
+ requestVariables =
52
+ await TiledeskChatbot.allParametersStatic(
53
+ this.tdcache, this.requestId
54
+ )
55
+
56
+ // Declarations
57
+ let payload = action.payload;
58
+
59
+ const filler = new Filler();
60
+
61
+ // receiver_list will be of just one element, so we can pick up only the first element, if exists.
62
+ let receiver = payload.receiver_list[0];
63
+
64
+
65
+ //header_params: text, image, document. NO: location
66
+ //body_params: text
67
+ //button_params: text
68
+ //footer_paramas: non supportati
69
+
70
+ receiver = await this.fillWholeReceiver(receiver, requestVariables);
71
+ payload.receiver_list[0] = receiver;
72
+ payload.transaction_id = this.context.requestId;
73
+ payload.broadcast = false;
74
+
75
+ const whatsapp_api_url_pre = process.env.WHATSAPP_ENDPOINT;
76
+ const server_base_url = process.env.API_URL || process.env.API_ENDPOINT;
77
+
78
+ if (whatsapp_api_url_pre) {
79
+ whatsapp_api_url = whatsapp_api_url_pre;
80
+ } else {
81
+ whatsapp_api_url = server_base_url + "/modules/whatsapp/api"
82
+ }
83
+ if (this.log) { console.log("DirSendWhatsapp whatsapp_api_url: ", whatsapp_api_url); };
84
+
85
+ const HTTPREQUEST = {
86
+ url: whatsapp_api_url + "/tiledesk/broadcast",
87
+ headers: {
88
+ 'Content-Type': 'application/json'
89
+ },
90
+ json: payload,
91
+ method: 'POST'
92
+ }
93
+
94
+ if (this.log) { console.log("DirSendWhatsapp HTTPREQUEST: ", HTTPREQUEST); }
95
+
96
+ this.#myrequest(
97
+ HTTPREQUEST, async (err, resbody) => {
98
+ if (err) {
99
+ console.error("DirSendWhatsapp error: ", err);
100
+ await this.chatbot.addParameter("flowError", "SendWhatsapp Error: " + err);
101
+ if (callback) {
102
+ if (falseIntent) {
103
+ await this.#executeCondition(false, trueIntent, null, falseIntent, null);
104
+ callback(true);
105
+ return;
106
+ }
107
+ callback();
108
+ return;
109
+ }
110
+ } else if (resbody.success === true) {
111
+ if (callback) {
112
+ if (trueIntent) {
113
+ await this.#executeCondition(true, trueIntent, null, falseIntent, null);
114
+ callback(true);
115
+ return;
116
+ }
117
+ callback();
118
+ return;
119
+ }
120
+ } else {
121
+ if (this.log) { console.log("DirSendWhatsapp unexpected resbody: ", resbody); }
122
+ if (callback) {
123
+ if (falseIntent) {
124
+ await this.#executeCondition(false, trueIntent, null, falseIntent, null);
125
+ callback(true);
126
+ return
127
+ }
128
+ callback();
129
+ return;
130
+ }
131
+ }
132
+ }
133
+ )
134
+ }
135
+
136
+ async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
137
+ let trueIntentDirective = null;
138
+ if (trueIntent) {
139
+ trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
140
+ }
141
+ let falseIntentDirective = null;
142
+ if (falseIntent) {
143
+ falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
144
+ }
145
+ if (result === true) {
146
+ if (trueIntentDirective) {
147
+ this.intentDir.execute(trueIntentDirective, () => {
148
+ if (callback) {
149
+ callback();
150
+ }
151
+ })
152
+ }
153
+ else {
154
+ if (this.log) { console.log("No trueIntentDirective specified"); }
155
+ if (callback) {
156
+ callback();
157
+ }
158
+ }
159
+ }
160
+ else {
161
+ if (falseIntentDirective) {
162
+ this.intentDir.execute(falseIntentDirective, () => {
163
+ if (callback) {
164
+ callback();
165
+ }
166
+ });
167
+ }
168
+ else {
169
+ if (this.log) { console.log("No falseIntentDirective specified"); }
170
+ if (callback) {
171
+ callback();
172
+ }
173
+ }
174
+ }
175
+ }
176
+
177
+ async fillWholeReceiver(receiver, requestVariables) {
178
+ return new Promise((resolve) => {
179
+
180
+ const filler = new Filler();
181
+ try {
182
+ receiver.phone_number = filler.fill(receiver.phone_number, requestVariables);
183
+ if (receiver.header_params) {
184
+ receiver.header_params.forEach(p => {
185
+ if (p.type === 'TEXT') {
186
+ p.text = filler.fill(p.text, requestVariables)
187
+ }
188
+ else if (p.type === 'IMAGE') {
189
+ p.image.link = filler.fill(p.image.link, requestVariables)
190
+ }
191
+ else if (p.type === 'DOCUMENT') {
192
+ p.document.link = filler.fill(p.document.link, requestVariables)
193
+ }
194
+ })
195
+ }
196
+
197
+ if (receiver.body_params) {
198
+ receiver.body_params.forEach(p => {
199
+ p.text = filler.fill(p.text, requestVariables)
200
+ })
201
+ }
202
+
203
+ if (receiver.buttons_params) {
204
+ receiver.buttons_params.forEach(p => {
205
+ p.text = filler.fill(p.text, requestVariables)
206
+ })
207
+ }
208
+
209
+ resolve(receiver);
210
+
211
+ } catch(err) {
212
+ console.error("DirSendWhatsapp fillWholeReceiver error: ", err)
213
+ resolve(null);
214
+ }
215
+
216
+ })
217
+ }
218
+
219
+ // HTTP REQUEST
220
+ async #myrequest(options, callback, log) {
221
+ return await axios({
222
+ url: options.url,
223
+ method: options.method,
224
+ data: options.json,
225
+ params: options.params,
226
+ headers: options.headers
227
+ }).then((res) => {
228
+ if (res && res.status == 200 && res.data) {
229
+ if (callback) {
230
+ callback(null, res.data);
231
+ }
232
+ }
233
+ else {
234
+ if (callback) {
235
+ callback(TiledeskClient.getErr({ message: "Response status not 200" }, options, res), null, null);
236
+ }
237
+ }
238
+ }).catch((err) => {
239
+ console.error("(tybot request) An error occured: ", err);
240
+ if (callback) {
241
+ callback(err, null, null);
242
+ }
243
+ })
244
+ }
245
+ }
246
+
247
+ module.exports = { DirSendWhatsapp }
@@ -265,8 +265,6 @@ class DirWebRequestV2 {
265
265
  return value;
266
266
  });
267
267
  console.log("** Options:", str_Options);
268
-
269
-
270
268
  }
271
269
  let axios_options = {
272
270
  url: options.url,
@@ -27,6 +27,7 @@ class Directives {
27
27
  static RANDOM_REPLY = 'randomreply';
28
28
  static CODE = 'code';
29
29
  static WHATSAPP_ATTRIBUTE = 'whatsapp_attribute';
30
+ static SEND_WHATSAPP = 'send_whatsapp';
30
31
  static FORM = "form";
31
32
  static CAPTURE_USER_REPLY = "capture_user_reply";
32
33
  static REPLACE_BOT_V2 = "replacebotv2";