Skip to content

Instantly share code, notes, and snippets.

@semateos
Created May 5, 2016 14:51
Show Gist options
  • Save semateos/5027f306f719ec1c9e136bb5ac90fcbf to your computer and use it in GitHub Desktop.
Save semateos/5027f306f719ec1c9e136bb5ac90fcbf to your computer and use it in GitHub Desktop.
// This is the main Bot interface
var superscript = require("superscript");
var mongoose = require("mongoose");
var fs = require("fs");
var parse = require("ss-parser")();
mongoose.connect('mongodb://localhost/superscriptDB');
//websocket client to interact with Altered
var alteredClient = require('altered-api-client');
var facts = require("sfacts");
var factSystem = facts.explore("botfacts");
var TopicSystem = require("superscript/lib/topics/index")(mongoose, factSystem);
// How should we reply to the user?
// direct - sents a DM
// atReply - sents a channel message with @username
// public sends a channel reply with no username
var replyType = "atReply";
var atReplyRE = /@\[(.*?)\]/;
var options = {};
options['factSystem'] = factSystem;
options['mongoose'] = mongoose;
//var slack = new Slack(token, true, true);
var botHandle = function(err, bot) {
alteredClient.login(function(err, data){
console.log('logged in to altered', data);
});
alteredClient.events.on('error', function(error) {
console.error("Error:");
console.log(error);
});
alteredClient.events.on('open', function(){
//console.log("Welcome to Slack. You are %s of %s", slack.self.name, slack.team.name);
});
alteredClient.events.on('close', function() {
console.warn("Disconnected");
});
alteredClient.events.on('new-notification', function(data) {
receiveData(alteredClient, bot, data);
});
};
var receiveData = function(slack, bot, data) {
var interaction = data.info.interact;
var user = interaction.owner;
var room = interaction.room;
var message = interaction.body;
console.log('message from:', user);
var match = message.match(atReplyRE);
message = message.replace(atReplyRE, '').trim();
if (message[0] == ':') {
message = message.substring(1).trim();
}
bot.reply(user.name, message, function(err, reply){
if (reply.string) {
console.log('reply:', reply.string);
//channel.send(reply.string);
}
});
};
var parseTopics = function(){
parse.loadDirectory('./topics', function(err, result){
fs.writeFile('data.json', JSON.stringify(result, null, 4), function (err) {
if (err) throw err;
console.log('Saved output to data.json');
mongoose.connection.db.dropDatabase();
TopicSystem.importerFile('./data.json', function(){
console.log('imported topics');
});
});
});
};
// watch topics for changes
require('chokidar').watch('./topics', {ignored: /[\/\\]\./}).on('all', function(event, path) {
console.log(event, path);
switch (event) {
case 'change':
parseTopics();
break;
}
});
new superscript(options, function(err, botInstance){
botHandle(null, botInstance);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment