@tiledesk/tiledesk-tybot-connector 0.2.138 → 0.2.139-rc1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.138",
3
+ "version": "0.2.139-rc1",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -2,6 +2,9 @@ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
2
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
3
  const { Filler } = require('../Filler');
4
4
 
5
+ const axios = require("axios").default;
6
+ let https = require("https");
7
+
5
8
  class DirReplaceBotV2 {
6
9
 
7
10
  constructor(context) {
@@ -52,30 +55,125 @@ class DirReplaceBotV2 {
52
55
  );
53
56
  const filler = new Filler();
54
57
  botName = filler.fill(botName, variables);
55
- this.tdClient.replaceBotByName(this.requestId, botName, () => {
56
- if (blockName) {
57
- if (this.log) {console.log("Sending hidden /start message to bot in dept");}
58
- const message = {
59
- type: "text",
60
- text: "/" + blockName,
61
- attributes : {
62
- subtype: "info"
58
+
59
+ const HTTPREQUEST = {
60
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/requests/" + this.requestId + "/replace",
61
+ headers: {
62
+ 'Content-Type': 'application/json',
63
+ 'Authorization': 'JWT ' + this.context.token
64
+ },
65
+ json: { name: botName},
66
+ method: 'PUT'
67
+ }
68
+
69
+ this.#myrequest(
70
+ HTTPREQUEST, async (err, resbody) => {
71
+ if (err) {
72
+ console.log("DirReplaceBot error: ", err);
73
+ if (callback) {
74
+ callback();
75
+ return;
63
76
  }
64
77
  }
65
- this.tdClient.sendSupportMessage(
66
- this.requestId,
67
- message, (err) => {
68
- if (err) {
69
- console.error("Error sending hidden message:", err.message);
78
+
79
+ if (this.log) { console.log("DirReplaceBot replace resbody: ", resbody) };
80
+ if (blockName) {
81
+ if (this.log) { console.log("Sending hidden /start message to bot in dept"); }
82
+ const message = {
83
+ type: "text",
84
+ text: "/" + blockName,
85
+ attributes: {
86
+ subtype: "info"
70
87
  }
71
- if (this.log) {console.log("Hidden message sent.");}
72
- callback();
73
- });
74
- }
75
- else {
76
- callback();
88
+ }
89
+ this.tdClient.sendSupportMessage(
90
+ this.requestId,
91
+ message, (err) => {
92
+ if (err) {
93
+ console.error("Error sending hidden message:", err.message);
94
+ }
95
+ if (this.log) { console.log("Hidden message sent."); }
96
+ callback();
97
+ });
98
+ }
99
+ else {
100
+ callback();
101
+ }
77
102
  }
78
- });
103
+ )
104
+
105
+ // this.tdClient.replaceBotByName(this.requestId, botName, () => {
106
+ // if (blockName) {
107
+ // if (this.log) {console.log("Sending hidden /start message to bot in dept");}
108
+ // const message = {
109
+ // type: "text",
110
+ // text: "/" + blockName,
111
+ // attributes : {
112
+ // subtype: "info"
113
+ // }
114
+ // }
115
+ // this.tdClient.sendSupportMessage(
116
+ // this.requestId,
117
+ // message, (err) => {
118
+ // if (err) {
119
+ // console.error("Error sending hidden message:", err.message);
120
+ // }
121
+ // if (this.log) {console.log("Hidden message sent.");}
122
+ // callback();
123
+ // });
124
+ // }
125
+ // else {
126
+ // callback();
127
+ // }
128
+ // });
129
+ }
130
+
131
+ #myrequest(options, callback) {
132
+ if (this.log) {
133
+ console.log("API URL:", options.url);
134
+ console.log("** Options:", JSON.stringify(options));
135
+ }
136
+ let axios_options = {
137
+ url: options.url,
138
+ method: options.method,
139
+ params: options.params,
140
+ headers: options.headers
141
+ }
142
+ if (options.json !== null) {
143
+ axios_options.data = options.json
144
+ }
145
+ if (this.log) {
146
+ console.log("axios_options:", JSON.stringify(axios_options));
147
+ }
148
+ if (options.url.startsWith("https:")) {
149
+ const httpsAgent = new https.Agent({
150
+ rejectUnauthorized: false,
151
+ });
152
+ axios_options.httpsAgent = httpsAgent;
153
+ }
154
+ axios(axios_options)
155
+ .then((res) => {
156
+ if (this.log) {
157
+ console.log("Response for url:", options.url);
158
+ console.log("Response headers:\n", JSON.stringify(res.headers));
159
+ }
160
+ if (res && res.status == 200 && res.data) {
161
+ if (callback) {
162
+ callback(null, res.data);
163
+ }
164
+ }
165
+ else {
166
+ if (callback) {
167
+ callback(new Error("Response status is not 200"), null);
168
+ }
169
+ }
170
+ })
171
+ .catch((error) => {
172
+ console.error("(DirAskGPT) Axios error: ", JSON.stringify(error));
173
+ if (callback) {
174
+ callback(error, null);
175
+ }
176
+ });
79
177
  }
80
178
  }
81
179