@tiledesk/tiledesk-tybot-connector 0.2.3 → 0.2.5

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,8 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ ### 0.2.5 - prod debug
9
+
8
10
  ### 0.2.1 - online
9
11
  - bug fix: /ext/parameters/requests/:requestid replies with res.send([]);
10
12
  - bug fix: allParameterStatic() => check for attributes not null before iterating Object.entries()
package/index.js CHANGED
@@ -486,42 +486,45 @@ router.get('/message/context/:messageid', async (req, res) => {
486
486
  });
487
487
 
488
488
  router.get('/ext/parameters/requests/:requestid', async (req, res) => {
489
- res.send([]);
490
- // const requestId = req.params.requestid;
491
- // const parameters = await TiledeskChatbot.allParametersStatic(tdcache, requestId);
492
- // if (req.query.all != null) {
493
- // res.send(parameters);
494
- // }
495
- // else {
496
- // const RESERVED = [
497
- // TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY,
498
- // TiledeskChatbotConst.REQ_CHAT_URL,
499
- // TiledeskChatbotConst.REQ_CITY_KEY,
500
- // TiledeskChatbotConst.REQ_COUNTRY_KEY,
501
- // TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY,
502
- // TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY,
503
- // TiledeskChatbotConst.REQ_END_USER_ID_KEY,
504
- // TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY,
505
- // TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY,
506
- // TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY,
507
- // TiledeskChatbotConst.REQ_PROJECT_ID_KEY,
508
- // TiledeskChatbotConst.REQ_REQUEST_ID_KEY,
509
- // TiledeskChatbotConst.REQ_USER_AGENT_KEY,
510
- // TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY,
511
- // TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY
512
- // ]
513
- // let userParams = {};
514
- // if (parameters) {
515
- // for (const [key, value] of Object.entries(parameters)) {
516
- // // console.log(key, value);
517
- // // There is a bug that moves the requestId as a key in request attributes, so: && !key.startsWith("support-group-")
518
- // if (!key.startsWith("_") && !RESERVED.some(e => e === key) && !key.startsWith("support-group-")) {
519
- // userParams[key] = value;
520
- // }
521
- // }
522
- // }
523
- // res.send(userParams);
524
- // }
489
+ const requestId = req.params.requestid;
490
+ const parameters = await TiledeskChatbot.allParametersStatic(tdcache, requestId);
491
+ if (parameters === null) {
492
+ res.send([]);
493
+ return;
494
+ }
495
+ if (req.query.all != null) {
496
+ res.send(parameters);
497
+ }
498
+ else {
499
+ const RESERVED = [
500
+ TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY,
501
+ TiledeskChatbotConst.REQ_CHAT_URL,
502
+ TiledeskChatbotConst.REQ_CITY_KEY,
503
+ TiledeskChatbotConst.REQ_COUNTRY_KEY,
504
+ TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY,
505
+ TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY,
506
+ TiledeskChatbotConst.REQ_END_USER_ID_KEY,
507
+ TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY,
508
+ TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY,
509
+ TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY,
510
+ TiledeskChatbotConst.REQ_PROJECT_ID_KEY,
511
+ TiledeskChatbotConst.REQ_REQUEST_ID_KEY,
512
+ TiledeskChatbotConst.REQ_USER_AGENT_KEY,
513
+ TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY,
514
+ TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY
515
+ ]
516
+ let userParams = {};
517
+ if (parameters) {
518
+ for (const [key, value] of Object.entries(parameters)) {
519
+ // console.log(key, value);
520
+ // There is a bug that moves the requestId as a key in request attributes, so: && !key.startsWith("support-group-")
521
+ if (!key.startsWith("_") && !RESERVED.some(e => e === key) && !key.startsWith("support-group-")) {
522
+ userParams[key] = value;
523
+ }
524
+ }
525
+ }
526
+ res.send(userParams);
527
+ }
525
528
  });
526
529
 
527
530
  router.get('/', (req, res) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -614,7 +614,7 @@ class DirectivesChatbotPlug {
614
614
  token: token,
615
615
  APIURL: API_URL,
616
616
  APIKEY: "___",
617
- log: true
617
+ log: false
618
618
  });
