-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.js
More file actions
165 lines (153 loc) · 6.26 KB
/
bot.js
File metadata and controls
165 lines (153 loc) · 6.26 KB
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
157
158
159
160
161
162
163
164
165
import { RtmClient, WebClient, CLIENT_EVENTS, RTM_EVENTS } from '@slack/client';
import axios from 'axios';
import express from 'express';
import { messageConfirmation, getQueryParams } from './constants';
import { User } from './models';
const router = express.Router();
const botToken = process.env.SLACK_BOT_TOKEN || '';
const rtm = new RtmClient(botToken);
const web = new WebClient(botToken);
let channel = 'T6AVBE3GX';
// The client will emit an RTM.AUTHENTICATED event on successful connection, with the `rtm.start` payload
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => {
for (const c of rtmStartData.channels) {
if (c.is_member && c.name === 'general') { channel = c.id; }
}
console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
});
// you need to wait for the client to fully connect before you can send messages
rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, () => {
// things to do when the bot connects to slack
});
const mapping = {};
rtm.on(RTM_EVENTS.MESSAGE, (msg) => {
const dm = rtm.dataStore.getDMByUserId(msg.user);
if (!dm || dm.id !== msg.channel || msg.type !== 'message') {
return;
}
const bool = msg.text.includes('<@');
if(bool) {
const arr = [];
let temp = [];
for(let a = 0; a < msg.text.length; a++) {
if(msg.text[a] === '@') {
temp.push(a);
}
if(msg.text[a] === '>') {
temp.push(a);
arr.push(temp);
temp = [];
}
}
console.log(msg.text);
for(let b = 0; b < arr.length; b++) {
const temp = arr[b];
console.log(temp);
}
for(let b = 0; b < arr.length; b++) {
const i = msg.text.indexOf('@');
const j = msg.text.indexOf('>');
console.log(i, j, msg.text);
const id = msg.text.slice(i + 1, j);
console.log("id", id);
const username = rtm.dataStore.getUserById(id).profile.first_name;
const reg = /(\<.*?\>)/i;
mapping[username] = id;
const newMessage = msg.text.replace(reg, username);
msg.text = newMessage;
console.log(username, newMessage);
}
// const i = msg.text.indexOf('@');
// const j = msg.text.indexOf('>');
// const id = msg.text.slice(i + 1, j);
// const username = rtm.dataStore.getUserById(id).profile.first_name;
// const reg = /(\<.*?\>)/gi;
// mapping[username] = id;
// const newMessage = msg.text.replace(reg, username);
// console.log(username, newMessage);
// msg.text = newMessage;
}
User.findOne({slackId: msg.user})
.then((user) => {
if (!user) {
return new User(
{
slackId: msg.user,
slackDmId: msg.channel,
google: {}
}).save();
}
return user;
})
.then((user) => {
console.log('USER is', rtm.dataStore.getUserById(user.slackId));
if (!user.google) {
rtm.sendMessage(`Hello this is scheduler bot. I need to schedule reminders. Please visit http://glacial-shelf-50059.herokuapp.com/connect?user=${user._id} to setup Google Calendar`, msg.channel);
} else {
getQuery(msg.text, msg.user)
.then(({ data }) => {
// if (JSON.parse(user).pending && (JSON.parse(user.pending).type === 'meeting' || JSON.parse(user.pending).type === 'reminder') ) {
// rtm.sendMessage("Please select a choice before moving on", msg.channel);
// return;
// }
switch (data.result.action) {
case 'meeting.add':
if (data.result.actionIncomplete) {
rtm.sendMessage(data.result.fulfillment.speech, msg.channel);
} else {
console.log('Finish', data);
const ids = [];
for (const key in mapping) {
if (mapping.hasOwnProperty(key)) {
ids.push(mapping[key]);
}
}
// let text = data.result.fulfillment.speech;
// const startIndex = text.indexOf('with');
// const endIndex = text.indexOf('at');
// text = text.slice(startIndex + 5, endIndex - 1).trim();
// console.log(mapping);
web.chat.postMessage(msg.channel, data.result.fulfillment.speech, messageConfirmation(data.result.fulfillment.speech, "remember to add code to actaully cancel the meeting/not schedule one"));
user.pending = JSON.stringify(Object.assign({}, data.result.parameters, { type: 'meeting', ids: ids }));
user.save();
}
break;
case 'reminder.add':
if (data.result.actionIncomplete) {
rtm.sendMessage(data.result.fulfillment.speech, msg.channel);
} else {
console.log('Finish', data);
// global_state = data.result.parameters;
web.chat.postMessage(msg.channel, data.result.fulfillment.speech, messageConfirmation(data.result.fulfillment.speech, "remember to add code to actaully cancel the meeting/not schedule one"));
// David removed the next 3 lines. I don't think that's right, but if the program is broken try removing them
console.log('data.result.parameters is ', data.result.parameters);
user.pending = JSON.stringify(Object.assign({}, data.result.parameters, { type: 'reminder' }));
user.save();
// the above 3 lines
}
break;
default:
console.log('default statement');
console.log(data.result.action);
if (data.result.action === 'bestbot.reply' || data.result.action.startsWith('smalltalk.')) {
rtm.sendMessage(data.result.fulfillment.speech, msg.channel);
}
return;
}
return;
})
.catch((err) => {
console.log('error is ', err);
});
}
});
});
function getQuery(msg, sessionId) {
return axios.get('https://api.api.ai/api/query', {
params: getQueryParams(msg, sessionId),
headers: {
Authorization: `Bearer ${process.env.API_AI_TOKEN}`
}
});
}
export { web, rtm };