Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
389 changes: 297 additions & 92 deletions app.js

Large diffs are not rendered by default.

215 changes: 142 additions & 73 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,142 @@

var RtmClient = require('@slack/client').RtmClient;
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
var WebClient = require('@slack/client').WebClient;

var obj = {
"text": "Would you like to play a game?",
"attachments": [
{
"text": "Choose a game to play",
"fallback": "You are unable to choose a game",
"callback_id": "wopr_game",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "game",
"text": "Chess",
"type": "button",
"value": "chess"
},
{
"name": "game",
"text": "Falken's Maze",
"type": "button",
"value": "maze"
},
{
"name": "game",
"text": "Thermonuclear War",
"style": "danger",
"type": "button",
"value": "war",
"confirm": {
"title": "Are you sure?",
"text": "Wouldn't you prefer a good game of chess?",
"ok_text": "Yes",
"dismiss_text": "No"
}
}
]
}
]
}


var token = process.env.SLACK_SECRET || '';
var web = new WebClient(token);
var rtm = new RtmClient(token);
rtm.start();

rtm.on(RTM_EVENTS.MESSAGE, function handleRtmMessage(message) {
console.log('Message:', message);
if(message.message.sub_type==='bot_message') {
return;
}
rtm.sendMessage("hello i am seeing and replytin to your meesage", message.channel);
web.chat.postMessage(message.channel, 'Hello there', obj, function(err, res) {
if (err) {
console.log('Error:', err);
} else {
console.log('Message sent: ', res);
}
});
});

rtm.on(RTM_EVENTS.REACTION_ADDED, function handleRtmReactionAdded(reaction) {
console.log('Reaction added:', reaction);
});

rtm.on(RTM_EVENTS.REACTION_REMOVED, function handleRtmReactionRemoved(reaction) {
console.log('Reaction removed:', reaction);
});
// var app = require('./app');
//
// // CONNECTING TO MONGO_DB
// var mongoose = require('mongoose');
// mongoose.connect(process.env.MONGODB_URI);
// var { User } = require('./models');
// var { RtmClient, WebClient, CLIENT_EVENTS, RTM_EVENTS } = require('@slack/client');
// var axios = require('axios');
//
// function imReply(data) {
// return ({"attachments": [
// {
// "text": `Creating a reminder for '${data.result.parameters.subject}' on ${data.result.parameters.date}`,
// "fallback": "You are unable to create reminder",
// "callback_id": "reminder",
// "color": "#3AA3E3",
// "attachment_type": "default",
// "actions": [
// {
// "name": "confrim",
// "text": "Yes",
// "type": "button",
// "value": "yes"
// },
// {
// "name": "confirm",
// "text": "No",
// "type": "button",
// "value": "no"
// },
// ]
// }
// ]})
// }
//
// var token = process.env.SLACK_SECRET || '';
// var web = new WebClient(token);
// var rtm = new RtmClient(token);
// rtm.start();
//
// rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => {
// console.log(`logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
// })
//
// var pendingState = {
// subject: "",
// date: ""
// };
//
// rtm.on(RTM_EVENTS.MESSAGE, function handleRtmMessage(message) {
// var dm = rtm.dataStore.getDMByUserId(message.user);
// console.log("DM--------", dm, "MESSAGE-------", message);
// if (!dm || dm.id !== message.channel || message.type !== 'message') {
// console.log('Message not send to DM, ignoring');
// return;
// }
// //CHECK IF THEY ARE IN MONGO AS HAVING REGISTERED GOOGLE
// var u = rtm.dataStore.getUserById(message.user);
// //CHECK FOR USER OR CREATE ONE
// User.findOne({slack_ID: message.user})
// .then(function(user){
// //SET UP INITIAL SLACK INFO IN MONGO
// if(!user){
// return new User({
// default_meeting_len: 30,
// slack_ID: message.user,
// slack_DM_ID: message.channel,
// slack_Username: u.profile.real_name,
// slack_Email: u.profile.email,
// }).save();
// }
// return user;
// })
// .then(function(user){
// console.log("USER IS", user);
// if(!user.googleAccount){
// //submit the link to grant Google access
// rtm.sendMessage("Hello This is Scheduler bot. In order to schedule reminders for you, I need access to you Google calendar", message.channel);
// web.chat.postMessage(message.channel,
// 'Use this link to give access to your google cal account http://localhost:3000/connect?auth_id='
// + user._id);
// return;
// }
//
// axios.get('https://api.api.ai/api/query', {
// params: {
// v: 20150910,
// lang: 'en',
// // timezone: '2017-07-17T16:58:21-0700',
// query: message.text,
// sessionId: message.user
// },
// headers: {
// Authorization: `Bearer ${process.env.API_AI_TOKEN}`
// }
// })
// .then(function( { data } ) {
// console.log("DATA", data, "DATA-messages", data.result.fulfillment.messages);
// if(!data.result.actionIncomplete && data.result.parameters.date && data.result.parameters.subject ) {
// // rtm.sendMessage(data.result.fulfillment.speech, message.channel);
// pendingState = data.result.parameters;
// web.chat.postMessage(message.channel, 'Chill homie', imReply(data), function(err, res) {
// if (err) {
// console.log('Error:', err);
// } else {
// console.log('Message sent: ', res);
// }
// });
// } else if(data.result.parameters.date && !data.result.parameters.subject){
// console.log('NO SUBJECT');
// pendingState.date = data.result.parameters.date;
// rtm.sendMessage(data.result.fulfillment.speech, message.channel);
// } else if(data.result.parameters.subject && !data.result.parameters.date){
// console.log('NO DATE');
// pendingState.subject = data.result.parameters.subject;
// rtm.sendMessage(data.result.fulfillment.speech, message.channel);
// } else {
// rtm.sendMessage(data.result.fulfillment.speech, message.channel);
// }
// console.log("HOW IS PENDING STATE LOOKING RIGHT NOW? HMM?", pendingState);
// return pendingState;
// })
// .catch(function(err) {
// console.log("ERROR", err);
// })
//
// });
// });
//
// rtm.on(RTM_EVENTS.REACTION_ADDED, function handleRtmReactionAdded(reaction) {
// console.log('Reaction added:', reaction);
// });
//
// rtm.on(RTM_EVENTS.REACTION_REMOVED, function handleRtmReactionRemoved(reaction) {
// console.log('Reaction removed:', reaction);
// });
//
//
// module.exports = {
// rtm: rtm,
// pendingState: pendingState // this doesn't work!!!
// };
49 changes: 49 additions & 0 deletions cronjob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI);

