iobroker.telegram 1.16.0 → 2.0.1

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 (23) hide show
  1. package/README.md +13 -9
  2. package/admin/custom/customComponents.js +3 -3
  3. package/admin/custom/customComponents.js.map +1 -1
  4. package/admin/custom/static/js/main.8a972ef8.js +5 -0
  5. package/admin/custom/static/js/main.8a972ef8.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/src_Components_jsx.a3ebac36.chunk.js.map +1 -1
  9. package/admin/custom/static/js/src_bootstrap_jsx.8f9c256e.chunk.js.map +1 -1
  10. package/admin/i18n/en.json +2 -2
  11. package/admin/rules/asset-manifest.json +5 -5
  12. package/admin/rules/customRuleBlocks.js +3 -3
  13. package/admin/rules/customRuleBlocks.js.map +1 -1
  14. package/admin/rules/static/js/{main.7e3dd3e3.js → main.3029a968.js} +2 -2
  15. package/admin/rules/static/js/main.3029a968.js.map +1 -0
  16. 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
  17. package/admin/rules/static/js/src_ActionTelegram_jsx.96d464bf.chunk.js.map +1 -1
  18. package/io-package.json +27 -25
  19. package/main.js +240 -453
  20. package/package.json +24 -19
  21. package/admin/custom/static/js/main.bd70094f.js +0 -5
  22. package/admin/custom/static/js/main.bd70094f.js.map +0 -1
  23. 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
  }
