简体   繁体   中英

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. 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. When I tried to return the JSON array to my bot, I get some errors. What could I be doing wrong? Here is my code.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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