var { User, Reminder } = require('./models');
var { rtm } = require('./app');

Reminder.find({}, function(err, reminders) {
console.log("REMINDERS", reminders);
if(err) {
console.log('There was an error with finding the reminders');
} else {
// reminders is an array of reminder JSONs
// const curDate = new Date().toLocaleDateString();
const curDate = new Date().toISOString().split('T')[0];
// sets up the next day
const tomDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000).toISOString().split('T')[0];
// const tomDay = parseInt(curDate.split('/')[1]) + 1;
// let tomDate = curDate.split('/')
// tomDate[1] = parseInt(tomDate[1]) + 1;
// tomDate = tomDate.join('/')
console.log("TODAY DATE", curDate, "TOMORROW DATE", tomDate);

User.find({}, function(err, users) {
console.log("YO! I FOUND USERS!", users);
});

reminders.forEach(function(reminder) {
if( curDate === reminder.day ) { //On due day of reminder, send Slack msg & delete the reminder doc
console.log("Reminder now", reminder);
console.log('need to send RTM message here');
User.findOne({slack_ID: reminder.reqID}, function(err, user) {
console.log("TODAY, USER iS", user);
rtm.sendMessage(`Reminder! You gotta remember to ${reminder.subject} today bro!`, user.slack_DM_ID)
if(!err) {
Reminder.remove({reqID: reminder.reqID}, function(err) {
if(err) {
console.log("Error removing reminder for today!");
}
})
}
})
} else if ( tomDate === reminder.day ) { //On day before due day of reminder, send Slack msg to app user
User.findOne({slack_ID: reminder.reqID}, function(err, user) {
rtm.sendMessage(`Reminder! You gotta remember to ${reminder.subject} tomorrow bro!`, user.slack_DM_ID)
})
}
})
}
})
20 changes: 0 additions & 20 deletions index.js

This file was deleted.

42 changes: 36 additions & 6 deletions models.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI);

var userSchema = mongoose.Schema({
username: String,
password: String,
phone: String
// googleAccount: {
// access_token: String,
// refresh_token: String,
// profile_ID: String,
// profile_name: String
// },
googleAccount: {},
slack_ID: {
type: String,
required: true
},
slack_DM_ID: {
type: String,
required: true
},
slack_Username: String,
slack_Email: String
});

var reminderSchema = mongoose.Schema({
// slack_DM_ID: String,
subject: {
required: true,
type: String
},
day: {
required: true,
type: String
},
googCalID: String,
reqID: String
})


User = mongoose.model('User', userSchema);
var User = mongoose.model('User', userSchema);
var Reminder = mongoose.model('Reminder', reminderSchema);

module.exports = {
User:User
User: User,
Reminder: Reminder
};
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
},
"dependencies": {
"@slack/client": "^3.10.0",
"body-parser": "~1.13.2",
"axios": "^0.16.2",
"body-parser": "^1.13.3",
"client-oauth2": "^4.1.0",
"connect-mongo": "^1.3.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.13.1",
"express-handlebars": "^3.0.0",
"express-session": "^1.13.0",
"googleapis": "^20.1.0",
"hbs": "~3.1.0",
"mongoose": "^4.5.1",
"mongoose": "^4.11.3",
"mongoose-findorcreate": "^0.1.2",
"morgan": "~1.6.1",
"multer": "^1.1.0",
Expand Down
1 change: 0 additions & 1 deletion public/scripts/script.js

This file was deleted.

5 changes: 0 additions & 5 deletions public/stylesheets/style.css

This file was deleted.

Loading