<commit_before>/**
 * Created by thomasjeanneau on 08/02/2017.
 */

import Botkit from 'botkit';
import jsonfile from 'jsonfile';
import Airtable from 'airtable';

const settings = jsonfile.readFileSync('settings.json');

Airtable.configure({
  endpointUrl: 'https://api.airtable.com',
  apiKey: settings.airtable_api_key
});

const port = process.env.PORT || 3000;
const base = Airtable.base(settings.airtable_base_key);
const controller = Botkit.slackbot({
  debug: false,
  interactive_replies: true
});

controller.configureSlackApp({
  clientId: settings.slack_client_id,
  clientSecret: settings.slack_client_secret,
  redirectUri: 'https://mangrove-pairing.herokuapp.com/',
  scopes: ['bot']
});

controller.setupWebserver(port, function(err, webserver) {
  controller
    .createHomepageEndpoint(webserver)
    .createOauthEndpoints(webserver,function(err,req,res) {
      res.send(':)');
    })
    .createWebhookEndpoints(webserver);
});

controller.spawn({
  token: settings.bot_access_token,
}).startRTM();

controller.hears('hello', ['direct_message', 'direct_mention', 'mention'], (bot, message) => {
  bot.reply(message, 'Hello you :D');
});

controller.hears('show mangrove friends', ['direct_message', 'direct_mention', 'mention'], (bot, message) => {
  base('P2PL Tests').select({
    maxRecords: 150,
    view: "Main View",
  }).eachPage(function page(records, fetchNextPage) {
    records.forEach(function (record) {
      bot.reply(message, record.get('Name'));
    });
    fetchNextPage();
  }, function done(err) {
    if (err) {
      console.error(err);
      return;
    }
  });
});

controller.hears('show P2PL applicants', ['direct_message', 'direct_mention', 'mention'], (bot, message) => {
  base('P2PL Tests').select({
    maxRecords: 150,
    view: "Main View",
    fields: ["Name", "Interests", "Skills"]
  }).eachPage(function page(records, fetchNextPage) {
    records.forEach(function (record) {
      if (record.get('Interests') && record.get('Skills')) {
        bot.reply(message, {
          'text': `:sparkles: _${record.get('Name')}_ :sparkles:`,
          'attachments': [
            {
              'title': ':sleuth_or_spy: Interests',
              'text': record.get('Interests').join(', '),
              'color': '#9575CD'
            },
            {
              'title': ':muscle: Skills',
              'text': record.get('Skills').join(', '),
              'color': '#E57373'
            }
          ],
        });
      }
    });
    fetchNextPage();
  }, function done(err) {
    if (err) {
      console.error(err);
      return;
    }
  });
});

controller.hears('feedback', 'direct_message', function (bot, message) {
  bot.startConversation(message, function (err, convo) {
    convo.ask({
        attachments: [
          {
            title: 'Hey you ! I\'ve you done your last pairing ? ',
            callback_id: '123',
            attachment_type: 'default',
            actions: [
              {
                "name": "yes",
                "text": "Yes",
                "value": "yes",
                "type": "button",
              },
              {
                "name": "no",
                "text": "No",
                "value": "no",
                "type": "button",
              }
            ]
          }
        ]
      },
      [
        {
          pattern: 'yes',
          callback: function (response, convo) {
            convo.say('Great! I will continue...');
            // do something else...
            convo.next();

          }
        },
        {
          pattern: 'no',
          callback: function (response, convo) {
            convo.say('Perhaps later.');
            // do something else...
            convo.next();
          }
        },
        {
          default: true,
          callback: function (response, convo) {
            // just repeat the question
            convo.repeat();
            convo.next();
          }
        }
      ]
    )
    ;

  })

});<commit_msg>Add pair command to fire python code<commit_after>/**
 * Created by thomasjeanneau on 08/02/2017.
 */

import Botkit from 'botkit';
import jsonfile from 'jsonfile';
import Airtable from 'airtable';

const settings = jsonfile.readFileSync('settings.json');

var exec = require('child_process').exec;

Airtable.configure({
  endpointUrl: 'https://api.airtable.com',
  apiKey: settings.airtable_api_key
});

const port = process.env.PORT || 3000;
const base = Airtable.base(settings.airtable_base_key);
const controller = Botkit.slackbot({
  debug: false,
  interactive_replies: true
});

