skip to Main Content

I’m new to telegram bot and node js, I’m developing a simple bot that calls api urls to get json objects, but I’ve this error with a command. This is the command’s code:

bot.onText(//fixtures/, (msg) => {
  const chatId = msg.chat.id;
  var out = "";

  function myFunction(arr) {
      var i;
      for(i = 0; i < arr.length; i++) {
          out += arr[i].name + "--";
      }
  }

  request.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
          var myArr = JSON.parse(this.responseText);
          myFunction(myArr);
      }
  };

  request.open("GET", url, true);
  request.send();

  bot.sendMessage(chatId, out);

And this is full error:

Unhandled rejection Error: ETELEGRAM: 400 Bad Request: message text is empty
at request.then.resp (C:UsersUtente1DesktopMyFootballBotnode_modulesnode-telegram-bot-apisrctelegram.js:280:15)
at tryCatcher (C:UsersUtente1DesktopMyFootballBotnode_modulesbluebirdjsreleaseutil.js:16:23)
at Promise._settlePromiseFromHandler (C:UsersUtente1DesktopMyFootballBotnode_modulesbluebirdjsreleasepromise.js:512:31)
at Promise._settlePromise (C:UsersUtente1DesktopMyFootballBotnode_modulesbluebirdjsreleasepromise.js:569:18)
at Promise._settlePromise0 (C:UsersUtente1DesktopMyFootballBotnode_modulesbluebirdjsreleasepromise.js:614:10)
at Promise._settlePromises (C:UsersUtente1DesktopMyFootballBotnode_modulesbluebirdjsreleasepromise.js:694:18)
at _drainQueueStep (C:UsersUtente1DesktopMyFootballBotnode_modulesbluebirdjsreleaseasync.js:138:12)
at _drainQueue (C:UsersUtente1DesktopMyFootballBotnode_modulesbluebirdjsreleaseasync.js:131:9)
at Async._drainQueues (C:UsersUtente1DesktopMyFootballBotnode_modulesbluebirdjsreleaseasync.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (C:UsersUtente1DesktopMyFootballBotnode_modulesbluebirdjsreleaseasync.js:17:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)

2

Answers


  1. You should send your message after your success ajax request reply. Move bot.sendMessage(chatId, out); inside onReadyStateChange callback:

    request.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var myArr = JSON.parse(this.responseText);
            myFunction(myArr);
            bot.sendMessage(chatId, out);
        }
    };
    
    Login or Signup to reply.
  2. You will need to read the text gotten from the telegram bot.
    One way to do that is to:

    var out = msg.text;
    

    So you will have this:

     bot.onText(//fixtures/, (msg) => {
     const chatId = msg.chat.id;
      var out = msg.text;
    
      function myFunction(arr) {
          var i;
          for(i = 0; i < arr.length; i++) {
              out += arr[i].name + "--";
          }
      }
    
     request.onreadystatechange = 
    function() {
          if (this.readyState == 4 && 
          this.status == 200) {
            var myArr = JSON.parse(this.responseText);
            myFunction(myArr);
          }
      };
    
      request.open("GET", url, true);
      request.send();
    
      bot.sendMessage(chatId, out);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search