skip to Main Content

when i run my schedule code to post message, each time the message is sent i receive this error message:

Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)

Code below does not use try { in the beginning, but even with this i got the same error message ..

'use strict';

const Telegram = require('telegram-node-bot');
var schedule = require('node-schedule');

class AutoMsgController extends Telegram.TelegramBaseController {
    AutoMsgHandler(scope) {
        console.log(scope);
        if (statement) {
            const button = {'inline_keyboard': [
                [{ text: 'BUTTON1', url: 'URL1' }],
                [{ text: 'BUTTON2', url: 'URL2' }]
            ]}
            const parsedbutton = JSON.stringify(button);
            console.log(parsedbutton);
            scope.api.sendMessage(scope.message._chat._id, '*ACTIVATED!*', { 'parse_mode' : 'Markdown'});
            var j = schedule.scheduleJob('*/5 * * * * *', function(){ //5 Seconds
            scope.api.sendMessage(scope.message._chat._id, 'My message'
            ).then (
            function (e) {
                console.log ('Could not send automsg', e);
                throw e;
              }
            );
    }).catch((err) => {
        console.log('Api call error:', err.message)
      });
    }
}
    get routes() {
        return {
            'AutoMsgCommand': 'AutoMsgHandler'
        };
    }
}

module.exports = AutoMsgController;

If i try to use try { im unable to finish, it says : finally expected

'use strict';

const Telegram = require('telegram-node-bot');
var schedule = require('node-schedule');

class AutoMsgController extends Telegram.TelegramBaseController {
    AutoMsgHandler(scope) {
        console.log(scope);
        if (statement) {
            try {
            const button = {'inline_keyboard': [
                [{ text: 'BUTTON1', url: 'URL1' }],
                [{ text: 'BUTTON2', url: 'URL2' }]
            ]}
            const parsedbutton = JSON.stringify(button);
            console.log(parsedbutton);
            scope.api.sendMessage(scope.message._chat._id, '*ACTIVATED!*', { 'parse_mode' : 'Markdown'});
            var j = schedule.scheduleJob('*/5 * * * * *', function(){ //5 Seconds
            scope.api.sendMessage(scope.message._chat._id, 'My message'
            ).then (
            function (e) {
                console.log ('Could not send automsg', e);
                throw e;
              }
            );
    }).catch((err) => {
        console.log('Api call error:', err.message)
      });
    }
  } //<- here is the problem `finally expected`
}
    get routes() {
        return {
            'AutoMsgCommand': 'AutoMsgHandler'
        };
    }
}

module.exports = AutoMsgController;

Can’t figure it out… from other posts i was not able to find answer.

Even in this case it will drop the same thing…

'use strict';

const Telegram = require('telegram-node-bot');
var schedule = require('node-schedule');

class AutoMsgController extends Telegram.TelegramBaseController {
    AutoMsgHandler(scope) {
        console.log(scope);
        if (statement) {
            try {
            const button = {'inline_keyboard': [
                [{ text: 'BUTTON1', url: 'URL1' }],
                [{ text: 'BUTTON2', url: 'URL2' }]
            ]}
            const parsedbutton = JSON.stringify(button);
            console.log(parsedbutton);
            scope.api.sendMessage(scope.message._chat._id, '*ACTIVATED!*', { 'parse_mode' : 'Markdown'});
            var j = schedule.scheduleJob('*/5 * * * * *', function(){ //5 Seconds
            scope.api.sendMessage(scope.message._chat._id, 'My message'
            ).then (
            function (e) {
                console.log ('Could not send automsg', e);
                throw e;
              }
            );
    }).catch((err) => {
        console.log('Api call error:', err.message)
      });
        catch (error) {
        console.log('Api call error:', error)
      }
    }
}
    get routes() {
        return {
            'AutoMsgCommand': 'AutoMsgHandler'
        };
    }
}

module.exports = AutoMsgController;
'use strict';

const Telegram = require('telegram-node-bot');
var schedule = require('node-schedule');

class AutoMsgController extends Telegram.TelegramBaseController {
    AutoMsgHandler(scope) {
        console.log(scope);
        if (statement) {
            try {
            const button = {'inline_keyboard': [
                [{ text: 'BUTTON1', url: 'URL1' }],
                [{ text: 'BUTTON2', url: 'URL2' }]
            ]}
            const parsedbutton = JSON.stringify(button);
            console.log(parsedbutton);
            scope.api.sendMessage(scope.message._chat._id, '*ACTIVATED!*', { 'parse_mode' : 'Markdown'});
            var j = schedule.scheduleJob('*/5 * * * * *', function(){ //5 Seconds
            scope.api.sendPhoto(scope.message._chat._id, 'My Message'
            ).then (
                function (e) {
                    console.log ('Could not send automsg', e);
                    throw e;
                  }
                );
        }).catch((err) => {
            console.log('Api call error:', err.message)
          });         
        } catch (error) {
        console.log('Api call error:', error)
      }
    }
}
    get routes() {
        return {
            'AutoMsgCommand': 'AutoMsgHandler'
        };
    }
}

module.exports = AutoMsgController;

Made progress with this code
Api call error: TypeError: schedule.scheduleJob(…).catch is not a function

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 5)

2

Answers


  1. You forgot to place a catch or finally block after the try, as well as using then rather than catch for error handling.

    For more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try…catch

    Also, you should look at what the error is that is being thrown, since an error indicates a failed transaction.

    'use strict';
    
    const Telegram = require('telegram-node-bot');
    var schedule = require('node-schedule');
    
    class AutoMsgController extends Telegram.TelegramBaseController {
        AutoMsgHandler(scope) {
            console.log(scope);
            if (statement) {
                try {
                const button = {'inline_keyboard': [
                    [{ text: 'BUTTON1', url: 'URL1' }],
                    [{ text: 'BUTTON2', url: 'URL2' }]
                ]}
                const parsedbutton = JSON.stringify(button);
                console.log(parsedbutton);
                scope.api.sendMessage(scope.message._chat._id, '*ACTIVATED!*', { 'parse_mode' : 'Markdown'});
                var j = schedule.scheduleJob('*/5 * * * * *', function(){ //5 Seconds
                scope.api.sendMessage(scope.message._chat._id, 'My message'
                ).then(function (response) {
                    // Do something with the response
                }).catch (
                function (e) {
                    console.log ('Could not send automsg', e);
                    throw e; // This still throws the same warning, but could be useful
                  }
                );
        }).catch((err) => {
            console.log('Api call error:', err.message)
          });
        }
      } catch (error) {
        // Do error handling stuff
      }
    }
        get routes() {
            return {
                'AutoMsgCommand': 'AutoMsgHandler'
            };
        }
    }
    
    module.exports = AutoMsgController;
    
    Login or Signup to reply.
  2. What about this:

    const Telegram = require("telegram-node-bot");
    var schedule = require("node-schedule");
    
    class AutoMsgController extends Telegram.TelegramBaseController {
      AutoMsgHandler(scope) {
        console.log(scope);
        if (statement) {
          try {
            const button = {
              inline_keyboard: [
                [{ text: "BUTTON1", url: "URL1" }],
                [{ text: "BUTTON2", url: "URL2" }],
              ],
            };
            const parsedbutton = JSON.stringify(button);
            console.log(parsedbutton);
            scope.api.sendMessage(scope.message._chat._id, "*ACTIVATED!*", {
              parse_mode: "Markdown",
            });
            var j = schedule
              .scheduleJob("*/5 * * * * *", function () {
                //5 Seconds
                scope.api
                  .sendMessage(scope.message._chat._id, "My message")
                  .then(function (e) {
                    console.log("Could not send automsg", e);
                    throw e;
                  }).catch((error) => {
                    console.log("sendMessage call error:", error.message);
                    throw error;
                  });;
              });
          } catch (error) {
            console.log("Api call error:", error);
          }
        }
      }
      get routes() {
        return {
          AutoMsgCommand: "AutoMsgHandler",
        };
      }
    }
    
    module.exports = AutoMsgController;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search