const config = require(`../config/${process.env.NODE_ENV || 'production'}`);
var querystring = require('querystring');
var request = require('request');

var process_message = function(text) {
  var data = {'query': text};

  request({
    headers: {
      'x-app-id': config.threeScale.appid,
      'x-app-key': config.threeScale.appkey
    },
    uri: 'https://trackapi.nutritionix.com/v2/natural/nutrients',
    json: data,
    method: 'POST'
  }, function(err, res, body) {
    console.log(body);
    console.log(err);
    if (!err) {
      return body.foods[0].food_name + ' has about ' + body.foods[0].nf_calories + ' calories.';
    } else {
      return 'Could not detect any foods.'
    }

  })

};


module.exports = {
  process_message: process_message
};

@@ -283,17 +283,17 @@
 cale.app
-i
+I
 d,%0A     
@@ -327,17 +327,17 @@
 cale.app
-k
+K
 ey%0A    %7D
@@ -479,62 +479,93 @@
     
-console.log(body);%0A    console.log(err);%0A    if (!err)
+if (res.status_code !=== 200) %7B%0A      return 'Could not detect any foods.'%0A    %7D else
  %7B%0A 
@@ -589,16 +589,17 @@
 foods%5B0%5D
+s
 .food_na
@@ -670,64 +670,8 @@
    %7D
- else %7B%0A      return 'Could not detect any foods.'%0A    %7D
 %0A%0A  

check resp status code; not err from callback