简体   繁体   English

将JSON数组返回到Node JS中的电报机器人?

[英]Returning a json array to telegram bot in node js?

I am a bit stuck with my telegram bot. 我对电报漫游器有些困惑。 I have built the bot in node js and my backend is in Java. 我已经在节点js中构建了该漫游器,而后端在Java中。 So, basically, when a user enters the category in the bot, it will return a list of options with different categories. 因此,基本上,当用户在漫游器中输入类别时,它将返回具有不同类别的选项列表。 Now, my java web service is returning the categories as JSON array. 现在,我的Java Web服务将类别作为JSON数组返回。 When I tried to return the JSON array to my bot, I get some errors. 当我尝试将JSON数组返回给我的机器人时,出现一些错误。 What could I be doing wrong? 我可能做错了什么? Here is my code. 这是我的代码。

Bot.js Bot.js

telegram.on("text",function (message,req,res) {

    var messagetext = message.text;
    var receiver = message.chat.id; //the user receiving the response from the bot
    var timestamp = message.date; //timestamp
    var msgid = message.message_id;//message id
    var sender = message.from.id; //id of the telegram bot
    console.log("message",messagetext);
    fd.itemcategory().then(function (v) {
        console.log(v);
        telegram.sendMessage(sender,v);


    });
});

Botservice.js Botservice.js

module.exports = {
    itemcategory: function(callback) {
        var categories=[];
        return new Promise(function(resolve, reject){
            request('https://********.ngrok.io/', { json: true },  function(err,res,body) {

                for(i=0;i<body.categories.length;i++){
                    categories.push(body.categories[i].categories.name);
                }
                resolve(categories);


            });



        });


    }

};

Error logs 错误日志

Unhandled rejection Error: ETELEGRAM: 400 Bad Request: message text is empty
    at TelegramError.Error (native)
    at TelegramError.BaseError (C:\Users\Brian\Desktop\TelegramBot\node_modules\
node-telegram-bot-api\lib\errors.js:22:108)
    at new TelegramError (C:\Users\Brian\Desktop\TelegramBot\node_modules\node-t
elegram-bot-api\lib\errors.js:90:117)
    at C:\Users\Brian\Desktop\TelegramBot\node_modules\node-telegram-bot-api\lib
\telegram.js:213:15
    at tryCatcher (C:\Users\Brian\Desktop\TelegramBot\node_modules\bluebird\js\r
elease\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\Users\Brian\Desktop\TelegramBot\nod
e_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (C:\Users\Brian\Desktop\TelegramBot\node_modules\b
luebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (C:\Users\Brian\Desktop\TelegramBot\node_modules\
bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\Brian\Desktop\TelegramBot\node_modules\
bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (C:\Users\Brian\Desktop\TelegramBot\node_modules\bluebi
rd\js\release\async.js:133:16)
    at Async._drainQueues (C:\Users\Brian\Desktop\TelegramBot\node_modules\blueb
ird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\Brian\Desktop\Tel
egramBot\node_modules\bluebird\js\release\async.js:17:14)
    at processImmediate [as _immediateCallback] (timers.js:367:17)

v - Json array v-Json数组

[ 'Delivery',
  'Dine-out',
  'Nightlife',
  'Catching-up',
  'Takeaway',
  'Cafes',
  'Daily Menus',
  'Breakfast',
  'Lunch',
  'Dinner',
  'Pubs & Bars',
  'Pocket Friendly Deli
  'Clubs & Lounges' ]

According to the documentation , the message must be in string format. 根据文档 ,该消息必须为字符串格式。 You are sending an array. 您正在发送数组。 That propably causes the error. 那很可能导致错误。

So you need to parse your array to a string manually. 因此,您需要手动将数组解析为字符串。 Here is some code to implement it: 这是一些实现它的代码:

var text = '';
for (var i = 0; i < v.length; i++) {
    text += v[i] + ' '; // or however you want to format it
}
telegram.sendMessage(sender, text);

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

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