iobroker.telegram 1.16.0 → 2.0.0

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 (28) hide show
  1. package/README.md +9 -5
  2. package/admin/custom/customComponents.js +3 -3
  3. package/admin/custom/customComponents.js.map +1 -1
  4. package/admin/custom/static/js/main.1ee6620f.js +5 -0
  5. package/admin/custom/static/js/main.1ee6620f.js.map +1 -0
  6. package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23730.49b95cc9.chunk.js.map +1 -1
  7. package/admin/custom/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-de23731.ae65be75.chunk.js.map +1 -1
  8. package/admin/custom/static/js/node_modules_prop-types_index_js.bf62a0c5.chunk.js.map +1 -1
  9. package/admin/custom/static/js/node_modules_react_index_js.44097ea5.chunk.js.map +1 -1
  10. package/admin/custom/static/js/src_Components_jsx.a3ebac36.chunk.js.map +1 -1
  11. package/admin/custom/static/js/src_bootstrap_jsx.8f9c256e.chunk.js.map +1 -1
  12. package/admin/i18n/en.json +2 -2
  13. package/admin/rules/asset-manifest.json +7 -7
  14. package/admin/rules/customRuleBlocks.js +3 -3
  15. package/admin/rules/customRuleBlocks.js.map +1 -1
  16. package/admin/rules/static/js/main.c65f0e85.js +5 -0
  17. package/admin/rules/static/js/main.c65f0e85.js.map +1 -0
  18. package/admin/rules/static/js/node_modules_iobroker_adapter-react-v5_assets_devices_sync_recursive_-node_modules_iobroker_a-3fbe7d.46a44060.chunk.js.map +1 -1
  19. package/admin/rules/static/js/node_modules_prop-types_index_js.4a6f1268.chunk.js.map +1 -1
  20. package/admin/rules/static/js/node_modules_react_index_js.fbeb147f.chunk.js.map +1 -1
  21. package/admin/rules/static/js/src_ActionTelegram_jsx.96d464bf.chunk.js.map +1 -1
  22. package/io-package.json +14 -13
  23. package/main.js +223 -429
  24. package/package.json +24 -19
  25. package/admin/custom/static/js/main.bd70094f.js +0 -5
  26. package/admin/custom/static/js/main.bd70094f.js.map +0 -1
  27. package/admin/rules/static/js/main.7e3dd3e3.js +0 -5
  28. package/admin/rules/static/js/main.7e3dd3e3.js.map +0 -1
package/main.js CHANGED
@@ -48,7 +48,7 @@ let tmpDirName;
48
48
  const server = {
49
49
  app: null,
50
50
  server: null,
51
- settings: null
51
+ settings: null,
52
52
  };
53
53
 
54
54
  let adapter;
@@ -63,7 +63,7 @@ const systemLang2CallMe = {
63
63
  it: 'it-IT-Standard-A',
64
64
  es: 'es-ES-Standard-A',
65
65
  pl: 'pl-PL-Standard-A',
66
- 'zh-cn': 'en-GB-Standard-A'
66
+ 'zh-cn': 'en-GB-Standard-A',
67
67
  };
68
68
 
69
69
  function startAdapter(options) {
@@ -236,13 +236,13 @@ function startAdapter(options) {
236
236
  adapter.log.info(`https server listening on port ${port}`);
237
237
 
238
238
  main()
239
- .then(() => {});
239
+ .catch(e => adapter.log.error(`Cannot start adapter: ${e}`));
240
240
  });
241
241
  }
242
242
  });
243
243
  } else {
244
244
  main()
245
- .then(() => {});
245
+ .catch(e => adapter.log.error(`Cannot start adapter: ${e}`));
246
246
  }
247
247
  });
248
248
 
@@ -293,35 +293,35 @@ function startAdapter(options) {
293
293
  sendMessage(state.val)
294
294
  .then(data => adapter.setState('communicate.response', state.val, true));
295
295
  } else
