<commit_before>var Express = require('express');
var BodyParser = require('body-parser');
var ResponseMiddleware = require('./response-middleware');
var EventEmitter = require('./event-emitter');
var StringMatcher = require('./string-matcher');
var RegExpMatcher = require('./regexp-matcher');
var ArrayMatcher = require('./array-matcher');
var FacebookMessengerClient = require('./facebook-messenger-client');
var WebhookClient = require('./webhook-client');
var MemoryStorage = require('./memory-storage');

function Bot() {

  this.server = Express()
  this.eventEmitter = new EventEmitter()

  this.server.use(BodyParser.json())
  this.server.use(new ResponseMiddleware())
  this.server.get('/', this.handleWebUIRequest.bind(this))
  this.server.get('/webhook', this.handleWebhookRequest.bind(this))
  this.server.post('/webhook', this.handleWebhookRequest.bind(this))
}

Bot.prototype.handleWebUIRequest = function(req, res) {
  res.send('Web UI');
}

Bot.prototype.handleWebhookRequest = function(req, res) {
  this.trigger('webhook', req, res);
}

Bot.prototype.trigger = function(eventName) {
  console.log(eventName +' event triggered')
  this.eventEmitter.emit.apply(this.eventEmitter, arguments)
}

Bot.prototype.listen = function(port) {
  console.log("Bot is listening to port " + port)
  this.server.listen(port);
}

Bot.prototype.on = function(eventName, handler) {
  this.eventEmitter.addListener(eventName, handler)
}

Bot.prototype.hears = function(pattern, handler) {

  var matcher = pattern

  if (pattern instanceof Array) {
    matcher = new ArrayMatcher()
  } else if (pattern instanceof String) {
    matcher = new StringMatcher(pattern)
  } else if(pattern instanceof RegExp) {
    matcher = new RegExpMatcher(pattern)
  }

  this.on('message_received', function(message, session, next) {
    if (matcher(message)) {
      handler(message, session)
    } else {
      next()
    }
  })
}

Bot.prototype.use = function(component) {
  component(this)
}

module.exports = {
  Bot: Bot,
  WebhookClient: WebhookClient,
  FacebookMessengerClient: FacebookMessengerClient,
  MemoryStorage: MemoryStorage
}
<commit_msg>Exposes new router and starter helper.<commit_after>var Express = require('express');
var BodyParser = require('body-parser');
var ResponseMiddleware = require('./response-middleware');
var EventEmitter = require('./event-emitter');
var StringMatcher = require('./string-matcher');
var RegExpMatcher = require('./regexp-matcher');
var ArrayMatcher = require('./array-matcher');
var FacebookMessengerClient = require('./facebook-messenger-client');
var WebhookClient = require('./webhook-client');
var MemoryStorage = require('./memory-storage');

function Bot() {

  this.router = Express.Router()
  this.eventEmitter = new EventEmitter()

  this.router.use(BodyParser.json())
  this.router.use(new ResponseMiddleware())
  this.router.get('/', this.handleWebUIRequest.bind(this))
  this.router.get('/webhook', this.handleWebhookRequest.bind(this))
  this.router.post('/webhook', this.handleWebhookRequest.bind(this))
}

Bot.prototype.handleWebUIRequest = function(req, res) {
  res.send('Web UI');
}

Bot.prototype.handleWebhookRequest = function(req, res) {
  this.trigger('webhook', req, res);
}

Bot.prototype.trigger = function(eventName) {
  console.log(eventName +' event triggered')
  this.eventEmitter.emit.apply(this.eventEmitter, arguments)
}

Bot.prototype.on = function(eventName, handler) {
  this.eventEmitter.addListener(eventName, handler)
}

Bot.prototype.hears = function(pattern, handler) {

  var matcher = pattern

  if (pattern instanceof Array) {
    matcher = new ArrayMatcher()
  } else if (pattern instanceof String) {
    matcher = new StringMatcher(pattern)
  } else if(pattern instanceof RegExp) {
    matcher = new RegExpMatcher(pattern)
  }

  this.on('message_received', function(message, session, next) {
    if (matcher(message)) {
      handler(message, session)
    } else {
      next()
    }
  })
}

Bot.prototype.use = function(component) {
  component(this)
}

function startBot(bot, port) {
  console.log("Bot is listening to port " + port)
  var server = Express()
  server.use(bot.router)
  server.listen(port)
}

module.exports = {
  Bot: Bot,
  WebhookClient: WebhookClient,
  FacebookMessengerClient: FacebookMessengerClient,
  MemoryStorage: MemoryStorage,
  startBot: startBot
}

{'fraction_non_alphanumeric': 0.1418098510882016, 'fraction_numerical': 0.0, 'mean_word_length': 4.8525469168900806, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1913839', 'n_tokens_mistral': 1323, 'n_tokens_neox': 1315, 'n_words': 268}