const express = require('express');
const bodyParser = require('body-parser');
const schedule = require('node-schedule');
const twitterbot = require('./lib/twitterbot.js');
const gh = require('./lib/github.js');
const handlers = require('./lib/cmd-handlers.js');

const app = express();
const port = process.env.PORT || 3000;
const validToken = require('./lib/database.js').validToken;

/**
 * Ariadne 2.0 - Superior Productivity
 * Copyright (C) 2016 Dewitt Buckingham (http://zerodaedalus.com)
 *
 * TODO: Add some better logging utility. (Winston?)
 * TODO: Integration testing
 */
console.log('waking up...');

/**
 * CONFIGURE SERVER
 */

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));


app.post('/api/tweet', confirmToken, (req, res) => {
  twitterbot.tweet(req.body.status, req.body.params, (err) => {
    if (err) {
      console.log(err);
      res.sendStatus(500);
    } else {
      res.sendStatus(200);
    }
  });
});

app.get('/api', (req, res) => {
  res.sendStatus(200);
});

app.get('*', (req, res) => {
  res.redirect(301, 'https://twitter.com/ariadnebot');
});

/**
 * INITIALIZE TWITTERBOT
 */

twitterbot.init((err) => {
  // Check if twitterbot failed to intialize.
  if (err) {
    console.log(`twitterbot failed to initialize: ${err}`);
  } else {
    /**
     * INITIALIZE GITHUB
     */
    
    gh.init((err) => {
      if (err) {
        console.log(`failed to pull github info: ${err}`);
      } else {
        console.log('github info loaded');
        console.log('loading modules...');
        twitterbot.attach('info', 'A little helpful information about Ariadne.', 'info', handlers.INFO);
        twitterbot.attach('github', 'Get information about a specific Github user or repository', 'github <user OR user/repo>', handlers.GITHUB);
        twitterbot.attach('test', 'Simple test to see if Ariadne is up and running', 'test', handlers.TEST);
        twitterbot.attach('thanks', 'Everyone appreciates a little gratitude.', 'thanks', handlers.THANKS);
        twitterbot.attach('math', 'Solve math problems', 'math <problem>', handlers.MATH);
        twitterbot.attach('help', 'Some help with Ariadne\'s commands', 'help [command]', handlers.HELP);

        // Launch REST API
        app.listen(port, () => {
          console.log(`Ariadne REST API started on port ${port}`);
        });
      }
    });

    /**
     * schedule repeating messages
     */
    schedule.scheduleJob('0 */3 * * *', () => {
      twitterbot.randomQuote();
    });
  }
});

/**
 * Handle uncaught exceptions
 */
process.on('uncaughtException', (err) => {
  console.error('caught exception:');
  console.error(err);
  console.trace();
});

/**
 * MIDDLEWARE
 */

function confirmToken(req, res, next) {
  console.log('Confirming access token...');
  validToken(req.body.auth, (err) => {
    if (err) {
      console.log('Token invalid...');
      res.sendStatus(403);      
    } else {
      console.log('Token confirmed...');
      next();      
    }
  });
}

@@ -68,24 +68,27 @@
 y-parser');%0A
+// 
 const schedu
@@ -2369,32 +2369,107 @@
     %7D);%0A%0A   
+ // REMOVED: Because tweets are bugged if not replying to a message.%0A    //
  /**%0A   
   * schedule
@@ -2452,24 +2452,27 @@
   // /**%0A   
+ //
   * schedule
@@ -2494,24 +2494,27 @@
 ages%0A   
+ //
   */%0A   
  schedul
@@ -2505,16 +2505,19 @@
   */%0A   
+ //
  schedul
@@ -2556,16 +2556,19 @@
 =%3E %7B%0A   
+ //
    twitt
@@ -2587,24 +2587,27 @@
 Quote();%0A   
+ //
  %7D);%0A  %7D%0A%7D);

Remove Quote Feature