@tiledesk/tiledesk-tybot-connector 0.2.4 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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