<commit_before>'use strict';

const config = require('./config');

const login = require('facebook-chat-api');
const client = require('twilio')(config.twilio.accountSid, config.twilio.authToken);

const Person = require('./models/person');
const Messages = require('./models/messages');

login({email: config.fb.email, password: <PASSWORD>}, function callback(err, api) {
  if (err) return console.error(err);

  var listen = api.listen(function callback(err, message) {
    if (err) return console.error(err);

    var threadID = message.threadID;
    var body = message.body.toLowerCase(); 
    var sender = message.senderID;

    if (body.includes('@facebot ')) {
      body = body.slice('@facebot '.length);
      if (body.includes('register ')) {
        body = body.slice('register '.length).split('`');

        Person.create({name: body[0].trim(), number: body[1]}, function(err, person) {
          if (err) return HandleError(err);
          else console.log('New user added')
        });

      } else if (body.includes('send ')) {
        body = body.slice('send '.length).split('`');
        Person.findOne({name: body[0].trim()}, function(err, person) {
          if (err) return HandleError(err);
          client.sms.messages.create({
            body: body[1],
            to: person.number,
            from: config.twilio.number
          }, function(err, sms) {
            if (!err) console.log(sms.sid);
            else console.dir(err);
          });
        });
      }
    }
    client.messages.list({}, function(err, data) { 
      data.messages.forEach(function(message) { 
        if (message.to == config.twilio.number) {
          FacebotModel.findOne({ number: message.from }, function(err, entry) {
            api.sendMessage(entry.name + ' says ' + message.body, threadID);
          });
        }
      }); 
    });
  });
});<commit_msg>Add Schema to track sent messages (no more duplicates 😁)<commit_after>'use strict';

const config = require('./config');

const login = require('facebook-chat-api');
const client = require('twilio')(config.twilio.accountSid, config.twilio.authToken);

const Person = require('./models/person');
const Messages = require('./models/messages');

login({email: config.fb.email, password: <PASSWORD>}, function callback(err, api) {
  if (err) return console.error(err);

  var listen = api.listen(function callback(err, message) {
    if (err) return console.error(err);

    var threadID = message.threadID;
    var body = message.body.toLowerCase(); 
    var sender = message.senderID;

    if (body.includes('@facebot ')) {
      body = body.slice('@facebot '.length);
      if (body.includes('register ')) {
        body = body.slice('register '.length).split('`');

        Person.create({name: body[0].trim(), number: body[1]}, function(err, person) {
          if (err) return HandleError(err);
          else console.log('New user added')
        });

      } else if (body.includes('send ')) {
        body = body.slice('send '.length).split('`');
        Person.findOne({name: body[0].trim()}, function(err, person) {
          if (err) return HandleError(err);
          client.sms.messages.create({
            body: body[1],
            to: person.number,
            from: config.twilio.number
          }, function(err, sms) {
            if (!err) console.log(sms.sid);
            else console.dir(err);
          });
        });
      }
    }
    client.messages.list({}, function(err, data) { 
      data.messages.forEach(function(sms) {
        if ( sms.to == config.twilio.number ) {

          Messages.count( { sid: sms.sid }, function(err, count) {
            if ( count == 0 ) {
              Person.findOne({number: sms.from}, function(err, person) {
                if (err) return HandleError(err);
                api.sendMessage(person.name + ' says ' + sms.body + ' (' + sms.date_sent + ')', threadID);
              });
              Messages.create({sid: sms.sid}, function(err, message) {
                if (err) return HandleError(err);
                else console.log('Message added')
              });
            }
          });
        }
      }); 
    });
  });
});
{'fraction_non_alphanumeric': 0.16666666666666666, 'fraction_numerical': 0.0021551724137931034, 'mean_word_length': 2.3821862348178136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2413888', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1220, 'n_words': 262}