n8n-nodes-telepilot-2 0.5.2

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.
Files changed (31) hide show
  1. package/LICENSE.md +7 -0
  2. package/README.md +152 -0
  3. package/deploy/test-n8n-imports/credentials/credential-dummy.json +20 -0
  4. package/deploy/test-n8n-imports/workflows/Telepilot_getMe.json +68 -0
  5. package/dist/credentials/TelePilotApi.credentials.d.ts +7 -0
  6. package/dist/credentials/TelePilotApi.credentials.js +46 -0
  7. package/dist/credentials/TelePilotApi.credentials.js.map +1 -0
  8. package/dist/nodes/TelePilot/TelePilot.node.d.ts +6 -0
  9. package/dist/nodes/TelePilot/TelePilot.node.js +960 -0
  10. package/dist/nodes/TelePilot/TelePilot.node.js.map +1 -0
  11. package/dist/nodes/TelePilot/TelePilot.node.json +18 -0
  12. package/dist/nodes/TelePilot/TelePilot.svg +16 -0
  13. package/dist/nodes/TelePilot/TelePilotNodeConnectionManager.d.ts +48 -0
  14. package/dist/nodes/TelePilot/TelePilotNodeConnectionManager.js +255 -0
  15. package/dist/nodes/TelePilot/TelePilotNodeConnectionManager.js.map +1 -0
  16. package/dist/nodes/TelePilot/TelePilotTrigger.node.d.ts +5 -0
  17. package/dist/nodes/TelePilot/TelePilotTrigger.node.js +127 -0
  18. package/dist/nodes/TelePilot/TelePilotTrigger.node.js.map +1 -0
  19. package/dist/nodes/TelePilot/common.descriptions.d.ts +53 -0
  20. package/dist/nodes/TelePilot/common.descriptions.js +1041 -0
  21. package/dist/nodes/TelePilot/common.descriptions.js.map +1 -0
  22. package/dist/nodes/TelePilot/tdlib/types.d.ts +8 -0
  23. package/dist/nodes/TelePilot/tdlib/types.js +3 -0
  24. package/dist/nodes/TelePilot/tdlib/types.js.map +1 -0
  25. package/dist/nodes/TelePilot/tdlib/updateEvents.d.ts +2 -0
  26. package/dist/nodes/TelePilot/tdlib/updateEvents.js +394 -0
  27. package/dist/nodes/TelePilot/tdlib/updateEvents.js.map +1 -0
  28. package/dist/package.json +66 -0
  29. package/dist/tsconfig.tsbuildinfo +1 -0
  30. package/index.js +0 -0
  31. package/package.json +66 -0
