简体   繁体   English

未处理的拒绝错误:ETELEGRAM:400 错误请求:消息文本为空

[英]Unhandled rejection Error: ETELEGRAM: 400 Bad Request: message text is empty

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.我是电报机器人和 node.js 的新手,我正在开发一个简单的机器人,它调用 api url 来获取 json 个对象,但是我在命令中遇到了这个错误。 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:\Users\Utente1\Desktop\MyFootballBot\node_modules\node-telegram-bot-api\src\telegram.js:280:15)
at tryCatcher (C:\Users\Utente1\Desktop\MyFootballBot\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (C:\Users\Utente1\Desktop\MyFootballBot\node_modules\bluebird\js\release\promise.js:512:31)
at Promise._settlePromise (C:\Users\Utente1\Desktop\MyFootballBot\node_modules\bluebird\js\release\promise.js:569:18)
at Promise._settlePromise0 (C:\Users\Utente1\Desktop\MyFootballBot\node_modules\bluebird\js\release\promise.js:614:10)
at Promise._settlePromises (C:\Users\Utente1\Desktop\MyFootballBot\node_modules\bluebird\js\release\promise.js:694:18)
at _drainQueueStep (C:\Users\Utente1\Desktop\MyFootballBot\node_modules\bluebird\js\release\async.js:138:12)
at _drainQueue (C:\Users\Utente1\Desktop\MyFootballBot\node_modules\bluebird\js\release\async.js:131:9)
at Async._drainQueues (C:\Users\Utente1\Desktop\MyFootballBot\node_modules\bluebird\js\release\async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\Utente1\Desktop\MyFootballBot\node_modules\bluebird\js\release\async.js:17:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)

You should send your message after your success ajax request reply. ajax请求回复成功后,您应该发送您的消息。 Move bot.sendMessage(chatId, out);移动bot.sendMessage(chatId, out); inside onReadyStateChange callback:onReadyStateChange回调中:

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

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM