-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
156 lines (137 loc) · 4.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const app = express()
const Cosmic = require('cosmicjs')
const BootBot = require('bootbot')
require('dotenv').config()
const chrono = require('chrono-node')
var schedule = require('node-schedule')
const EventEmitter = require('events').EventEmitter
var config = {}
const reminders = []
const eventEmitter = new EventEmitter()
app.set('port', (process.env.PORT || 5000))
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.get('/', function(req, res) {
res.send("hey there boi")
})
app.get('/webhook/', function(req, res) {
if (req.query['hub.verify_token'] === process.env.VERIFY_TOKEN){
return res.send(req.query['hub.challenge'])
}
res.send('wrong token')
})
app.listen(app.get('port'), function(){
console.log('Started on port', app.get('port'))
})
const bot = new BootBot({
accessToken: process.env.ACCESS_TOKEN,
verifyToken: process.env.VERIFY_TOKEN,
appSecret: process.env.APP_SECRET
})
bot.setGreetingText("Hello, I'm here to help you manage your tasks. Be sure to setup your bucket by typing 'Setup'. ")
bot.setGetStartedButton((payload, chat) => {
if(config.bucket === undefined){
chat.say('Hello my name is Note Buddy and I can help you keep track of your thoughts')
chat.say("It seems like you have not setup your bucket settings yet. That has to be done before you can do anything else. Make sure to type 'setup'")
}
BotUserId = payload.sender.id
});
bot.hear('setup', (payload, chat) => {
const getBucketSlug = (convo) => {
convo.ask("What's your Bucket's slug?", (payload, convo) => {
var slug = payload.message.text;
convo.set('slug', slug)
convo.say("setting slug as "+slug).then(() => getBucketReadKey(convo));
})
}
const getBucketReadKey = (convo) => {
convo.ask("What's your Bucket's read key?", (payload, convo) => {
var readkey = payload.message.text;
convo.set('read_key', readkey)
convo.say('setting read_key as '+readkey).then(() => getBucketWriteKey(convo))
})
}
const getBucketWriteKey = (convo) => {
convo.ask("What's your Bucket's write key?", (payload, convo) => {
var writekey = payload.message.text
convo.set('write_key', writekey)
convo.say('setting write_key as '+writekey).then(() => finishing(convo))
})
}
const finishing = (convo) => {
var newConfigInfo = {
slug: convo.get('slug'),
read_key: convo.get('read_key'),
write_key: convo.get('write_key')
}
config.bucket = newConfigInfo
convo.say('All set :)')
convo.end();
}
chat.conversation((convo) => {
getBucketSlug(convo)
})
})
bot.hear(['hello', 'hey', 'sup'], (payload, chat)=>{
chat.getUserProfile().then((user) => {
chat.say(`Hey ${user.first_name}, How are you today?`)
})
})
bot.hear('config', (payload, chat) => {
if(JSON.stringify(config.bucket) === undefined){
chat.say("No config found :/ Be sure to run 'setup' to add your bucket details")
}
chat.say("A config has been found :) "+ JSON.stringify(config.bucket))
})
bot.hear('create', (payload, chat) => {
chat.conversation((convo) => {
convo.ask("What would you like your reminder to be? etc 'I have an appointment tomorrow from 10 to 11 AM' the information will be added automatically", (payload, convo) => {
datetime = chrono.parseDate(payload.message.text)
var params = {
write_key: config.bucket.write_key,
type_slug: 'reminders',
title: payload.message.text,
metafields: [
{
key: 'date',
type: 'text',
value: datetime
}
]
}
Cosmic.addObject(config, params, function(error, response){
if(!error){
eventEmitter.emit('new', response.object.slug, datetime)
convo.say("reminder added correctly :)")
convo.end()
} else {
convo.say("there seems to be a problem. . .")
convo.end()
}
})
})
})
})
bot.hear('help', (payload, chat) => {
chat.say('Here are the following commands for use.')
chat.say("'create': add a new reminder")
chat.say("'setup': add your bucket info such as slug and write key")
chat.say("'config': lists your current bucket config")
})
eventEmitter.on('new', function(itemSlug, time) {
schedule.scheduleJob(time, function(){
Cosmic.getObject(config, {slug: itemSlug}, function(error, response){
if(response.object.metadata.date == new Date(time).toISOString()){
bot.say(BotUserId, response.object.title)
console.log('firing reminder')
} else {
eventEmitter.emit('new', response.object.slug, response.object.metafield.date.value)
console.log('times do not match checking again at '+response.object.metadata.date)
}
})
})
})
bot.start()