it('should use global defaults', () => { im.setDefaults({ executable: 'foobar' }); im.commands().get('a', 'b').should.equal('foobar a b'); });
it('should ignore global defaults if options is passed to commands()', () => { im.setDefaults({ executable: 'foobar' }); im.commands({ executable: 'monkey' }).get('a', 'b').should.equal('monkey a b'); });
exports.processRequest = function processRequest (request, response) { var actionFound, commands, input, outgoingData, pipedResponse, regex, requestText, responseMethod, responseText, VARIABLES ; input = request.body.text; // The keys on this object will be replaced with their corresponding values // at runtime. VARIABLES = { 'HERE': '#' + request.body.channel_name, 'ME': '@' + request.body.user_name, 'TODAY': new Date(Date.now()).toDateString(), 'NOW': new Date(Date.now()).toLocaleTimeString(), 'DOMAIN': request.body.team_domain }; _.each(VARIABLES, function (value, key){ regex = new RegExp('%24' + key, 'gm'); input = input.replace(regex, value); }); // Parse commands commands = parse.commands(input); responseMethod = (request.body.trigger_word) ? 'webhook' : 'api'; requestText; if (request.body.trigger_word) requestText = parse.slackText(input.substring(request.body.trigger_word.length + 1, input.length)); else {// command requestText = decodeURIComponent(input.replace(/\+/g, '%20')); } log.info('bot processing request', request.body, request.id); outgoingData = { channel_id: request.body.channel_id, channel_name: request.body.channel_name, team_domain: request.body.team_domain, team_id: request.body.team_id, text: requestText, timestamp: request.body.timestamp, user_id: request.body.user_id, user_name: request.body.user_name }; responseText; actionFound; pipedResponse = null; _.each(commands, function (command) { actionFound = _.find(exports.actions, {name: command.name}); // Actions desn't exist. Inform the user. if (!actionFound) { log.error('no bot action found', requestText, request.id); responseText = 'Invalid action, try `help`.'; response.statusCode = 200; response.end(formatResponse(responseText)); } // If the action hasn't completed in time, let the user know. setTimeout(function() { if (!responseText) { log.error('bot action timed out', actionFound.name, request.id); response.statusCode = 500; response.end(); } }, config.timeout); outgoingData.command = _.clone(command); outgoingData.pipedResponse = _.clone(pipedResponse); actionFound.execute(outgoingData, function (actionResponse) { responseText = actionResponse; // No data back form the action. if (!responseText) { response.statusCode = 500; response.end(); log.error('action did not return a response', actionFound.name, request.id); return; } // Success. Now, format the responseText. log.info('bot responding with action', actionFound.name, request.id); if (typeof responseText === 'string') { responseText.replace('&', '&').replace('<', '<').replace('>', '>'); } else { responseText = JSON.stringify(responseText); } // If the command should be piped, save the result. if (command.pipe) { pipedResponse = responseText; return true; } else { pipedResponse = null; } // User is ending their command with the `>`. Assume current room. if (command.redirects && !command.redirectTo.length) { command.redirectTo.push({ type: 'channel', name: request.body.channel_name }); } // If the response should be redirected, then do so if (command.redirectTo.length > 0) { _.each(command.redirectTo, function (redirect) { switch (redirect.type) { case 'user': exports.sendMessage(responseText, '@' + redirect.name); break; case 'channel': exports.sendMessage(responseText, '#' + redirect.name); break; case 'group': exports.sendMessage(responseText, '#' + redirect.name); break; case 'file': // Todo file creation/editing break; default: break; } }); return true; } response.statusCode = 200; response.end(formatResponse(responseText)); log.info('bot successfully responded', {}, request.id); return true; }); }); function formatResponse (response) { return (request.body.trigger_word) ? JSON.stringify({text: response}) : response; } };