var HTTPS = require('https');
var path = require('path');
var config = require('./config');

var groupIdToBotMap = {};
config.bots.forEach(function (bot) {
  groupIdToBotMap[bot.groupID] = bot;
});

var commands = [];
var registerCommand = function (command, description, func) {
  commands[command] = [description, func];
};

var normalizedPath = path.join(__dirname, "commands");
require("fs").readdirSync(normalizedPath).forEach(function (file) {
  if (path.extname(file) === '.js') {
    require("./commands/" + file)(registerCommand)
  }
});

function respond(request) {
  var bot = groupIdToBotMap[request.group_id],
      msg = request.text;
  if (!bot) {
    console.log('New message received, but no bot configured for Group ID ' + request.group_id);
  } else {
    console.log('New message from %s in group \'%s\': %s', request.name, bot.groupLocalID, msg);
    if (typeof msg === 'string' && msg.substring(0, 4) === 'bot ') {
      msg = msg.substring(4);
      processCommand(bot.groupLocalID, msg, function (response) {
        if (response !== null) {
          postMessage(bot.botID, response);
        }
      });
    }
  }
}

function processCommand(groupLocalID, message, cb) {
  var tokens = message.split(' '),
      commandString = tokens.shift();

  if (groupLocalID === '3') {
    // do not send anything to the group I assigned local id 3
    cb(null);
    return;
  }

  if (!commandString) {
    cb(null);
    return;
  }

  if (commandString === 'help') {
    var helpText = 'Usage: bot <command> <arguments> [optional arguments]\n' +
      'Available commands:\nhelp: Display this text\n';
    for (var key in commands) {
      if (commands.hasOwnProperty(key)) {
        helpText += commands[key][0] + '\n';
      }
    }
    cb(helpText);
    return;
  }

  var command = commands[commandString];
  if (!command) {
    cb('Command \'' + commandString + '\' not supported. Type \'bot help\' for available commands.');
    return;
  }

  command[1](groupLocalID, tokens, cb);
}

function postMessage(botID, text) {
  var options, body, botReq;

  options = {
    hostname: 'api.groupme.com',
    path: '/v3/bots/post',
    method: 'POST'
  };

  body = {
    "bot_id" : botID,
    "text" : text
  };

  console.log('sending ' + text + ' to ' + botID);

  botReq = HTTPS.request(options, function(res) {
      if(res.statusCode == 202) {
        //neat
      } else {
        console.log('rejecting bad status code ' + res.statusCode);
      }
  });

  botReq.on('error', function(err) {
    console.log('error posting message '  + JSON.stringify(err));
  });
  botReq.on('timeout', function(err) {
    console.log('timeout posting message '  + JSON.stringify(err));
  });
  botReq.end(JSON.stringify(body));
}

exports.respond = respond;
exports.processCommand = processCommand;
exports.postMessage = postMessage;

@@ -84,16 +84,30 @@
 onfig');
+%0Avar bot = %7B%7D;
 %0A%0Avar gr
@@ -225,20 +225,20 @@
 s = %5B%5D;%0A
-var 
+bot.
 register
@@ -529,16 +529,20 @@
 + file)(
+bot.
 register
@@ -559,16 +559,30 @@
  %7D%0A%7D);%0A%0A
+bot.respond = 
 function
@@ -582,23 +582,16 @@
 unction 
-respond
 (request
@@ -1159,27 +1159,23 @@
  %7D%0A  %7D%0A%7D
+;
 %0A%0A
-function 
+bot.
 processC
@@ -1180,16 +1180,28 @@
 sCommand
+ = function 
 (groupLo
@@ -2035,19 +2035,15 @@
 );%0A%7D
+;
 %0A%0A
-function 
+bot.
 post
@@ -2045,24 +2045,36 @@
 .postMessage
+ = function 
 (botID, text
@@ -2775,109 +2775,29 @@
 );%0A%7D
+;
 %0A%0A
-exports.respond = respond;%0Aexports.processCommand = processCommand;%0Aexports.postMessage = postMessage
+module.exports = bot
 ;%0A

update bot.js to use module.exports instead of exports to avoid confusion