EDIT: testing only the code for the GET-Request (without telegram api) on my raspberry pi. Instead of sending a tg message, I write it to a file. I’ve discovered that the magic happens in this part:
req.end(function (res) {
if (res.error) throw new Error(res.error);
console.log(res.body);
});
The console.log(res.body)
gives me this output (which are the correct names of tables / columns):
{ test:
{ columns: [ 'eventID', 'event_desc' ],
records:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ] } }
But if I replace it with fs.appendFile("getrequest.log",res.body + "n");
I only get:
[object Object]
Any idea why the result changes when I use another function?
Original question:
I’m really new in to nodejs and the telegram bot api.
What I’m trying is to make a HTTP GET request to an URL, which returns data in JSON format. I’d like that the bot is sending that result into the chat.
My bot is running on openshift and is written in NodeJS, using this guide and code sample. To figure out the code for the GET request, i’ve used postman. It already helped me to make a POST request to this URL. So I’m using unirest and this code:
bot.onText(//list (.+)/, function (msg, match) {
var fromId = msg.chat.id;
var eventDb = match[1];
var eventURL = "https://api.italianrockmafia.ch/api.php/";
eventURL += eventDb;
var unirest = require("unirest");
var req = unirest("GET", eventURL);
req.headers({
"cache-control": "no-cache"
});
req.end(function (res) {
if (res.error) throw new Error(res.error);
console.log(res.body);
});
bot.sendMessage(fromId, req);
});
So I’m not sure which value i must use in bot.sendMessage();
and where I must use this function. I’ve tried several combinations ( with req
,res
,res.body
) But none of them worked.
If I use the code just like above, the log says message ist empty
.
When I change the last lines to:
req.end(function (res) {
bot.sendMessage(fromId, res);
if (res.error) throw new Error(res.error);
console.log(res.body);
});
the log only throws up a “Wrong file identifier/HTTP URL specified” in a file of the telegram module it self. This message shows up at every command (even that ones which are working).
So my 2 questions are:
- Which value do I need to send?
- Where in the code do I need to put
bot.sendMessage
in?
I’m struggling really at those two, so some help is really appreciated!
2
Answers
Turns out I just had no clue how to handle JSON. layer 8 error...
I think this has something to do with the inputfile limitations