619
619
  let i = -1;
620
620
  if (this.log) { console.log("processing Inline directives:", directives); }
@@ -1,6 +1,7 @@
1
1
  const { Filler } = require('../Filler');
2
2
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
3
  const { TiledeskChatbotUtil } = require('../../models/TiledeskChatbotUtil');
4
+ let axios = require('axios');
4
5
 
5
6
  class DirReply {
6
7
 
@@ -97,29 +98,96 @@ class DirReply {
97
98
  }
98
99
  // send!
99
100
  if (this.log) {console.log("Reply:", JSON.stringify(message))};
100
- this.context.tdclient.sendSupportMessage(
101
+ // this.context.tdclient.sendSupportMessage(
102
+ // this.requestId,
103
+ // message,
104
+ // (err) => {
105
+ // if (err) {
106
+ // console.error("Error sending reply:", err.message);
107
+ // }
108
+ // if (this.log) {console.log("Reply message sent.");}
109
+ // callback();
110
+ // });
111
+
112
+ this.sendSupportMessage(
101
113
  this.requestId,
102
114
  message,
103
115
  (err) => {
104
116
  if (err) {
105
117
  console.error("Error sending reply:", err.message);
106
118
  }
107
- if (this.log) {console.log("Reply message sent.");}
119
+ // if (this.log) {console.log("Reply message sent.");}
120
+ console.log("Reply message sent.");
108
121
  callback();
109
- });
122
+ });
123
+ }
124
+
125
+ sendSupportMessage(requestId, message, callback) {
126
+ // const jwt_token = TiledeskClient.fixToken(this.token);
127
+ //const url = `${this.APIURL}/${this.projectId}/requests/${requestId}/messages`;
128
+ const url = `${this.context.tdclient.APIURL}/${this.projectId}/requests/${requestId}/messages`
129
+ const HTTPREQUEST = {
130
+ url: url,
131
+ headers: {
132
+ 'Content-Type' : 'application/json',
133
+ 'Authorization': "JWT " + this.token
134
+ },
135
+ json: message,
136
+ method: 'POST'
137
+ };
138
+ this.myrequest(
139
+ HTTPREQUEST,
140
+ function(err, response, resbody) {
141
+ if (response.status === 200) {
142
+ if (callback) {
143
+ callback(null, resbody)
144
+ }
145
+ }
146
+ else if (callback) {
147
+ callback(err);
148
+ }
149
+ }, this.log
150
+ );
110
151
  }
111
152
 
112
- // fillCommandTemplates(command, variables) {
113
- // if (command && command.attributes && command.attachment && command.attachment.buttons && command.attachment.buttons.length > 0){
114
- // let buttons = command.attachment.buttons.length;
115
- // const filler = new Filler();
116
- // buttons.forEach(button => {
117
- // if (button.link) {
118
- // button.link = filler.fill(button.link, variables);
119
- // }
120
- // });
121
- // }
122
- // }
153
+ myrequest(options, callback, log) {
154
+ if (log) {
155
+ console.log("API URL:", options.url);
156
+ console.log("** Options:", JSON.stringify(options));
157
+ }
158
+ axios(
159
+ {
160
+ url: options.url,
161
+ method: options.method,
162
+ data: options.json,
163
+ params: options.params,
164
+ headers: options.headers
165
+ })
166
+ .then((res) => {
167
+ // if (log) {
168
+ console.log("Reply: Response for url:", options.url);
169
+ console.log("Reply: Response headers:\n", JSON.stringify(res.headers));
170
+ //console.log("******** Response for url:", res);
171
+ // }
172
+ if (res && res.status == 200 && res.data) {
173
+ if (callback) {
174
+ callback(null, res.data);
175
+ }
176
+ }
177
+ else {
178
+ if (callback) {
179
+ console.error("Status ! 200");
180
+ callback({ message: "Response status not 200" });
181
+ }
182
+ }
183
+ })
184
+ .catch((error) => {
185
+ console.error("An error occurred:", error);
186
+ if (callback) {
187
+ callback(error, null, null);
188
+ }
189
+ });
190
+ }
123
191
 
124
192
  }
125
193