@@ -520,104 +520,89 @@ function handleWebHook(req, res) {
520
520
 
521
521
  function saveSendRequest(msg) {
522
522
  adapter.log.debug(`Request: ${JSON.stringify(msg)}`);
523
+ if (typeof msg === 'object'){
524
+ if (adapter.config.storeRawRequest) {
525
+ adapter.setState('communicate.botSendRaw', JSON.stringify(msg), true, err =>
526
+ err && adapter.log.error(err));
527
+ }
523
528
 
524
- if (msg && adapter.config.storeRawRequest) {
525
- adapter.setState('communicate.botSendRaw', JSON.stringify(msg), true, err =>
526
- err && adapter.log.error(err));
527
- }
528
-
529
- if (msg && msg.message_id) {
530
- adapter.setState('communicate.botSendMessageId', msg.message_id, true, err =>
531
- err && adapter.log.error(err));
532
- }
529
+ if (msg.message_id) {
530
+ adapter.setState('communicate.botSendMessageId', msg.message_id, true, err =>
531
+ err && adapter.log.error(err));
532
+ }
533
533
 
534
- if (msg && msg.chat && msg.chat.id) {
535
- adapter.setState('communicate.botSendChatId', msg.chat.id.toString(), true, err =>
536
- err && adapter.log.error(err));
534
+ if (msg.chat && msg.chat.id) {
535
+ adapter.setState('communicate.botSendChatId', msg.chat.id.toString(), true, err =>
536
+ err && adapter.log.error(err));
537
+ }
537
538
  }
538
539
  }
539
540
 
540
541
  function _sendMessageHelper(dest, name, text, options) {
541
- return new Promise((resolve, reject) => {
542
- let count = 0;
542
+ return new Promise((resolve) => {
543
+ const messageIds = {};
543
544
  if (options && options.chatId !== undefined && options.user === undefined) {
544
545
  options.user = adapter.config.useUsername ? users[options.chatId].userName : users[options.chatId].firstName;
545
546
  }
546
547
 
547
548
  if (options && options.editMessageReplyMarkup !== undefined) {
548
549
  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
- });
550
+ bot && executeSending(() => bot.editMessageReplyMarkup(options.editMessageReplyMarkup.reply_markup, options.editMessageReplyMarkup.options), options, resolve);
565
551
  } else if (options && options.editMessageText !== undefined) {
566
552
  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
- });
553
+ bot && executeSending(() => bot.editMessageText(text, options.editMessageText.options), options, resolve);
583
554
  } else if (options && options.editMessageMedia !== undefined) {
584
555
  adapter.log.debug(`Send editMessageMedia to "${name}"`);
585
556
  if (text) {
586
557
  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')) {
558
+ if (
559
+ (typeof text === 'string' &&
560
+ text.match(/\.(jpg|png|jpeg|bmp|gif)$/i) &&
561
+ (fs.existsSync(text) || text.match(/^(https|http)/i))
562
+ ) ||
563
+ (options && options.type === 'photo')
564
+ ) {
588
565
  mediaInput = {
589
566
  type: 'photo',
590
- media: text
567
+ media: text,
591
568
  };
592
- } else
593
- if ((typeof text === 'string' && text.match(/\.(gif)/i) && fs.existsSync(text)) || (options && options.type === 'animation')) {
569
+ } else if (
570
+ (typeof text === 'string' && text.match(/\.(gif)/i) && fs.existsSync(text)) ||
571
+ (options && options.type === 'animation')
572
+ ) {
594
573
  mediaInput = {
595
574
  type: 'animation',
596
- media: text
575
+ media: text,
597
576
  };
598
- } else
599
- if ((typeof text === 'string' && text.match(/\.(mp4)$/i) && fs.existsSync(text)) || (options && options.type === 'video')) {
577
+ } else if (
578
+ (typeof text === 'string' && text.match(/\.(mp4)$/i) && fs.existsSync(text)) ||
579
+ (options && options.type === 'video')
580
+ ) {
600
581
  mediaInput = {
601
582
  type: 'video',
602
- media: text
583
+ media: text,
603
584
  };
604
- } else
605
- if ((typeof text === 'string' && text.match(/\.(wav|mp3|ogg)$/i) && fs.existsSync(text)) || (options && options.type === 'audio')) {
585
+ } else if (
586
+ (typeof text === 'string' && text.match(/\.(wav|mp3|ogg)$/i) && fs.existsSync(text)) ||
587
+ (options && options.type === 'audio')
588
+ ) {
606
589
  mediaInput = {
607
590
  type: 'audio',
608
- media: text
591
+ media: text,
609
592
  };
610
- } else
611
- if ((typeof text === 'string' && text.match(/\.(txt|doc|docx|csv|pdf|xls|xlsx)$/i) && fs.existsSync(text)) || (options && options.type === 'document')) {
593
+ } else if (
594
+ (typeof text === 'string' && text.match(/\.(txt|doc|docx|csv|pdf|xls|xlsx)$/i) && fs.existsSync(text)) ||
595
+ (options && options.type === 'document')
596
+ ) {
612
597
  mediaInput = {
613
598
  type: 'document',
614
- media: text
599
+ media: text,
615
600
  };
616
601
  }
617
602
 
618
603
  if (mediaInput) {
619
604
  const opts = {
620
- qs: options.editMessageMedia.options,
605
+ qs: options.editMessageMedia.options,
621
606
  };
622
607
  opts.formData = {};
623
608
 
@@ -640,376 +625,188 @@ function _sendMessageHelper(dest, name, text, options) {
640
625
  }
641
626
 
642
627
  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
- });
628
+ bot && executeSending(() => bot._request('editMessageMedia', opts), options, resolve);
659
629
  } else {
660
630
  adapter.log.error(`Cannot send editMessageMedia [chatId - ${options.chatId}]: unsupported media type`);
661
631
  options = null;
662
- resolve(count);
632
+ resolve(JSON.stringify(messageIds));
663
633
  }
664
634
  } else {
665
635
  adapter.log.error(`Cannot send editMessageMedia [chatId - ${options.chatId}]: no media found. "text" may not be empty`);
666
636
  options = null;
667
- resolve(count);
637
+ resolve(JSON.stringify(messageIds));
668
638
  }
669
639
  } else if (options && options.editMessageCaption !== undefined) {
670
640
  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
- });
641
+ bot && executeSending(() => bot.editMessageCaption(text, options.editMessageCaption.options), options, resolve);
687
642
  } else if (options && options.deleteMessage !== undefined) {
688
643
  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
- });
644
+ bot && executeSending(() => bot.deleteMessage(options.deleteMessage.options.chat_id, options.deleteMessage.options.message_id), options, resolve);
705
645
  } 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}`);
646
+ if (options && options.latitude !== undefined) {
647
+ adapter.log.debug(`Send location to "${name}": ${text}`);
648
+ bot && executeSending(() => bot.sendLocation(dest, parseFloat(options.latitude), parseFloat(options.longitude), options), options, resolve);
649
+ } else if (options && options.type === 'mediagroup') {
650
+ adapter.log.debug(`Send media group to "${name}": `);
651
+ if (bot) {
652
+ const {media: fileNames} = options;
653
+ if (fileNames instanceof Array) {
654
+ bot.sendChatAction(dest, 'upload_photo')
655
+ .then(param => {
656
+ if (fileNames.every(name => fs.existsSync(name))) {
657
+ const filesAsArray = fileNames
658
+ .map(element => {
659
+ try {
660
+ return {type: 'photo', media: fs.readFileSync(element)};
661
+ } catch (error) {
662
+ adapter.log.error(`Cannot read file${element}`);
663
+ return undefined;
765
664
  }
766
- options = null;
767
- resolve(count);
768
- });
665
+ })
666
+ .filter(element => element !== undefined);
667
+
668
+ const size = filesAsArray
669
+ .map(element => element.media.length)
670
+ .reduce((acc, val) => acc + val);
671
+
672
+ adapter.log.info(`Send media group to "${name}": ${size} bytes`);
673
+ if (filesAsArray.length > 0) {
674
+ executeSending(() => bot.sendMediaGroup(dest, filesAsArray), options, resolve);
675
+ }
676
+ } else {
677
+ adapter.log.debug('files must exists');
678
+ options = null;
679
+ resolve(JSON.stringify(messageIds));
769
680
  }
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}`);
681
+ })
682
+ .catch(error => {
683
+ adapter.log.error(`upload Error:${error}`);
684
+ });
916
685
  } else {
917
- adapter.log.error(`Cannot send audio [user - ${options.user}]: ${error}`);
686
+ adapter.log.debug('option media should be an array');
687
+ resolve(JSON.stringify(messageIds));
918
688
  }
