@tiledesk/tiledesk-tybot-connector 0.2.119 → 0.2.120

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
@@ -19,6 +19,8 @@ available on:
19
19
 
20
20
  # v0.2.120 -> test
21
21
  - removed check in go() - it is already in execute() - on SetAttributeV2: if (!action) {...}
22
+ - merged Whatsapp Action
23
+ - updated "@tiledesk/tiledesk-client": "^0.10.13"
22
24
 
23
25
  # v0.2.119 -> test
24
26
  - fixed: addParameter("payload", message) => addParameter("payload", message.attributes.payload)
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.120",
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);}
@@ -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 }
@@ -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";