296
- if (id.endsWith('communicate.responseSilent')) {
297
- if (typeof state.val === 'object') {
298
- adapter.log.error(`communicate.responseSilent only supports passing a message to send as string. You provided ${JSON.stringify(state.val)}. Please use "communicate.responseSilentJson" instead with a stringified JSON object!`);
299
- return;
300
- }
301
- // Send to someone this message
302
- sendMessage(state.val, null, null, {disable_notification: true})
303
- .then(data => adapter.setState('communicate.responseSilent', state.val, true));
304
- } else
305
- if (id.endsWith('communicate.responseJson')) {
306
- try {
307
- const val = JSON.parse(state.val);
308
- // Send to someone this message
309
- sendMessage(val)
310
- .then(data => adapter.setState('communicate.responseJson', state.val, true));
311
- } catch (err) {
312
- adapter.log.error(`could not parse Json in communicate.responseJon state: ${err.message}`);
313
- }
314
- } else
315
- if (id.endsWith('communicate.responseSilentJson')) {
316
- try {
317
- const val = JSON.parse(state.val);
296
+ if (id.endsWith('communicate.responseSilent')) {
297
+ if (typeof state.val === 'object') {
298
+ adapter.log.error(`communicate.responseSilent only supports passing a message to send as string. You provided ${JSON.stringify(state.val)}. Please use "communicate.responseSilentJson" instead with a stringified JSON object!`);
299
+ return;
300
+ }
318
301
  // Send to someone this message
319
- sendMessage(val, null, null, {disable_notification: true})
302
+ sendMessage(state.val, null, null, {disable_notification: true})
320
303
  .then(data => adapter.setState('communicate.responseSilent', state.val, true));
321
- } catch (err) {
322
- adapter.log.error(`could not parse Json in communicate.responseSilentJon state: ${err.message}`);
323
- }
324
- }
304
+ } else
305
+ if (id.endsWith('communicate.responseJson')) {
306
+ try {
307
+ const val = JSON.parse(state.val);
308
+ // Send to someone this message
309
+ sendMessage(val)
310
+ .then(data => adapter.setState('communicate.responseJson', state.val, true));
311
+ } catch (err) {
312
+ adapter.log.error(`could not parse Json in communicate.responseJon state: ${err.message}`);
313
+ }
314
+ } else
315
+ if (id.endsWith('communicate.responseSilentJson')) {
316
+ try {
317
+ const val = JSON.parse(state.val);
318
+ // Send to someone this message
319
+ sendMessage(val, null, null, {disable_notification: true})
320
+ .then(data => adapter.setState('communicate.responseSilent', state.val, true));
321
+ } catch (err) {
322
+ adapter.log.error(`could not parse Json in communicate.responseSilentJon state: ${err.message}`);
323
+ }
324
+ }
325
325
  } else {
326
326
  if (commands[id] && commands[id].report) {
327
327
  if (commands[id].reportChanges) {
@@ -463,7 +463,7 @@ function getName(obj) {
463
463
  }
464
464
 
465
465
  const actions = [
466
- 'typing', 'upload_photo', 'upload_video', 'record_video', 'record_audio', 'upload_document', 'find_location'
466
+ 'typing', 'upload_photo', 'upload_video', 'record_video', 'record_audio', 'upload_document', 'find_location',
467
467
  ];
468
468
 
469
469
  function handleWebHook(req, res) {
@@ -512,7 +512,7 @@ function handleWebHook(req, res) {
512
512
  });
513
513
  } else {
514
514
  res.writeHead(404, 'Resource Not Found', {
515
- 'Content-Type': 'text/html'
515
+ 'Content-Type': 'text/html',
516
516
  });
517
517
  res.end('<!doctype html><html><head><title>404</title></head><body>404: Resource Not Found</body></html>');
518
518
  }
@@ -539,85 +539,69 @@ function saveSendRequest(msg) {
539
539
 
540
540
  function _sendMessageHelper(dest, name, text, options) {
541
541
  return new Promise((resolve, reject) => {
542
- let count = 0;
542
+ const messageIds = {};
543
543
  if (options && options.chatId !== undefined && options.user === undefined) {
544
544
  options.user = adapter.config.useUsername ? users[options.chatId].userName : users[options.chatId].firstName;
545
545
  }
546
546
 
547
547
  if (options && options.editMessageReplyMarkup !== undefined) {
548
548
  adapter.log.debug(`Send editMessageReplyMarkup to "${name}"`);
549
- bot && bot.editMessageReplyMarkup(options.editMessageReplyMarkup.reply_markup, options.editMessageReplyMarkup.options)
550
- .then(response => saveSendRequest(response))
551
- .then(() => {
552
- options = null;
553
- count++;
554
- resolve(count);
555
- })
556
- .catch(error => {
557
- if (options.chatId) {
558
- adapter.log.error(`Cannot send editMessageReplyMarkup [chatId - ${options.chatId}]: ${error}`);
559
- } else {
560
- adapter.log.error(`Cannot send editMessageReplyMarkup [user - ${options.user}]: ${error}`);
561
- }
562
- options = null;
563
- resolve(count);
564
- });
549
+ bot && executeSending(() => bot.editMessageReplyMarkup(options.editMessageReplyMarkup.reply_markup, options.editMessageReplyMarkup.options), options, resolve);
565
550
  } else if (options && options.editMessageText !== undefined) {
566
551
  adapter.log.debug(`Send editMessageText to "${name}"`);
567
- bot && bot.editMessageText(text, options.editMessageText.options)
568
- .then(response => saveSendRequest(response))
569
- .then(() => {
570
- options = null;
571
- count++;
572
- resolve(count);
573
- })
574
- .catch(error => {
575
- if (options.chatId) {
576
- adapter.log.error(`Cannot send editMessageText [chatId - ${options.chatId}]: ${error}`);
577
- } else {
578
- adapter.log.error(`Cannot send editMessageText [user - ${options.user}]: ${error}`);
579
- }
580
- options = null;
581
- resolve(count);
582
- });
552
+ bot && executeSending(() => bot.editMessageText(text, options.editMessageText.options), options, resolve);
583
553
  } else if (options && options.editMessageMedia !== undefined) {
584
554
  adapter.log.debug(`Send editMessageMedia to "${name}"`);
585
555
  if (text) {
586
556
  let mediaInput;
587
- if ((typeof text === 'string' && text.match(/\.(jpg|png|jpeg|bmp|gif)$/i) && (fs.existsSync(text) || text.match(/^(https|http)/i))) || (options && options.type === 'photo')) {
557
+ if (
558
+ (typeof text === 'string' &&
559
+ text.match(/\.(jpg|png|jpeg|bmp|gif)$/i) &&
560
+ (fs.existsSync(text) || text.match(/^(https|http)/i))
561
+ ) ||
562
+ (options && options.type === 'photo')
563
+ ) {
588
564
  mediaInput = {
589
565
  type: 'photo',
590
- media: text
566
+ media: text,
591
567
  };
592
- } else
593
- if ((typeof text === 'string' && text.match(/\.(gif)/i) && fs.existsSync(text)) || (options && options.type === 'animation')) {
568
+ } else if (
569
+ (typeof text === 'string' && text.match(/\.(gif)/i) && fs.existsSync(text)) ||
570
+ (options && options.type === 'animation')
571
+ ) {
594
572
  mediaInput = {
595
573
  type: 'animation',
596
- media: text
574
+ media: text,
597
575
  };
598
- } else
599
- if ((typeof text === 'string' && text.match(/\.(mp4)$/i) && fs.existsSync(text)) || (options && options.type === 'video')) {
576
+ } else if (
577
+ (typeof text === 'string' && text.match(/\.(mp4)$/i) && fs.existsSync(text)) ||
578
+ (options && options.type === 'video')
579
+ ) {
600
580
  mediaInput = {
601
581
  type: 'video',
602
- media: text
582
+ media: text,
603
583
  };
604
- } else
605
- if ((typeof text === 'string' && text.match(/\.(wav|mp3|ogg)$/i) && fs.existsSync(text)) || (options && options.type === 'audio')) {
584
+ } else if (
585
+ (typeof text === 'string' && text.match(/\.(wav|mp3|ogg)$/i) && fs.existsSync(text)) ||
586
+ (options && options.type === 'audio')
587
+ ) {
606
588
  mediaInput = {
607
589
  type: 'audio',
608
- media: text
590
+ media: text,
609
591
  };
610
- } else
611
- if ((typeof text === 'string' && text.match(/\.(txt|doc|docx|csv|pdf|xls|xlsx)$/i) && fs.existsSync(text)) || (options && options.type === 'document')) {
592
+ } else if (
593
+ (typeof text === 'string' && text.match(/\.(txt|doc|docx|csv|pdf|xls|xlsx)$/i) && fs.existsSync(text)) ||
594
+ (options && options.type === 'document')
595
+ ) {
612
596
  mediaInput = {
613
597
  type: 'document',
614
- media: text
598
+ media: text,
615
599
  };
616
600
  }
617
601
 
618
602
  if (mediaInput) {
619
603
  const opts = {
620
- qs: options.editMessageMedia.options,
604
+ qs: options.editMessageMedia.options,
621
605
  };
622
606
  opts.formData = {};
623
607
 
@@ -640,376 +624,188 @@ function _sendMessageHelper(dest, name, text, options) {
640
624
  }
641
625
 
642
626
  opts.qs.media = JSON.stringify(payload);
643
- bot && bot._request('editMessageMedia', opts)
644
- .then(response => saveSendRequest(response))
645
- .then(() => {
646
- options = null;
647
- count++;
648
- resolve(count);
649
- })
650
- .catch(error => {
651
- if (options.chatId) {
652
- adapter.log.error(`Cannot send editMessageMedia [chatId - ${options.chatId}]: ${error}`);
653
- } else {
654
- adapter.log.error(`Cannot send editMessageMedia [user - ${options.user}]: ${error}`);
655
- }
656
- options = null;
657
- resolve(count);
658
- });
627
+ bot && executeSending(() => bot._request('editMessageMedia', opts), options, resolve);
659
628
  } else {
660
629
  adapter.log.error(`Cannot send editMessageMedia [chatId - ${options.chatId}]: unsupported media type`);
661
630
  options = null;
662
- resolve(count);
631
+ resolve(JSON.stringify(messageIds));
663
632
  }
664
633
  } else {
665
634
  adapter.log.error(`Cannot send editMessageMedia [chatId - ${options.chatId}]: no media found. "text" may not be empty`);
666
635
  options = null;
667
- resolve(count);
636
+ resolve(JSON.stringify(messageIds));
668
637
  }
669
638
  } else if (options && options.editMessageCaption !== undefined) {
670
639
  adapter.log.debug(`Send editMessageCaption to "${name}"`);
671
- bot && bot.editMessageCaption(text, options.editMessageCaption.options)
672
- .then(response => saveSendRequest(response))
673
- .then(() => {
674
- options = null;
675
- count++;
676
- resolve(count);
677
- })
678
- .catch(error => {
679
- if (options.chatId) {
680
- adapter.log.error(`Cannot send editMessageCaption [chatId - ${options.chatId}]: ${error}`);
681
- } else {
682
- adapter.log.error(`Cannot send editMessageCaption [user - ${options.user}]: ${error}`);
683
- }
684
- options = null;
685
- resolve(count);
686
- });
640
+ bot && executeSending(() => bot.editMessageCaption(text, options.editMessageCaption.options), options, resolve);
687
641
  } else if (options && options.deleteMessage !== undefined) {
688
642
  adapter.log.debug(`Send deleteMessage to "${name}"`);
689
- bot && bot.deleteMessage(options.deleteMessage.options.chat_id, options.deleteMessage.options.message_id)
690
- .then(response => saveSendRequest(response))
691
- .then(() => {
692
- options = null;
693
- count++;
694
- resolve(count);
695
- })
696
- .catch(error => {
697
- if (options.chatId) {
698
- adapter.log.error(`Cannot send deleteMessage [chatId - ${options.chatId}]: ${error}`);
699
- } else {
700
- adapter.log.error(`Cannot send deleteMessage [user - ${options.user}]: ${error}`);
701
- }
702
- options = null;
703
- resolve(count);
704
- });
643
+ bot && executeSending(() => bot.deleteMessage(options.deleteMessage.options.chat_id, options.deleteMessage.options.message_id), options, resolve);
705
644
  } else
706
- if (options && options.latitude !== undefined) {
707
- adapter.log.debug(`Send location to "${name}": ${text}`);
708
- if (bot) {
709
- bot.sendLocation(dest, parseFloat(options.latitude), parseFloat(options.longitude), options)
710
- .then(response => saveSendRequest(response))
711
- .then(() => {
712
- adapter.log.debug('Location sent');
713
- options = null;
714
- count++;
715
- resolve(count);
716
- })
717
- .catch(error => {
718
- if (options.chatId) {
719
- adapter.log.error(`Cannot send location [chatId - ${options.chatId}]: ${error}`);
720
- } else {
721
- adapter.log.error(`Cannot send location [user - ${options.user}]: ${error}`);
722
- }
723
- options = null;
724
- reject(error);
725
- });
726
- }
727
- } else if (options && options.type === 'mediagroup') {
728
- adapter.log.debug(`Send media group to "${name}": `);
729
- if (bot) {
730
- const {media: fileNames} = options;
731
- if (fileNames instanceof Array) {
732
- bot.sendChatAction(dest, 'upload_photo')
733
- .then(param => {
734
- if (fileNames.every(name => fs.existsSync(name))) {
735
- const filesAsArray = fileNames
736
- .map(element => {
737
- try {
738
- return {type: 'photo', media: fs.readFileSync(element)};
739
- } catch (error) {
740
- adapter.log.error(`Cannot read file${element}`);
741
- return undefined;
742
- }
743
- })
744
- .filter(element => element !== undefined);
745
-
746
- const size = filesAsArray
747
- .map(element => element.media.length)
748
- .reduce((acc, val) => acc + val);
749
-
750
- adapter.log.info(`Send media group to "${name}": ${size} bytes`);
751
- if (filesAsArray.length > 0) {
752
- bot.sendMediaGroup(dest, filesAsArray)
753
- .then((response) => saveSendRequest(response))
754
- .then(() => {
755
- adapter.log.debug('photos sent');
756
- count++;
757
- resolve(count);
758
- options = null;
759
- })
760
- .catch(error => {
761
- if (options.chatId) {
762
- adapter.log.error(`Cannot send media group [chatId - ${options.chatId}]: ${error}`);
763
- } else {
764
- adapter.log.error(`Cannot send media group [user - ${options.user}]: ${error}`);
645
+ if (options && options.latitude !== undefined) {
646
+ adapter.log.debug(`Send location to "${name}": ${text}`);
647
+ bot && executeSending(() => bot.sendLocation(dest, parseFloat(options.latitude), parseFloat(options.longitude), options), options, resolve);
648
+ } else if (options && options.type === 'mediagroup') {
649
+ adapter.log.debug(`Send media group to "${name}": `);
650
+ if (bot) {
651
+ const {media: fileNames} = options;
652
+ if (fileNames instanceof Array) {
653
+ bot.sendChatAction(dest, 'upload_photo')
654
+ .then(param => {
655
+ if (fileNames.every(name => fs.existsSync(name))) {
656
+ const filesAsArray = fileNames
657
+ .map(element => {
658
+ try {
659
+ return {type: 'photo', media: fs.readFileSync(element)};
660
+ } catch (error) {
661
+ adapter.log.error(`Cannot read file${element}`);
662
+ return undefined;
765
663
  }
766
- options = null;
767
- resolve(count);
768
- });
664
+ })
665
+ .filter(element => element !== undefined);
666
+
667
+ const size = filesAsArray
668
+ .map(element => element.media.length)
669
+ .reduce((acc, val) => acc + val);
670
+
671
+ adapter.log.info(`Send media group to "${name}": ${size} bytes`);
672
+ if (filesAsArray.length > 0) {
673
+ executeSending(() => bot.sendMediaGroup(dest, filesAsArray), options, resolve);
674
+ }
675
+ } else {
676
+ adapter.log.debug('files must exists');
677
+ options = null;
678
+ resolve(JSON.stringify(messageIds));
769
679
  }
770
- } else {
771
- adapter.log.debug('files must exists');
772
- options = null;
773
- resolve(count);
774
- }
775
- })
776
- .catch(error => {
777
- adapter.log.error(`upload Error:${error}`);
778
- });
779
- } else {
780
- adapter.log.debug('option media should be an array');
781
- resolve(count);
782
- }
783
- } else {
784
- adapter.log.debug('no files added!');
785
- options = null;
786
- resolve(count);
787
- }
788
- } else if (text && typeof text === 'string' && actions.includes(text)) {
789
- adapter.log.debug(`Send action to "${name}": ${text}`);
790
- bot && bot.sendChatAction(dest, text)
791
- .then(response => saveSendRequest(response))
792
- .then(() => {
793
- adapter.log.debug('Action sent');
794
- options = null;
795
- count++;
796
- resolve(count);
797
- })
798
- .catch(error => {
799
- if (options.chatId) {
800
- adapter.log.error(`Cannot send action [chatId - ${options.chatId}]: ${error}`);
801
- } else {
802
- adapter.log.error(`Cannot send action [user - ${options.user}]: ${error}`);
803
- }
804
- options = null;
805
- resolve(count);
806
- });
807
- } else if (text && ((typeof text === 'string' && text.match(/\.webp$/i) && fs.existsSync(text)) || (options && options.type === 'sticker'))) {
808
- if (typeof text === 'string') {
809
- adapter.log.debug(`Send sticker to "${name}": ${text}`);
810
- } else {
811
- adapter.log.debug(`Send sticker to "${name}": ${text.length} bytes`);
812
- }
813
- bot && bot.sendSticker(dest, text, options)
814
- .then(response => saveSendRequest(response))
815
- .then(() => {
816
- adapter.log.debug('Sticker sent');
817
- count++;
818
- resolve(count);
819
- options = null;
820
- })
821
- .catch(error => {
822
- if (options.chatId) {
823
- adapter.log.error(`Cannot send sticker [chatId - ${options.chatId}]: ${error}`);
824
- } else {
825
- adapter.log.error(`Cannot send sticker [user - ${options.user}]: ${error}`);
826
- }
827
- options = null;
828
- resolve(count);
829
- });
830
- } else if (text && ((typeof text === 'string' && text.match(/\.(gif)/i) && fs.existsSync(text)) || (options && options.type === 'animation'))) {
831
- if (typeof text === 'string') {
832
- adapter.log.debug(`Send animation to "${name}": ${text}`);
833
- } else {
834
- adapter.log.debug(`Send animation to "${name}": ${text.length} bytes`);
835
- }
836
- bot && bot.sendAnimation(dest, text, options)
837
- .then(response => saveSendRequest(response))
838
- .then(() => {
839
- adapter.log.debug('animation sent');
840
- options = null;
841
- count++;
842
- resolve(count);
843
- })
844
- .catch(error => {
845
- if (options.chatId) {
846
- adapter.log.error(`Cannot send animation [chatId - ${options.chatId}]: ${error}`);
847
- } else {
848
- adapter.log.error(`Cannot send animation [user - ${options.user}]: ${error}`);
849
- }
850
- options = null;
851
- resolve(count);
852
- });
853
- } else if (text && ((typeof text === 'string' && text.match(/\.(mp4)$/i) && fs.existsSync(text)) || (options && options.type === 'video'))) {
854
- if (typeof text === 'string') {
855
- adapter.log.debug(`Send video to "${name}": ${text}`);
856
- } else {
857
- adapter.log.debug(`Send video to "${name}": ${text.length} bytes`);
858
- }
859
- bot && bot.sendVideo(dest, text, options)
860
- .then(response => saveSendRequest(response))
861
- .then(() => {
862
- adapter.log.debug('Video sent');
863
- count++;
864
- resolve(count);
865
- options = null;
866
- })
867
- .catch(error => {
868
- if (options.chatId) {
869
- adapter.log.error(`Cannot send video [chatId - ${options.chatId}]: ${error}`);
870
- } else {
871
- adapter.log.error(`Cannot send video [user - ${options.user}]: ${error}`);
872
- }
873
- options = null;
874
- resolve(count);
875
- });
876
- } else if (text && ((typeof text === 'string' && text.match(/\.(txt|doc|docx|csv|pdf|xls|xlsx)$/i) && fs.existsSync(text)) || (options && options.type === 'document'))) {
877
- if (typeof text === 'string') {
878
- adapter.log.debug(`Send document to "${name}": ${text}`);
879
- } else {
880
- adapter.log.debug(`Send document to "${name}": ${text.length} bytes`);
881
- }
882
- bot && bot.sendDocument(dest, text, options)
883
- .then(response => saveSendRequest(response))
884
- .then(() => {
885
- adapter.log.debug('Document sent');
886
- options = null;
887
- count++;
888
- resolve(count);
889
- })
890
- .catch(error => {
891
- if (options.chatId) {
892
- adapter.log.error(`Cannot send document [chatId - ${options.chatId}]: ${error}`);
893
- } else {
894
- adapter.log.error(`Cannot send document [user - ${options.user}]: ${error}`);
895
- }
896
- options = null;
897
- resolve(count);
898
- });
899
- } else if (text && ((typeof text === 'string' && text.match(/\.(wav|mp3|ogg)$/i) && fs.existsSync(text)) || (options && options.type === 'audio'))) {
900
- if (typeof text === 'string') {
901
- adapter.log.debug(`Send audio to "${name}": ${text}`);
902
- } else {
903
- adapter.log.debug(`Send audio to "${name}": ${text.length} bytes`);
904
- }
905
- bot && bot.sendAudio(dest, text, options)
906
- .then(response => saveSendRequest(response))
907
- .then(() => {
908
- adapter.log.debug('Audio sent');
909
- options = null;
910
- count++;
911
- resolve(count);
912
- })
913
- .catch(error => {
914
- if (options.chatId) {
915
- adapter.log.error(`Cannot send audio [chatId - ${options.chatId}]: ${error}`);
680
+ })
681
+ .catch(error => {
682
+ adapter.log.error(`upload Error:${error}`);
683
+ });
916
684
  } else {
917
- adapter.log.error(`Cannot send audio [user - ${options.user}]: ${error}`);
685
+ adapter.log.debug('option media should be an array');
686
+ resolve(JSON.stringify(messageIds));
918
687
  }
688
+ } else {
689
+ adapter.log.debug('no files added!');
919
690
  options = null;
920
- resolve(count);
921
- });
922
- } else if (text && ((typeof text === 'string' && text.match(/\.(jpg|png|jpeg|bmp|gif)$/i) && (fs.existsSync(text) || text.match(/^(https|http)/i))) || (options && options.type === 'photo'))) {
923
- if (typeof text === 'string') {
924
- adapter.log.debug(`Send photo to "${name}": ${text}`);
691
+ resolve(JSON.stringify(messageIds));
692
+ }
693
+ } else if (text && typeof text === 'string' && actions.includes(text)) {
694
+ adapter.log.debug(`Send action to "${name}": ${text}`);
695
+ bot && executeSending(() => bot.sendChatAction(dest, text), options, resolve);
696
+ } else if (text && ((typeof text === 'string' && text.match(/\.webp$/i) && fs.existsSync(text)) || (options && options.type === 'sticker'))) {
697
+ if (typeof text === 'string') {
698
+ adapter.log.debug(`Send sticker to "${name}": ${text}`);
699
+ } else {
700
+ adapter.log.debug(`Send sticker to "${name}": ${text.length} bytes`);
701
+ }
702
+ bot && executeSending(() => bot.sendSticker(dest, text, options), options, resolve);
703
+ } else if (text && ((typeof text === 'string' && text.match(/\.(gif)/i) && fs.existsSync(text)) || (options && options.type === 'animation'))) {
704
+ if (typeof text === 'string') {
705
+ adapter.log.debug(`Send animation to "${name}": ${text}`);
706
+ } else {
707
+ adapter.log.debug(`Send animation to "${name}": ${text.length} bytes`);
708
+ }
709
+ bot && executeSending(() => bot.sendAnimation(dest, text, options), options, resolve);
710
+ } else if (text && ((typeof text === 'string' && text.match(/\.(mp4)$/i) && fs.existsSync(text)) || (options && options.type === 'video'))) {
711
+ if (typeof text === 'string') {
712
+ adapter.log.debug(`Send video to "${name}": ${text}`);
713
+ } else {
714
+ adapter.log.debug(`Send video to "${name}": ${text.length} bytes`);
715
+ }
716
+ bot && executeSending(() => bot.sendVideo(dest, text, options), options, resolve);
717
+ } else if (text && ((typeof text === 'string' && text.match(/\.(txt|doc|docx|csv|pdf|xls|xlsx)$/i) && fs.existsSync(text)) || (options && options.type === 'document'))) {
718
+ adapter.log.debug(`Send document to "${name}": ${(typeof text === 'string') ? text : text.length}`);
719
+ bot && executeSending(() => bot.sendDocument(dest, text, options), options, resolve);
720
+ } else if (
721
+ text &&
722
+ (
723
+ (typeof text === 'string' &&
724
+ text.match(/\.(wav|mp3|ogg)$/i) &&
725
+ fs.existsSync(text)
726
+ ) ||
727
+ (options && options?.type === 'audio')
728
+ )
729
+ ) {
730
+ adapter.log.debug(`Send audio to "${name}": ${(typeof text === 'string') ? text : text.length}`);
731
+
732
+ bot && executeSending(() => bot.sendAudio(dest, text, options), options, resolve);
733
+
734
+ } else if (
735
+ text &&
736
+ (
737
+ (
738
+ typeof text === 'string' && // if the message is a string, and it is a path to file or URL
739
+ text.match(/\.(jpg|png|jpeg|bmp|gif)$/i) &&
740
+ (fs.existsSync(text) || text.match(/^(https|http)/i))
741
+ ) ||
742
+ (options && options.type === 'photo') // if the type of message is photo
743
+ )
744
+ ) {
745
+ adapter.log.debug(`Send photo to "${name}": ${(typeof text === 'string') ? text : text.length}`);
746
+
747
+ bot && executeSending(() => bot.sendPhoto(dest, text, options), options, resolve);
748
+ } else if (options && options.answerCallbackQuery !== undefined) {
749
+ adapter.log.debug(`Send answerCallbackQuery to "${name}"`);
750
+ if (options.answerCallbackQuery.showAlert === undefined) {
751
+ options.answerCallbackQuery.showAlert = false;
752
+ }
753
+ if (bot && callbackQueryId[options.chatId]) {
754
+ const originalChatId = callbackQueryId[options.chatId].id;
755
+ delete callbackQueryId[options.chatId];
756
+ executeSending(() => bot.answerCallbackQuery(originalChatId, options.answerCallbackQuery.text, options.answerCallbackQuery.showAlert), options, resolve);
757
+ }
925
758
  } else {
926
- adapter.log.debug(`Send photo to "${name}": ${text.length} bytes`);
927
- }
928
- bot && bot.sendPhoto(dest, text, options)
929
- .then(response => saveSendRequest(response))
930
- .then(() => {
931
- adapter.log.debug('Photo sent');
932
- options = null;
933
- count++;
934
- resolve(count);
935
- })
936
- .catch(error => {
937
- if (options.chatId) {
938
- adapter.log.error(`Cannot send photo [chatId - ${options.chatId}]: ${error}`);
939
- } else {
940
- adapter.log.error(`Cannot send photo [user - ${options.user}]: ${error}`);
941
- }
942
- options = null;
943
- resolve(count);
944
- });
945
- } else if (options && options.answerCallbackQuery !== undefined) {
946
- adapter.log.debug(`Send answerCallbackQuery to "${name}"`);
947
- if (options.answerCallbackQuery.showAlert === undefined) {
948
- options.answerCallbackQuery.showAlert = false;
949
- }
950
- if (bot && callbackQueryId[options.chatId]) {
951
- const originalChatId = callbackQueryId[options.chatId].id;
952
- delete callbackQueryId[options.chatId];
953
- bot.answerCallbackQuery(originalChatId, options.answerCallbackQuery.text, options.answerCallbackQuery.showAlert)
954
- .then(() => {
955
- options = null;
956
- count++;
957
- resolve(count);
958
- })
959
- .catch(error => {
960
- if (options.chatId) {
961
- adapter.log.error(`Cannot send answerCallbackQuery [chatId - ${options.chatId}]: ${error}`);
962
- } else {
963
- adapter.log.error(`Cannot send answerCallbackQuery [user - ${options.user}]: ${error}`);
964
- }
965
- options = null;
966
- resolve(count);
967
- });
968
- }
969
- } else {
970
- adapter.log.debug(`Send message to [${name}]: "${text}"`);
971
- if (text && typeof text === 'string') {
972
- if (text.startsWith('<MarkdownV2>') && text.endsWith('</MarkdownV2>')) {
973
- options = options || {};
974
- options.parse_mode = 'MarkdownV2';
975
- text = text.substring(12, text.length - 13);
976
- } else if (text.startsWith('<HTML>') && text.endsWith('</HTML>')) {
977
- options = options || {};
978
- options.parse_mode = 'HTML';
979
- text = text.substring(6, text.length - 7);
980
- } else if (text.startsWith('<Markdown>') && text.endsWith('</Markdown>')) {
759
+ adapter.log.debug(`Send message to [${name}]: "${text}"`);
760
+ if (text && typeof text === 'string') {
981
761
  options = options || {};
982
- options.parse_mode = 'Markdown';
983
- text = text.substring(10, text.length - 11);
762
+ if (text.startsWith('<MarkdownV2>') && text.endsWith('</MarkdownV2>')) {
763
+ options.parse_mode = 'MarkdownV2';
764
+ text = text.substring(12, text.length - 13);
765
+ } else if (text.startsWith('<HTML>') && text.endsWith('</HTML>')) {
766
+ options.parse_mode = 'HTML';
767
+ text = text.substring(6, text.length - 7);
768
+ } else if (text.startsWith('<Markdown>') && text.endsWith('</Markdown>')) {
769
+ options.parse_mode = 'Markdown';
770
+ text = text.substring(10, text.length - 11);
771
+ }
984
772
  }
773
+ bot && executeSending(() => bot.sendMessage(dest, text || '', options), options, resolve);
985
774
  }
986
-
987
- bot && bot.sendMessage(dest, text || '', options)
988
- .then(response => saveSendRequest(response))
989
- .then(() => {
990
- adapter.log.debug('Message sent');
991
- options = null;
992
- count++;
993
- resolve(count);
994
- })
995
- .catch(error => {
996
- if (options && options.chatId) {
997
- adapter.log.error(`Cannot send message [chatId - ${options.chatId}]: ${error}`);
998
- } else {
999
- adapter.log.error(`Cannot send message [user - ${options && options.user}]: ${error}`);
1000
- }
1001
- options = null;
1002
- resolve(count);
1003
- });
1004
-
1005
- }
1006
775
  });
1007
776
  }
1008
777
 
778
+ /**
779
+ * executes the given method and handles, what to do next
780
+ */
781
+ function executeSending(action, options, resolve){
782
+ // create an empty object, to store chat id and message id of successfully sent messages
783
+ const messageIds = {};
784
+ action()
785
+ .then(response => {
786
+ // put chat id and message id to the object, that will be returned
787
+ messageIds[options.chat_id] = response.message_id;
788
+ // puts ids to the ioBroker database
789
+ saveSendRequest(response);
790
+ })
791
+ .then(() => {
792
+ adapter.log.debug('Message sent');
793
+ options = null;
794
+ // return all the collected message ids to the callback
795
+ resolve(JSON.stringify(messageIds));
796
+ }).catch(error => {
797
+ // log error to the system
798
+ adapter.log.error(`Failed sending [${options.chatId ? 'chatId' : 'user'} - ${options.chatId ? options.chatId : options.user}]: ${error}`);
799
+ options = null;
800
+ // send the succesfully send messages as callback
801
+ resolve(JSON.stringify(messageIds));
802
+ });
803
+ }
804
+
1009
805
  function sendMessage(text, user, chatId, options) {
1010
806
  if (!text && typeof options !== 'object' && text !== 0 && (!options || !options.latitude)) {
1011
807
  adapter.log.warn('Invalid text: null');
1012
- return Promise.resolve(0);
808
+ return Promise.resolve({});
1013
809
  }
1014
810
 
1015
811
  if (text && typeof text === 'object' && text.text !== undefined && typeof text.text === 'string' && options === undefined) {
@@ -1041,12 +837,10 @@ function sendMessage(text, user, chatId, options) {
1041
837
  if (text && text.startsWith('<MarkdownV2>') && text.endsWith('</MarkdownV2>')) {
1042
838
  options.parse_mode = 'MarkdownV2';
1043
839
  text = text.substring(12, text.length - 13);
1044
- } else
1045
- if (text && text.startsWith('<HTML>') && text.endsWith('</HTML>')) {
840
+ } else if (text && text.startsWith('<HTML>') && text.endsWith('</HTML>')) {
1046
841
  options.parse_mode = 'HTML';
1047
842
  text = text.substring(6, text.length - 7);
1048
- } else
1049
- if (text && text.startsWith('<Markdown>') && text.endsWith('</Markdown>')) {
843
+ } else if (text && text.startsWith('<Markdown>') && text.endsWith('</Markdown>')) {
1050
844
  options.parse_mode = 'Markdown';
1051
845
  text = text.substring(10, text.length - 11);
1052
846
  }
@@ -1940,7 +1734,7 @@ function connect() {
1940
1734
  );
1941
1735
  }
1942
1736
  } catch (e) {
1943
-
1737
+ // looks empty
1944
1738
  }
1945
1739
  }
1946
1740
  // Check connection
@@ -2331,4 +2125,4 @@ if (module && module.parent) {
2331
2125
  } else {
2332
2126
  // or start the instance directly
2333
2127
  startAdapter();
2334
- }
2128
+ }