send result from HTTP GET request with a telegram bot
<p><strong>EDIT</strong>: 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:</p>

<pre><code>req.end(function (res) {
    if (res.error) throw new Error(res.error);

    console.log(res.body);
  });
</code></pre>

<p>The <code>console.log(res.body)</code>gives me this output (which are the correct names of tables / columns):</p>

<pre><code>{ test: 
   { columns: [ 'eventID', 'event_desc' ],
     records: 
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ] } }
</code></pre>

<p>But if I replace it with <code>fs.appendFile("getrequest.log",res.body + "\n");</code> I only get:</p>

<pre><code>[object Object]
</code></pre>

<p><strong>Any idea why the result changes when I use another function?</strong></p>

<hr>

<p>Original question:</p>

<p>I'm really new in to nodejs and the telegram bot api.</p>

<p>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. </p>

<p>My bot is running on openshift and is written in NodeJS, using <a href="https://github.com/yagop/node-telegram-bot-api" rel="nofollow noreferrer">this guide and code sample</a>. To figure out the code for the GET request, i've used <a href="https://www.getpostman.com" rel="nofollow noreferrer">postman</a>. It already helped me to make a POST request to this URL. So I'm using unirest and this code:</p>

<pre><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);
});
</code></pre>

<p>So I'm not sure which value i must use in <code>bot.sendMessage();</code> and where I must use this function. I've tried several combinations ( with <code>req</code>,<code>res</code>,<code>res.body</code>) But none of them worked.</p>

<p>If I use the code just like above, the log says <code>message ist empty</code>. 
When I change the last lines to:</p>

<pre><code>req.end(function (res) {
    bot.sendMessage(fromId, res);
   if (res.error) throw new Error(res.error);

   console.log(res.body);
 }); 
</code></pre>

<p>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).</p>

<p>So my 2 questions are:</p>

<ul>
<li><strong>Which value do I need to send?</strong></li>
<li><strong>Where in the code do I need to put <code>bot.sendMessage</code> in?</strong></li>
</ul>

<p>I'm struggling really at those two, so some help is really appreciated! </p>