@@ -0,0 +1,960 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TelePilot = void 0;
4
+ require("reflect-metadata");
5
+ const typedi_1 = require("typedi");
6
+ const TelePilotNodeConnectionManager_1 = require("./TelePilotNodeConnectionManager");
7
+ const common_descriptions_1 = require("./common.descriptions");
8
+ const debug = require('debug')('telepilot-node');
9
+ class TelePilot {
10
+ constructor() {
11
+ this.description = {
12
+ displayName: 'Telegram CoPilot',
13
+ name: 'telePilot',
14
+ icon: 'file:TelePilot.svg',
15
+ group: ['transform'],
16
+ version: 1,
17
+ description: 'Your Personal Telegram CoPilot',
18
+ defaults: {
19
+ name: 'Telegram CoPilot',
20
+ },
21
+ credentials: [
22
+ {
23
+ name: 'telePilotApi',
24
+ required: true,
25
+ },
26
+ ],
27
+ inputs: ['main'],
28
+ outputs: ['main'],
29
+ properties: [
30
+ common_descriptions_1.optionResources,
31
+ common_descriptions_1.operationLogin,
32
+ common_descriptions_1.operationUser,
33
+ common_descriptions_1.operationContact,
34
+ common_descriptions_1.operationGroup,
35
+ common_descriptions_1.operationChat,
36
+ common_descriptions_1.operationMessage,
37
+ common_descriptions_1.operationFile,
38
+ common_descriptions_1.operationCustom,
39
+ common_descriptions_1.variable_user_id,
40
+ common_descriptions_1.variable_force,
41
+ common_descriptions_1.variable_chat_id,
42
+ common_descriptions_1.variable_from_chat_id,
43
+ common_descriptions_1.variable_is_marked_as_unread,
44
+ common_descriptions_1.variable_from_message_id,
45
+ common_descriptions_1.variable_limit,
46
+ common_descriptions_1.variable_message_ids,
47
+ common_descriptions_1.variable_message_id,
48
+ common_descriptions_1.variable_message_force_read,
49
+ common_descriptions_1.variable_messageText,
50
+ common_descriptions_1.variable_local_photo_path,
51
+ common_descriptions_1.variable_photo_caption,
52
+ common_descriptions_1.variable_audio_path,
53
+ common_descriptions_1.variable_audio_file_path,
54
+ common_descriptions_1.variable_audio_caption,
55
+ common_descriptions_1.variable_video_photo_path,
56
+ common_descriptions_1.variable_file_path,
57
+ common_descriptions_1.variable_file_caption,
58
+ common_descriptions_1.variable_audio_binary_property_name,
59
+ common_descriptions_1.variable_send_as_voice,
60
+ common_descriptions_1.variable_revoke,
61
+ common_descriptions_1.variable_username,
62
+ common_descriptions_1.variable_query,
63
+ common_descriptions_1.variable_title,
64
+ common_descriptions_1.variable_message_thread_id,
65
+ common_descriptions_1.variable_description,
66
+ common_descriptions_1.variable_is_channel,
67
+ common_descriptions_1.variable_user_ids,
68
+ common_descriptions_1.variable_chat_action,
69
+ common_descriptions_1.variable_url,
70
+ common_descriptions_1.variable_video_duration,
71
+ common_descriptions_1.variable_video_width,
72
+ common_descriptions_1.variable_video_height,
73
+ common_descriptions_1.variable_video_supports_streaming,
74
+ common_descriptions_1.variable_thumbnail_width,
75
+ common_descriptions_1.variable_thumbnail_height,
76
+ common_descriptions_1.variable_thumbnail_file_path,
77
+ common_descriptions_1.variable_json,
78
+ common_descriptions_1.variable_file_id,
79
+ common_descriptions_1.variable_remote_file_id,
80
+ common_descriptions_1.variable_reply_to_msg_id,
81
+ common_descriptions_1.variable_supergroup_id,
82
+ common_descriptions_1.variable_audio_binary_property_name,
83
+ common_descriptions_1.variable_send_as_voice
84
+ ],
85
+ };
86
+ }
87
+ async execute() {
88
+ var _a;
89
+ const returnData = [];
90
+ const resource = this.getNodeParameter('resource', 0);
91
+ const operation = this.getNodeParameter('operation', 0);
92
+ const credentials = await this.getCredentials('telePilotApi');
93
+ const cM = typedi_1.Container.get(TelePilotNodeConnectionManager_1.TelePilotNodeConnectionManager);
94
+ debug('Executing telePilot node, resource=' + resource + ', operation=' + operation);
95
+ let result;
96
+ let client;
97
+ if (resource === 'login') {
98
+ if (operation === 'login') {
99
+ const loginWithPhoneNumberHelpCommand = () => {
100
+ return {
101
+ text: "Following commands are supported:\n\n" +
102
+ "/start - start login via Phone Number and code (MFA is also supported if set)\n" +
103
+ "/stop - terminates current ClientSession for this Credential\n" +
104
+ "/clear - deletes local tdlib database, new login is required\n" +
105
+ "/cred - shows which Telegram Credential is used in this ChatTrigger (name + apiId, apiHash, phoneNumber)\n" +
106
+ "/stat - print all open Telegram sessions"
107
+ };
108
+ };
109
+ debug('loginWithPhoneNumber');
110
+ const items = this.getInputData();
111
+ const message = items[0].json['chatInput'];
112
+ debug("message received: " + message);
113
+ if (message === undefined) {
114
+ returnData.push({
115
+ compatibility: "QR-Code login is disabled starting from version 0.3.0",
116
+ doc: "Please connect ChatTrigger to this node and read instructions:",
117
+ url: "https://telepilot.co/login-howto"
118
+ });
119
+ }
120
+ else if (message.startsWith("/")) {
121
+ switch (message) {
122
+ case "/start":
123
+ let authState = cM.getAuthStateForCredential(credentials === null || credentials === void 0 ? void 0 : credentials.apiId);
124
+ debug("loginWithPhoneNumber./start.authState: " + authState);
125
+ if (authState == TelePilotNodeConnectionManager_1.TelepilotAuthState.NO_CONNECTION) {
126
+ await cM.createClientSetAuthHandlerForPhoneNumberLogin(credentials === null || credentials === void 0 ? void 0 : credentials.apiId, credentials === null || credentials === void 0 ? void 0 : credentials.apiHash, credentials === null || credentials === void 0 ? void 0 : credentials.phoneNumber);
127
+ authState = cM.getAuthStateForCredential(credentials === null || credentials === void 0 ? void 0 : credentials.apiId);
128
+ debug("loginWithPhoneNumber./start2.authState: " + authState);
129
+ if (authState == TelePilotNodeConnectionManager_1.TelepilotAuthState.WAIT_CODE) {
130
+ returnData.push("Please provide AuthCode:");
131
+ }
132
+ else if (authState == TelePilotNodeConnectionManager_1.TelepilotAuthState.WAIT_PASSWORD) {
133
+ returnData.push("MFA Password:");
134
+ }
135
+ }
136
+ switch (authState) {
137
+ case TelePilotNodeConnectionManager_1.TelepilotAuthState.WAIT_PHONE_NUMBER:
138
+ await cM.clientLoginWithPhoneNumber(credentials === null || credentials === void 0 ? void 0 : credentials.apiId, credentials === null || credentials === void 0 ? void 0 : credentials.apiHash, credentials === null || credentials === void 0 ? void 0 : credentials.phoneNumber);
139
+ await (0, TelePilotNodeConnectionManager_1.sleep)(1000);
140
+ authState = cM.getAuthStateForCredential(credentials === null || credentials === void 0 ? void 0 : credentials.apiId);
141
+ if (authState == TelePilotNodeConnectionManager_1.TelepilotAuthState.WAIT_CODE) {
142
+ returnData.push("Please provide AuthCode:");
143
+ }
144
+ else if (authState == TelePilotNodeConnectionManager_1.TelepilotAuthState.WAIT_READY) {
145
+ returnData.push("You have succesfully logged in. You can close this chat and start using Telepilot.");
146
+ }
147
+ else {
148
+ returnData.push("Unexpected authState: " + authState);
149
+ }
150
+ break;
151
+ case TelePilotNodeConnectionManager_1.TelepilotAuthState.WAIT_READY:
152
+ returnData.push("You are logged in with phoneNumber " + (credentials === null || credentials === void 0 ? void 0 : credentials.phoneNumber));
153
+ break;
154
+ default:
155
+ debug("unexpected authState=" + authState);
156
+ returnData.push("unexpected authState=" + authState);
157
+ break;
158
+ }
159
+ break;
160
+ case "/stop":
161
+ cM.closeLocalSession(credentials === null || credentials === void 0 ? void 0 : credentials.apiId);
162
+ returnData.push("Telegram Account " + (credentials === null || credentials === void 0 ? void 0 : credentials.phoneNumber) + " disconnected.");
163
+ break;
164
+ case "/clear":
165
+ cM.deleteLocalInstance(credentials === null || credentials === void 0 ? void 0 : credentials.apiId);
166
+ returnData.push({
167
+ text: "Telegram Account disconnected, local session has been cleared. Please login again. " +
168
+ "Please check our guide at https://telepilot.co/login-howto"
169
+ });
170
+ break;
171
+ case "/cred":
172
+ let credResult = credentials;
173
+ credResult.apiHash = "[DELETED]";
174
+ returnData.push(credResult);
175
+ break;
176
+ case "/help":
177
+ returnData.push(loginWithPhoneNumberHelpCommand());
178
+ break;
179
+ case "/stat":
180
+ returnData.push(cM.getAllClientSessions());
181
+ break;
182
+ default:
183
+ returnData.push("Command not supported." + loginWithPhoneNumberHelpCommand());
184
+ break;
185
+ }
186
+ }
187
+ else {
188
+ let authState = cM.getAuthStateForCredential(credentials === null || credentials === void 0 ? void 0 : credentials.apiId);
189
+ debug("loginWithPhoneNumber.authState: " + authState);
190
+ switch (authState) {
191
+ case TelePilotNodeConnectionManager_1.TelepilotAuthState.NO_CONNECTION:
192
+ returnData.push({
193
+ text: "Unexpected command. Please refer to https://telepilot.co/login-howto or try /help command\n"
194
+ });
195
+ break;
196
+ case TelePilotNodeConnectionManager_1.TelepilotAuthState.WAIT_CODE:
197
+ const code = message;
198
+ await cM.clientLoginSendAuthenticationCode(credentials === null || credentials === void 0 ? void 0 : credentials.apiId, code);
199
+ await (0, TelePilotNodeConnectionManager_1.sleep)(1000);
200
+ authState = cM.getAuthStateForCredential(credentials === null || credentials === void 0 ? void 0 : credentials.apiId);
201
+ if (authState == TelePilotNodeConnectionManager_1.TelepilotAuthState.WAIT_PASSWORD) {
202
+ returnData.push("MFA Password:");
203
+ }
204
+ else if (authState == TelePilotNodeConnectionManager_1.TelepilotAuthState.WAIT_READY) {
205
+ returnData.push("You have succesfully logged in. You can close this chat and start using Telepilot.");
206
+ }
207
+ else {
208
+ returnData.push("Unexpected authState: " + authState);
209
+ }
210
+ break;
211
+ case TelePilotNodeConnectionManager_1.TelepilotAuthState.WAIT_PASSWORD:
212
+ const password = message;
213
+ await cM.clientLoginSendAuthenticationPassword(credentials === null || credentials === void 0 ? void 0 : credentials.apiId, password);
214
+ await (0, TelePilotNodeConnectionManager_1.sleep)(1000);
215
+ returnData.push("authState:" + cM.getAuthStateForCredential(credentials === null || credentials === void 0 ? void 0 : credentials.apiId));
216
+ break;
217
+ case TelePilotNodeConnectionManager_1.TelepilotAuthState.WAIT_READY:
218
+ returnData.push("You are logged in with phoneNumber " + (credentials === null || credentials === void 0 ? void 0 : credentials.phoneNumber));
219
+ break;
220
+ default:
221
+ debug("unexpected authState=" + authState);
222
+ returnData.push("unexpected authState=" + authState);
223
+ break;
224
+ }
225
+ }
226
+ }
227
+ else if (operation === 'closeSession') {
228
+ try {
229
+ cM.closeLocalSession(credentials === null || credentials === void 0 ? void 0 : credentials.apiId);
230
+ }
231
+ catch (e) {
232
+ throw e;
233
+ }
234
+ returnData.push("Telegram Account " + (credentials === null || credentials === void 0 ? void 0 : credentials.phoneNumber) + " disconnected.");
235
+ }
236
+ else if (operation === 'removeTdDatabase') {
237
+ result = await cM.deleteLocalInstance(credentials === null || credentials === void 0 ? void 0 : credentials.apiId);
238
+ returnData.push({
239
+ text: "Telegram Account disconnected, local session has been cleared.\nPlease login again. Please check our guide at https://telepilot.co/login-howto\n" +
240
+ "Or use /help"
241
+ });
242
+ }
243
+ }
244
+ else {
245
+ const clientSession = await cM.createClientSetAuthHandlerForPhoneNumberLogin(credentials === null || credentials === void 0 ? void 0 : credentials.apiId, credentials === null || credentials === void 0 ? void 0 : credentials.apiHash, credentials === null || credentials === void 0 ? void 0 : credentials.phoneNumber);
246
+ debug("clientSession.authState=" + clientSession.authState);
247
+ if (clientSession.authState != TelePilotNodeConnectionManager_1.TelepilotAuthState.WAIT_READY) {
248
+ await cM.closeLocalSession(credentials === null || credentials === void 0 ? void 0 : credentials.apiId);
249
+ if (this.continueOnFail()) {
250
+ returnData.push({ json: {
251
+ message: "Telegram account not logged in. " +
252
+ "Please use ChatTrigger node together with loginWithPhoneNumber action. " +
253
+ "Please check our guide at https://telepilot.co/login-howto or use /help command in Chat Trigger Node",
254
+ error: {
255
+ _: "error",
256
+ code: -1,
257
+ message: "Please login"
258
+ }
259
+ } });
260
+ return [this.helpers.returnJsonArray(returnData)];
261
+ }
262
+ else {
263
+ throw new Error("Please login: https://telepilot.co/login-howto");
264
+ }
265
+ }
266
+ else {
267
+ client = clientSession.client;
268
+ }
269
+ }
270
+ try {
271
+ if (resource === 'user') {
272
+ if (operation === 'getMe') {
273
+ const result = await client.invoke({
274
+ _: 'getMe',
275
+ });
276
+ returnData.push(result);
277
+ }
278
+ else if (operation === 'getUser') {
279
+ const user_id = this.getNodeParameter('user_id', 0);
280
+ result = await client.invoke({
281
+ _: 'getUser',
282
+ user_id,
283
+ });
284
+ returnData.push(result);
285
+ }
286
+ else if (operation === 'getUserFullInfo') {
287
+ const user_id = this.getNodeParameter('user_id', 0);
288
+ result = await client.invoke({
289
+ _: 'getUserFullInfo',
290
+ user_id,
291
+ });
292
+ returnData.push(result);
293
+ }
294
+ else if (operation === 'createPrivateChat') {
295
+ const user_id = this.getNodeParameter('user_id', 0);
296
+ const force = this.getNodeParameter('force', 0);
297
+ result = await client.invoke({
298
+ _: 'createPrivateChat',
299
+ user_id,
300
+ force,
301
+ });
302
+ returnData.push(result);
303
+ }
304
+ else if (operation === 'createNewSecretChat') {
305
+ const user_id = this.getNodeParameter('user_id', 0);
306
+ result = await client.invoke({
307
+ _: 'createNewSecretChat',
308
+ user_id,
309
+ });
310
+ returnData.push(result);
311
+ }
312
+ }
313
+ else if (resource === 'contact') {
314
+ if (operation === 'getContacts') {
315
+ result = await client.invoke({
316
+ _: 'getContacts',
317
+ });
318
+ returnData.push(result);
319
+ }
320
+ }
321
+ else if (resource === 'chat') {
322
+ if (operation === 'getChatHistory') {
323
+ const chat_id = this.getNodeParameter('chat_id', 0);
324
+ const from_message_id = this.getNodeParameter('from_message_id', 0);
325
+ const limit = this.getNodeParameter('limit', 0);
326
+ result = await client.invoke({
327
+ _: 'getChatHistory',
328
+ chat_id,
329
+ from_message_id,
330
+ offset: 0,
331
+ limit,
332
+ only_local: false,
333
+ });
334
+ returnData.push(result);
335
+ }
336
+ else if (operation === 'getChats') {
337
+ const result = await client.invoke({
338
+ _: 'getChats',
339
+ limit: 9999,
340
+ });
341
+ returnData.push(result);
342
+ }
343
+ else if (operation === 'getChat') {
344
+ const chat_id = this.getNodeParameter('chat_id', 0);
345
+ const result = await client.invoke({
346
+ _: 'getChat',
347
+ chat_id,
348
+ });
349
+ returnData.push(result);
350
+ }
351
+ else if (operation === 'searchPublicChat') {
352
+ const username = this.getNodeParameter('username', 0);
353
+ const result = await client.invoke({
354
+ _: 'searchPublicChat',
355
+ username,
356
+ });
357
+ debug(username);
358
+ debug(result);
359
+ returnData.push(result);
360
+ }
361
+ else if (operation === 'searchPublicChats') {
362
+ const query = this.getNodeParameter('query', 0);
363
+ const result = await client.invoke({
364
+ _: 'searchPublicChats',
365
+ query,
366
+ });
367
+ debug(query);
368
+ debug(result);
369
+ returnData.push(result);
370
+ }
371
+ else if (operation === 'joinChat') {
372
+ const chat_id = this.getNodeParameter('chat_id', 0);
373
+ const result = await client.invoke({
374
+ _: 'joinChat',
375
+ chat_id,
376
+ });
377
+ debug(chat_id);
378
+ debug(result);
379
+ returnData.push(result);
380
+ }
381
+ else if (operation === 'openChat') {
382
+ const chat_id = this.getNodeParameter('chat_id', 0);
383
+ const result = await client.invoke({
384
+ _: 'openChat',
385
+ chat_id,
386
+ });
387
+ debug(chat_id);
388
+ debug(result);
389
+ returnData.push(result);
390
+ }
391
+ else if (operation === 'closeChat') {
392
+ const chat_id = this.getNodeParameter('chat_id', 0);
393
+ const result = await client.invoke({
394
+ _: 'closeChat',
395
+ chat_id,
396
+ });
397
+ debug(chat_id);
398
+ debug(result);
399
+ returnData.push(result);
400
+ }
401
+ else if (operation === 'toggleChatIsMarkedAsUnread') {
402
+ const chat_id = this.getNodeParameter('chat_id', 0);
403
+ const is_marked_as_unread = this.getNodeParameter('is_marked_as_unread', 0);
404
+ const result = await client.invoke({
405
+ _: 'toggleChatIsMarkedAsUnread',
406
+ chat_id,
407
+ is_marked_as_unread,
408
+ });
409
+ returnData.push(result);
410
+ }
411
+ else if (operation === 'createNewSupergroupChat') {
412
+ const title = this.getNodeParameter('title', 0);
413
+ const is_channel = this.getNodeParameter('is_channel', 0);
414
+ const description = this.getNodeParameter('description', 0);
415
+ const result = await client.invoke({
416
+ _: 'createNewSupergroupChat',
417
+ title,
418
+ is_channel,
419
+ description,
420
+ location: null,
421
+ for_import: false,
422
+ });
423
+ returnData.push(result);
424
+ }
425
+ else if (operation === 'deleteChat') {
426
+ const chat_id = this.getNodeParameter('chat_id', 0);
427
+ const result = await client.invoke({
428
+ _: 'deleteChat',
429
+ chat_id,
430
+ });
431
+ returnData.push(result);
432
+ }
433
+ else if (operation === 'addChatMembers') {
434
+ const chat_id = this.getNodeParameter('chat_id', 0);
435
+ const user_ids = this.getNodeParameter('user_ids', 0);
436
+ const idsArray = user_ids
437
+ .toString()
438
+ .split(',')
439
+ .map((s) => s.toString().trim());
440
+ const result = await client.invoke({
441
+ _: 'addChatMembers',
442
+ chat_id,
443
+ user_ids: idsArray,
444
+ });
445
+ returnData.push(result);
446
+ }
447
+ else if (operation === 'sendChatAction') {
448
+ const chat_id = this.getNodeParameter('chat_id', 0);
449
+ const action = {
450
+ _: this.getNodeParameter('action', 0)
451
+ };
452
+ const result = await client.invoke({
453
+ _: 'sendChatAction',
454
+ chat_id,
455
+ action,
456
+ });
457
+ returnData.push(result);
458
+ }
459
+ }
460
+ else if (resource === 'file') {
461
+ if (operation === 'getRemoteFile') {
462
+ const remote_file_id = this.getNodeParameter('remote_file_id', 0);
463
+ const result = await client.invoke({
464
+ _: 'getRemoteFile',
465
+ remote_file_id,
466
+ });
467
+ returnData.push(result);
468
+ }
469
+ else if (operation === 'downloadFile') {
470
+ const file_id = this.getNodeParameter('file_id', 0);
471
+ const result = await client.invoke({
472
+ _: 'downloadFile',
473
+ file_id,
474
+ priority: 16,
475
+ synchronous: true,
476
+ });
477
+ returnData.push(result);
478
+ }
479
+ }
480
+ else if (resource === 'message') {
481
+ if (operation === 'getMessage') {
482
+ const chat_id = this.getNodeParameter('chat_id', 0);
483
+ const message_id = this.getNodeParameter('message_id', 0);
484
+ const result = await client.invoke({
485
+ _: 'getMessage',
486
+ chat_id,
487
+ message_id,
488
+ });
489
+ returnData.push(result);
490
+ }
491
+ else if (operation === 'getMessageLink') {
492
+ const chat_id = this.getNodeParameter('chat_id', 0);
493
+ const message_id = this.getNodeParameter('message_id', 0);
494
+ const result = await client.invoke({
495
+ _: 'getMessageLink',
496
+ chat_id,
497
+ message_id,
498
+ });
499
+ returnData.push(result);
500
+ }
501
+ else if (operation === 'getMessageLinkInfo') {
502
+ const url = this.getNodeParameter('url', 0);
503
+ const result = await client.invoke({
504
+ _: 'getMessageLinkInfo',
505
+ url,
506
+ });
507
+ returnData.push(result);
508
+ }
509
+ else if (operation === 'viewMessages') {
510
+ const chat_id = this.getNodeParameter('chat_id', 0);
511
+ const message_ids = this.getNodeParameter('message_ids', 0);
512
+ const force_read = this.getNodeParameter('force_read', 0);
513
+ const idsArray = message_ids
514
+ .toString()
515
+ .split(',')
516
+ .map((s) => s.toString().trim());
517
+ const result = await client.invoke({
518
+ _: 'viewMessages',
519
+ chat_id,
520
+ message_ids: idsArray,
521
+ source: null,
522
+ force_read: force_read
523
+ });
524
+ returnData.push(result);
525
+ }
526
+ else if (operation === 'sendMessage') {
527
+ const chat_id = this.getNodeParameter('chat_id', 0);
528
+ const messageText = this.getNodeParameter('messageText', 0);
529
+ const reply_to_msg_id = this.getNodeParameter('reply_to_msg_id', 0);
530
+ const message_thread_id = this.getNodeParameter('message_thread_id', 0);
531
+ const result = await client.invoke({
532
+ _: 'sendMessage',
533
+ chat_id,
534
+ reply_to_msg_id,
535
+ message_thread_id,
536
+ input_message_content: {
537
+ _: 'inputMessageText',
538
+ text: {
539
+ _: 'formattedText',
540
+ text: messageText,
541
+ },
542
+ },
543
+ });
544
+ returnData.push(result);
545
+ }
546
+ else if (operation === 'sendMessageVideo') {
547
+ const chat_id = this.getNodeParameter('chat_id', 0);
548
+ const videoFilePath = this.getNodeParameter('videoFilePath', 0);
549
+ let videoCaption = this.getNodeParameter('fileCaption', 0);
550
+ let videoDuration = this.getNodeParameter('videoDuration', 0);
551
+ let videoWidth = this.getNodeParameter('videoWidth', 0);
552
+ let videoHeight = this.getNodeParameter('videoHeight', 0);
553
+ let videoSupportsStreaming = this.getNodeParameter('videoSupportsStreaming', 0);
554
+ let thumbnailWidth = this.getNodeParameter('thumbnailWidth', 0);
555
+ let thumbnailHeight = this.getNodeParameter('thumbnailHeight', 0);
556
+ let thumbnailFilePath = this.getNodeParameter('thumbnailFilePath', 0);
557
+ const reply_to_msg_id = this.getNodeParameter('reply_to_msg_id', 0);
558
+ const message_thread_id = this.getNodeParameter('message_thread_id', 0);
559
+ if (videoCaption === '' && videoCaption.length == 0) {
560
+ videoCaption = null;
561
+ }
562
+ const result = await client.invoke({
563
+ _: 'sendMessage',
564
+ chat_id,
565
+ reply_to_msg_id,
566
+ message_thread_id,
567
+ input_message_content: {
568
+ _: 'inputMessageVideo',
569
+ video: {
570
+ _: 'inputFileLocal',
571
+ path: videoFilePath,
572
+ },
573
+ duration: videoDuration,
574
+ width: videoWidth,
575
+ height: videoHeight,
576
+ supports_streaming: videoSupportsStreaming,
577
+ thumbnail: {
578
+ _: 'inputThumbnail',
579
+ thumbnail: {
580
+ '_': 'inputFileLocal',
581
+ path: thumbnailFilePath
582
+ },
583
+ width: thumbnailWidth,
584
+ height: thumbnailHeight,
585
+ },
586
+ caption: {
587
+ _: 'formattedText',
588
+ text: videoCaption,
589
+ },
590
+ },
591
+ });
592
+ returnData.push(result);
593
+ }
594
+ else if (operation === 'sendMessageAudio') {
595
+ const outputItem = {
596
+ operation: 'sendMessageAudio',
597
+ success: false,
598
+ error: null,
599
+ result: null,
600
+ };
601
+ try {
602
+ const chat_id = this.getNodeParameter('chat_id', 0);
603
+ outputItem.chat_id = chat_id;
604
+ const audioSource = this.getNodeParameter('audioSource', 0);
605
+ outputItem.audioSource = audioSource;
606
+ const sendAsVoice = this.getNodeParameter('sendAsVoice', 0, false);
607
+ outputItem.sendAsVoice = sendAsVoice;
608
+ let audioFilePath = '';
609
+ let processError = false;
610
+ if (audioSource === 'binaryData') {
611
+ try {
612
+ const binaryPropertyName = this.getNodeParameter('audioBinaryPropertyName', 0);
613
+ outputItem.binaryPropertyName = binaryPropertyName;
614
+ const inputData = this.getInputData();
615
+ if (!inputData || !inputData[0]) {
616
+ outputItem.error = 'No input data available';
617
+ processError = true;
618
+ }
619
+ else {
620
+ const binaryData = inputData[0].binary;
621
+ if (!binaryData) {
622
+ outputItem.error = 'No binary data exists on input item';
623
+ processError = true;
624
+ }
625
+ else {
626
+ const binaryProperty = binaryData[binaryPropertyName];
627
+ if (!binaryProperty) {
628
+ outputItem.error = `Binary property "${binaryPropertyName}" not found`;
629
+ processError = true;
630
+ }
631
+ else if (!binaryProperty.filepath) {
632
+ outputItem.error = `Binary property "${binaryPropertyName}" does not contain a filepath`;
633
+ processError = true;
634
+ }
635
+ else {
636
+ audioFilePath = binaryProperty.filepath;
637
+ outputItem.audioFilePath = audioFilePath;
638
+ }
639
+ }
640
+ }
641
+ }
642
+ catch (binaryError) {
643
+ outputItem.error = `Binary data error: ${binaryError.message}`;
644
+ processError = true;
645
+ }
646
+ }
647
+ else if (audioSource === 'filePath') {
648
+ try {
649
+ audioFilePath = this.getNodeParameter('audioFilePath', 0);
650
+ if (!audioFilePath) {
651
+ outputItem.error = 'File path is empty';
652
+ processError = true;
653
+ }
654
+ else {
655
+ outputItem.audioFilePath = audioFilePath;
656
+ }
657
+ }
658
+ catch (pathError) {
659
+ outputItem.error = `File path error: ${pathError.message}`;
660
+ processError = true;
661
+ }
662
+ }
663
+ else {
664
+ outputItem.error = `Invalid audio source: ${audioSource}`;
665
+ processError = true;
666
+ }
667
+ if (!processError) {
668
+ let audioCaption = null;
669
+ try {
670
+ const captionValue = this.getNodeParameter('audioCaption', 0, '');
671
+ audioCaption = captionValue && captionValue.length > 0 ? captionValue : null;
672
+ outputItem.audioCaption = audioCaption;
673
+ }
674
+ catch (error) {
675
+ }
676
+ let reply_to_msg_id = '';
677
+ try {
678
+ reply_to_msg_id = this.getNodeParameter('reply_to_msg_id', 0, '');
679
+ outputItem.reply_to_msg_id = reply_to_msg_id;
680
+ }
681
+ catch (error) {
682
+ }
683
+ try {
684
+ const fs = require('fs');
685
+ if (!fs.existsSync(audioFilePath)) {
686
+ outputItem.error = `Audio file not found at path: ${audioFilePath}`;
687
+ processError = true;
688
+ }
689
+ else {
690
+ const stats = fs.statSync(audioFilePath);
691
+ outputItem.fileSize = stats.size;
692
+ outputItem.fileExists = true;
693
+ if (sendAsVoice) {
694
+ const fileExtension = (_a = audioFilePath.split('.').pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase();
695
+ outputItem.fileExtension = fileExtension;
696
+ if (fileExtension !== 'ogg') {
697
+ outputItem.warning = 'Voice messages work best with .ogg format. Your file is in .' + fileExtension + ' format. Consider converting to .ogg for better compatibility.';
698
+ debug('Warning: Voice message format may not be optimal:', fileExtension);
699
+ }
700
+ const maxSize = 20 * 1024 * 1024;
701
+ if (stats.size > maxSize) {
702
+ outputItem.error = `File size (${Math.round(stats.size / 1024 / 1024)}MB) exceeds Telegram's limit of 20MB for voice messages`;
703
+ processError = true;
704
+ }
705
+ }
706
+ }
707
+ }
708
+ catch (fsError) {
709
+ outputItem.error = `File system error: ${fsError.message}`;
710
+ processError = true;
711
+ }
712
+ }
713
+ if (!processError) {
714
+ try {
715
+ let inputMessageContent;
716
+ if (sendAsVoice) {
717
+ inputMessageContent = {
718
+ _: 'inputMessageVoiceNote',
719
+ voice_note: {
720
+ _: 'inputFileLocal',
721
+ path: audioFilePath,
722
+ },
723
+ caption: {
724
+ _: 'formattedText',
725
+ text: outputItem.audioCaption,
726
+ },
727
+ };
728
+ }
729
+ else {
730
+ inputMessageContent = {
731
+ _: 'inputMessageAudio',
732
+ audio: {
733
+ _: 'inputFileLocal',
734
+ path: audioFilePath,
735
+ },
736
+ caption: {
737
+ _: 'formattedText',
738
+ text: outputItem.audioCaption,
739
+ },
740
+ };
741
+ }
742
+ const result = await client.invoke({
743
+ _: 'sendMessage',
744
+ chat_id,
745
+ reply_to_msg_id: outputItem.reply_to_msg_id,
746
+ input_message_content: inputMessageContent,
747
+ });
748
+ outputItem.success = true;
749
+ outputItem.result = result;
750
+ }
751
+ catch (apiError) {
752
+ outputItem.errorDetails = {
753
+ message: apiError.message,
754
+ code: apiError.code,
755
+ stack: apiError.stack,
756
+ chat_id: chat_id,
757
+ sendAsVoice: sendAsVoice,
758
+ filePath: audioFilePath,
759
+ fileSize: outputItem.fileSize
760
+ };
761
+ debug('Telegram API error details:', JSON.stringify(outputItem.errorDetails, null, 2));
762
+ if (apiError.message.includes('User restricted receiving of video messages')) {
763
+ outputItem.error = 'The recipient has restricted receiving of voice messages. Please try sending as a regular audio file instead.';
764
+ outputItem.errorType = 'USER_RESTRICTION';
765
+ }
766
+ else if (apiError.message.includes('FILE_REFERENCE_EXPIRED')) {
767
+ outputItem.error = 'File reference has expired. Please try again.';
768
+ outputItem.errorType = 'FILE_REFERENCE_EXPIRED';
769
+ }
770
+ else if (apiError.message.includes('FILE_ID_INVALID')) {
771
+ outputItem.error = 'Invalid file ID. Please check the file and try again.';
772
+ outputItem.errorType = 'FILE_ID_INVALID';
773
+ }
774
+ else if (apiError.message.includes('CHAT_WRITE_FORBIDDEN')) {
775
+ outputItem.error = 'Cannot send messages to this chat. You may not have permission.';
776
+ outputItem.errorType = 'CHAT_WRITE_FORBIDDEN';
777
+ }
778
+ else {
779
+ outputItem.error = `Telegram API error: ${apiError.message}`;
780
+ outputItem.errorType = 'UNKNOWN_ERROR';
781
+ }
782
+ }
783
+ }
784
+ }
785
+ catch (generalError) {
786
+ outputItem.error = `General error: ${generalError.message}`;
787
+ }
788
+ returnData.push(outputItem);
789
+ }
790
+ else if (operation === 'sendMessageFile') {
791
+ const chat_id = this.getNodeParameter('chat_id', 0);
792
+ const filePath = this.getNodeParameter('filePath', 0);
793
+ let fileCaption = this.getNodeParameter('fileCaption', 0);
794
+ const reply_to_msg_id = this.getNodeParameter('reply_to_msg_id', 0);
795
+ const message_thread_id = this.getNodeParameter('message_thread_id', 0);
796
+ if (fileCaption === '' && fileCaption.length == 0) {
797
+ fileCaption = null;
798
+ }
799
+ const result = await client.invoke({
800
+ _: 'sendMessage',
801
+ chat_id,
802
+ reply_to_msg_id,
803
+ message_thread_id,
804
+ input_message_content: {
805
+ _: 'inputMessageDocument',
806
+ document: {
807
+ _: 'inputFileLocal',
808
+ path: filePath,
809
+ },
810
+ caption: {
811
+ _: 'formattedText',
812
+ text: fileCaption,
813
+ },
814
+ },
815
+ });
816
+ returnData.push(result);
817
+ }
818
+ else if (operation === 'sendMessagePhoto') {
819
+ const chat_id = this.getNodeParameter('chat_id', 0);
820
+ const localFilePath = this.getNodeParameter('localFilePath', 0);
821
+ let photoCaption = this.getNodeParameter('photoCaption', 0);
822
+ const reply_to_msg_id = this.getNodeParameter('reply_to_msg_id', 0);
823
+ const message_thread_id = this.getNodeParameter('message_thread_id', 0);
824
+ if (photoCaption === '' && photoCaption.length == 0) {
825
+ photoCaption = null;
826
+ }
827
+ const result = await client.invoke({
828
+ _: 'sendMessage',
829
+ chat_id,
830
+ reply_to_msg_id,
831
+ message_thread_id,
832
+ input_message_content: {
833
+ _: 'inputMessagePhoto',
834
+ photo: {
835
+ _: 'inputFileLocal',
836
+ path: localFilePath,
837
+ },
838
+ caption: {
839
+ _: 'formattedText',
840
+ text: photoCaption,
841
+ },
842
+ },
843
+ });
844
+ returnData.push(result);
845
+ }
846
+ else if (operation === 'editMessageText') {
847
+ const chat_id = this.getNodeParameter('chat_id', 0);
848
+ const message_id = this.getNodeParameter('message_id', 0);
849
+ const messageText = this.getNodeParameter('messageText', 0);
850
+ const result = await client.invoke({
851
+ _: 'editMessageText',
852
+ chat_id,
853
+ message_id,
854
+ input_message_content: {
855
+ _: 'inputMessageText',
856
+ text: {
857
+ _: 'formattedText',
858
+ text: messageText,
859
+ },
860
+ },
861
+ });
862
+ returnData.push(result);
863
+ }
864
+ else if (operation === 'deleteMessages') {
865
+ const chat_id = this.getNodeParameter('chat_id', 0);
866
+ const message_ids = this.getNodeParameter('message_ids', 0);
867
+ const revoke = this.getNodeParameter('revoke', 0);
868
+ const idsArray = message_ids
869
+ .toString()
870
+ .split(',')
871
+ .map((s) => s.toString().trim());
872
+ const result = await client.invoke({
873
+ _: 'deleteMessages',
874
+ chat_id,
875
+ message_ids: idsArray,
876
+ revoke,
877
+ });
878
+ returnData.push(result);
879
+ }
880
+ else if (operation === 'forwardMessages') {
881
+ const chat_id = this.getNodeParameter('chat_id', 0);
882
+ const from_chat_id = this.getNodeParameter('from_chat_id', 0);
883
+ const message_ids = this.getNodeParameter('message_ids', 0);
884
+ const message_thread_id = this.getNodeParameter('message_thread_id', 0);
885
+ const idsArray = message_ids
886
+ .toString()
887
+ .split(',')
888
+ .map((s) => s.toString().trim())
889
+ .filter((s) => s.length > 0);
890
+ const result = await client.invoke({
891
+ _: 'forwardMessages',
892
+ chat_id,
893
+ from_chat_id,
894
+ message_ids: idsArray,
895
+ message_thread_id,
896
+ });
897
+ returnData.push(result);
898
+ }
899
+ }
900
+ else if (resource === 'group') {
901
+ if (operation === 'getSupergroup') {
902
+ const supergroup_id = this.getNodeParameter('supergroup_id', 0);
903
+ result = await client.invoke({
904
+ _: 'getSupergroup',
905
+ supergroup_id,
906
+ });
907
+ returnData.push(result);
908
+ }
909
+ else if (operation === 'getSupergroupFullInfo') {
910
+ const supergroup_id = this.getNodeParameter('supergroup_id', 0);
911
+ result = await client.invoke({
912
+ _: 'getSupergroupFullInfo',
913
+ supergroup_id,
914
+ });
915
+ returnData.push(result);
916
+ }
917
+ }
918
+ else if (resource === 'request') {
919
+ if (operation === 'customRequest') {
920
+ const jsonString = this.getNodeParameter('request_json', 0);
921
+ const obj = JSON.parse(jsonString);
922
+ debug(`Request JSON is : ${jsonString}`);
923
+ result = await client.invoke(obj);
924
+ returnData.push(result);
925
+ }
926
+ }
927
+ }
928
+ catch (e) {
929
+ if (e.message === "A closed client cannot be reused, create a new Client") {
930
+ cM.markClientAsClosed(credentials === null || credentials === void 0 ? void 0 : credentials.apiId);
931
+ if (this.continueOnFail()) {
932
+ returnData.push({ json: { message: e.message, error: e } });
933
+ }
934
+ else {
935
+ throw new Error("Session was closed or terminated. Please login again: https://telepilot.co/login-howto");
936
+ }
937
+ }
938
+ else if (e.message === "Unauthorized") {
939
+ cM.markClientAsClosed(credentials === null || credentials === void 0 ? void 0 : credentials.apiId);
940
+ if (this.continueOnFail()) {
941
+ returnData.push({ json: { message: e.message, error: e } });
942
+ }
943
+ else {
944
+ throw new Error("Please login: https://telepilot.co/login-howto");
945
+ }
946
+ }
947
+ else {
948
+ if (this.continueOnFail()) {
949
+ returnData.push({ json: { message: e.message, error: e } });
950
+ }
951
+ else {
952
+ throw e;
953
+ }
954
+ }
955
+ }
956
+ return [this.helpers.returnJsonArray(returnData)];
957
+ }
958
+ }
959
+ exports.TelePilot = TelePilot;
960
+ //# sourceMappingURL=TelePilot.node.js.map