<commit_before>var Botkit = require(__dirname + '/CoreBot.js');
var WebSocket = require('ws');
var express = require('express');
var bodyParser = require('body-parser');
var querystring = require('querystring');


function SocketBot(configuration) {

    var controller = Botkit(configuration || {});

    var wss = new WebSocket.Server({
        port: configuration.socket_port || 8080,
     });

     wss.on('connection', function connection(ws) {

       // search through all the convos, if a bot matches, update its ws
       var bot = controller.spawn();
       bot.ws = ws;
       bot.connected = true;

       ws.on('message', function incoming(message) {

         console.log('received: %s', message);

         var message = JSON.parse(message);
         bot.findConversation(message, function(convo) {
            var reconnected = false;
            if (convo) {
                 if (convo.task.bot.connected) {
                     console.log('WEIRD STILL CONNECTED?');
                 } else {
                     console.log('BOT JUST RECONNECTED');
                     convo.task.bot.ws = bot.ws;
                     convo.task.bot.connected = true;
                     reconnected = true;
                     controller.trigger('reconnect',[bot, message]);
                 }
             }

             if (message.type == 'message') {
                 controller.receiveMessage(bot, message);
             } else if (message.type == 'hello' && !reconnected) {

                 var uid = message.user;
                 if (!uid) {
                     uid = guid();
                     bot.send({
                         type: 'hello',
                         user: uid,
                     });
                     controller.trigger(message.type,[bot, message]);
                 } else {
                     controller.trigger('welcome_back',[bot, message]);
                 }

             } else {
                 controller.trigger(message.type,[bot, message]);
             }


         });

       });

       ws.on('close', function(err) {
           console.log('CLOSED', err);
           bot.connected = false;
       });

     });


     controller.defineBot(function(botkit, config) {
       var bot = {
           type: 'socket',<commit_msg>wrap socket server in a function use pre-existing webserver<commit_after>var Botkit = require(__dirname + '/CoreBot.js');
var WebSocket = require('ws');
var express = require('express');
var bodyParser = require('body-parser');
var querystring = require('querystring');
var http = require('http');

function SocketBot(configuration) {

    var controller = Botkit(configuration || {});


    controller.openSocketServer = function(server) {

      // create the socket server along side the existing webserver.
      var wss = new WebSocket.Server({ server });

       wss.on('connection', function connection(ws) {

         // search through all the convos, if a bot matches, update its ws
         var bot = controller.spawn();
         bot.ws = ws;
         bot.connected = true;

         ws.on('message', function incoming(message) {

           console.log('received: %s', message);

           var message = JSON.parse(message);
           bot.findConversation(message, function(convo) {
              var reconnected = false;
              if (convo) {
                   if (convo.task.bot.connected) {
                       console.log('WEIRD STILL CONNECTED?');
                   } else {
                       console.log('BOT JUST RECONNECTED');
                       convo.task.bot.ws = bot.ws;
                       convo.task.bot.connected = true;
                       reconnected = true;
                       controller.trigger('reconnect',[bot, message]);
                   }
               }

               if (message.type == 'message') {
                   controller.receiveMessage(bot, message);
               } else if (message.type == 'hello' && !reconnected) {

                   var uid = message.user;
                   if (!uid) {
                       uid = guid();
                       bot.send({
                           type: 'hello',
                           user: uid,
                       });
                       controller.trigger(message.type,[bot, message]);
                   } else {
                       controller.trigger('welcome_back',[bot, message]);
                   }

               } else {
                   controller.trigger(message.type,[bot, message]);
               }


           });

         });

         ws.on('close', function(err) {
             console.log('CLOSED', err);
             bot.connected = false;
         });

       });
     }


     controller.defineBot(function(botkit, config) {
       var bot = {
           type: 'socket',
{'fraction_non_alphanumeric': 0.12072892938496584, 'fraction_numerical': 0.0008283288465520812, 'mean_word_length': 1.4295774647887325, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2042737', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1161, 'n_words': 269}