689
+ } else {
690
+ adapter.log.debug('no files added!');
919
691
  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}`);
692
+ resolve(JSON.stringify(messageIds));
693
+ }
694
+ } else if (text && typeof text === 'string' && actions.includes(text)) {
695
+ adapter.log.debug(`Send action to "${name}": ${text}`);
696
+ bot && executeSending(() => bot.sendChatAction(dest, text), options, resolve);
697
+ } else if (text && ((typeof text === 'string' && text.match(/\.webp$/i) && fs.existsSync(text)) || (options && options.type === 'sticker'))) {
698
+ if (typeof text === 'string') {
699
+ adapter.log.debug(`Send sticker to "${name}": ${text}`);
700
+ } else {
701
+ adapter.log.debug(`Send sticker to "${name}": ${text.length} bytes`);
702
+ }
703
+ bot && executeSending(() => bot.sendSticker(dest, text, options), options, resolve);
704
+ } else if (text && ((typeof text === 'string' && text.match(/\.(gif)/i) && fs.existsSync(text)) || (options && options.type === 'animation'))) {
705
+ if (typeof text === 'string') {
706
+ adapter.log.debug(`Send animation to "${name}": ${text}`);
707
+ } else {
708
+ adapter.log.debug(`Send animation to "${name}": ${text.length} bytes`);
709
+ }
710
+ bot && executeSending(() => bot.sendAnimation(dest, text, options), options, resolve);
711
+ } else if (text && ((typeof text === 'string' && text.match(/\.(mp4)$/i) && fs.existsSync(text)) || (options && options.type === 'video'))) {
712
+ if (typeof text === 'string') {
713
+ adapter.log.debug(`Send video to "${name}": ${text}`);
714
+ } else {
715
+ adapter.log.debug(`Send video to "${name}": ${text.length} bytes`);
716
+ }
717
+ bot && executeSending(() => bot.sendVideo(dest, text, options), options, resolve);
718
+ } else if (text && ((typeof text === 'string' && text.match(/\.(txt|doc|docx|csv|pdf|xls|xlsx)$/i) && fs.existsSync(text)) || (options && options.type === 'document'))) {
719
+ adapter.log.debug(`Send document to "${name}": ${(typeof text === 'string') ? text : text.length}`);
720
+ bot && executeSending(() => bot.sendDocument(dest, text, options), options, resolve);
721
+ } else if (
722
+ text &&
723
+ (
724
+ (typeof text === 'string' &&
725
+ text.match(/\.(wav|mp3|ogg)$/i) &&
726
+ fs.existsSync(text)
727
+ ) ||
728
+ (options && options?.type === 'audio')
729
+ )
730
+ ) {
731
+ adapter.log.debug(`Send audio to "${name}": ${(typeof text === 'string') ? text : text.length}`);
732
+
733
+ bot && executeSending(() => bot.sendAudio(dest, text, options), options, resolve);
734
+
735
+ } else if (
736
+ text &&
737
+ (
738
+ (
739
+ typeof text === 'string' && // if the message is a string, and it is a path to file or URL
740
+ text.match(/\.(jpg|png|jpeg|bmp|gif)$/i) &&
741
+ (fs.existsSync(text) || text.match(/^(https|http)/i))
742
+ ) ||
743
+ (options && options.type === 'photo') // if the type of message is photo
744
+ )
745
+ ) {
746
+ adapter.log.debug(`Send photo to "${name}": ${(typeof text === 'string') ? text : text.length}`);
747
+
748
+ bot && executeSending(() => bot.sendPhoto(dest, text, options), options, resolve);
749
+ } else if (options && options.answerCallbackQuery !== undefined) {
750
+ adapter.log.debug(`Send answerCallbackQuery to "${name}"`);
751
+ if (options.answerCallbackQuery.showAlert === undefined) {
752
+ options.answerCallbackQuery.showAlert = false;
753
+ }
754
+ if (bot && callbackQueryId[options.chatId]) {
755
+ const originalChatId = callbackQueryId[options.chatId].id;
756
+ delete callbackQueryId[options.chatId];
757
+ executeSending(() => bot.answerCallbackQuery(originalChatId, options.answerCallbackQuery.text, options.answerCallbackQuery.showAlert), options, resolve);
758
+ }
925
759
  } 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>')) {
760
+ adapter.log.debug(`Send message to [${name}]: "${text}"`);
761
+ if (text && typeof text === 'string') {
981
762
  options = options || {};
982
- options.parse_mode = 'Markdown';
983
- text = text.substring(10, text.length - 11);
763
+ if (text.startsWith('<MarkdownV2>') && text.endsWith('</MarkdownV2>')) {
764
+ options.parse_mode = 'MarkdownV2';
765
+ text = text.substring(12, text.length - 13);
766
+ } else if (text.startsWith('<HTML>') && text.endsWith('</HTML>')) {
767
+ options.parse_mode = 'HTML';
768
+ text = text.substring(6, text.length - 7);
769
+ } else if (text.startsWith('<Markdown>') && text.endsWith('</Markdown>')) {
770
+ options.parse_mode = 'Markdown';
771
+ text = text.substring(10, text.length - 11);
772
+ }
984
773
  }
774
+ bot && executeSending(() => bot.sendMessage(dest, text || '', options), options, resolve);
985
775
  }
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
776
  });
1007
777
  }
1008
778
 
779
+ /**
780
+ * executes the given method and handles, what to do next
781
+ */
782
+ function executeSending(action, options, resolve){
783
+ // create an empty object, to store chat id and message id of successfully sent messages
784
+ const messageIds = {};
785
+ action()
786
+ .then(response => {
787
+ // put chat id and message id to the object, that will be returned
788
+ messageIds[options.chat_id] = response.message_id;
789
+ // puts ids to the ioBroker database
790
+ saveSendRequest(response);
791
+ })
792
+ .then(() => {
793
+ adapter.log.debug('Message sent');
794
+ options = null;
795
+ // return all the collected message ids to the callback
796
+ resolve(JSON.stringify(messageIds));
797
+ }).catch(error => {
798
+ // log error to the system
799
+ adapter.log.error(`Failed sending [${options.chatId ? 'chatId' : 'user'} - ${options.chatId ? options.chatId : options.user}]: ${error}`);
800
+ options = null;
801
+ // send the succesfully send messages as callback
802
+ resolve(JSON.stringify(messageIds));
803
+ });
804
+ }
805
+
1009
806
  function sendMessage(text, user, chatId, options) {
1010
807
  if (!text && typeof options !== 'object' && text !== 0 && (!options || !options.latitude)) {
1011
808
  adapter.log.warn('Invalid text: null');
1012
- return Promise.resolve(0);
809
+ return Promise.resolve({});
1013
810
  }
1014
811
 
1015
812
  if (text && typeof text === 'object' && text.text !== undefined && typeof text.text === 'string' && options === undefined) {
@@ -1041,12 +838,10 @@ function sendMessage(text, user, chatId, options) {
1041
838
  if (text && text.startsWith('<MarkdownV2>') && text.endsWith('</MarkdownV2>')) {
1042
839
  options.parse_mode = 'MarkdownV2';
1043
840
  text = text.substring(12, text.length - 13);
1044
- } else
1045
- if (text && text.startsWith('<HTML>') && text.endsWith('</HTML>')) {
841
+ } else if (text && text.startsWith('<HTML>') && text.endsWith('</HTML>')) {
1046
842
  options.parse_mode = 'HTML';
1047
843
  text = text.substring(6, text.length - 7);
1048
- } else
1049
- if (text && text.startsWith('<Markdown>') && text.endsWith('</Markdown>')) {
844
+ } else if (text && text.startsWith('<Markdown>') && text.endsWith('</Markdown>')) {
1050
845
  options.parse_mode = 'Markdown';
1051
846
  text = text.substring(10, text.length - 11);
1052
847
  }
@@ -1059,10 +854,7 @@ function sendMessage(text, user, chatId, options) {
1059
854
  }
1060
855
  if (chatId) {
1061
856
  tPromiseList.push(_sendMessageHelper(chatId, 'chat', text, options));
1062
- return Promise.all(tPromiseList)
1063
- .then(results =>
1064
- results.reduce((e, acc) => acc + e, 0))
1065
- .catch(e => e);
857
+ return Promise.all(tPromiseList).catch(e => e);
1066
858
  } else if (user) {
1067
859
  if (typeof user !== 'string' && !(user instanceof Array)) {
1068
860
  adapter.log.warn(`Invalid type of user parameter: ${typeof user}. Expected is string or array.`);
@@ -1092,9 +884,7 @@ function sendMessage(text, user, chatId, options) {
1092
884
  adapter.log.warn(`${userArray.length - matches} of ${userArray.length} recipients are unknown!`);
1093
885
  }
1094
886
 
1095
- return Promise.all(tPromiseList)
1096
- .then(results => results.reduce((e, acc) => acc + e, 0))
1097
- .catch(e => e);
887
+ return Promise.all(tPromiseList).catch(e => e);
1098
888
  }
1099
889
 
1100
890
  const m = typeof text === 'string' ? text.match(/^@(.+?)\b/) : null;
@@ -1129,10 +919,7 @@ function sendMessage(text, user, chatId, options) {
1129
919
  });
1130
920
  }
1131
921
 
1132
- return Promise.all(tPromiseList)
1133
- .then(results =>
1134
- results.reduce((e, acc) => acc + e, 0))
1135
- .catch(e => -1);
922
+ return Promise.all(tPromiseList).catch(e => e);
1136
923
  }
1137
924
 
1138
925
  function saveFile(fileID, fileName, callback) {
@@ -1940,7 +1727,7 @@ function connect() {
1940
1727
  );
1941
1728
  }
1942
1729
  } catch (e) {
1943
-
1730
+ // looks empty
1944
1731
  }
1945
1732
  }
1946
1733
  // Check connection
@@ -2331,4 +2118,4 @@ if (module && module.parent) {
2331
2118
  } else {
2332
2119
  // or start the instance directly
2333
2120
  startAdapter();
2334
- }
2121
+ }