controller.configureSlackApp({
  clientId: settings.slack_client_id,
  clientSecret: settings.slack_client_secret,
  redirectUri: 'https://mangrove-pairing.herokuapp.com/',
  scopes: ['bot']
});

controller.setupWebserver(port, function(err, webserver) {
  controller
    .createHomepageEndpoint(webserver)
    .createOauthEndpoints(webserver,function(err,req,res) {
      res.send(':)');
    })
    .createWebhookEndpoints(webserver);
});

controller.spawn({
  token: settings.bot_access_token,
}).startRTM();

controller.hears('hello', ['direct_message', 'direct_mention', 'mention'], (bot, message) => {
  bot.reply(message, 'Hello you :D');
});

controller.hears('show mangrove friends', ['direct_message', 'direct_mention', 'mention'], (bot, message) => {
  base('P2PL Tests').select({
    maxRecords: 150,
    view: "Main View",
  }).eachPage(function page(records, fetchNextPage) {
    records.forEach(function (record) {
      bot.reply(message, record.get('Name'));
    });
    fetchNextPage();
  }, function done(err) {
    if (err) {
      console.error(err);
      return;
    }
  });
});

controller.hears('show P2PL applicants', ['direct_message', 'direct_mention', 'mention'], (bot, message) => {
  base('P2PL Tests').select({
    maxRecords: 150,
    view: "Main View",
    fields: ["Name", "Interests", "Skills"]
  }).eachPage(function page(records, fetchNextPage) {
    records.forEach(function (record) {
      if (record.get('Interests') && record.get('Skills')) {
        bot.reply(message, {
          'text': `:sparkles: _${record.get('Name')}_ :sparkles:`,
          'attachments': [
            {
              'title': ':sleuth_or_spy: Interests',
              'text': record.get('Interests').join(', '),
              'color': '#9575CD'
            },
            {
              'title': ':muscle: Skills',
              'text': record.get('Skills').join(', '),
              'color': '#E57373'
            }
          ],
        });
      }
    });
    fetchNextPage();
  }, function done(err) {
    if (err) {
      console.error(err);
      return;
    }
  });
});

controller.hears('feedback', 'direct_message', function (bot, message) {
  bot.startConversation(message, function (err, convo) {
    convo.ask({
        attachments: [
          {
            title: 'Hey you ! I\'ve you done your last pairing ? ',
            callback_id: '123',
            attachment_type: 'default',
            actions: [
              {
                "name": "yes",
                "text": "Yes",
                "value": "yes",
                "type": "button",
              },
              {
                "name": "no",
                "text": "No",
                "value": "no",
                "type": "button",
              }
            ]
          }
        ]
      },
      [
        {
          pattern: 'yes',
          callback: function (response, convo) {
            convo.say('Great! I will continue...');
            // do something else...
            convo.next();

          }
        },
        {
          pattern: 'no',
          callback: function (response, convo) {
            convo.say('Perhaps later.');
            // do something else...
            convo.next();
          }
        },
        {
          default: true,
          callback: function (response, convo) {
            // just repeat the question
            convo.repeat();
            convo.next();
          }
        }
      ]
    )
    ;

  })

});

controller.hears("pair", "direct_message", function(bot,message){
  var admins = []
    base('P2PL Tests').select({
      filterByFormula: "{Pairing admin}=1"
    }).eachPage(function page (records, fetchNextPage){
      records.forEach((record)=>{
        // console.log(record);
        admins.push(record["fields"]["Slack Handle"])
      })
      fetchNextPage();
    }, function done(err){
      if (err) { console.error(err); return; }
      bot.api.users.info({user: message.user}, (error, response) => {
        let {name, real_name} = response.user;
        if(admins.indexOf(name) >= 0){
          bot.reply(message,"Ok, I'll start pairing people");
          exec("python pairing.py", function(error, stdout, stderr){
            if (!error){
              bot.reply(message, "Pairing complete. Results should be available in airtable. You can run 'introductions' to send a message to each pair.")
            } else {
              console.log(error);
              bot.reply(message, "An error occurred during pairing.")
            }
          })
        } else {
          bot.reply(message,"Sorry but it looks like you're not an admin. You can't use this feature.")
        }
      })
    });
})
{'fraction_non_alphanumeric': 0.1593311758360302, 'fraction_numerical': 0.00744336569579288, 'mean_word_length': 2.0719019218025183, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 4, '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': '2425009', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2765, 'n_words': 565}