-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
232 lines (180 loc) · 7.88 KB
/
app.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const { createEventAdapter } = require('@slack/events-api');
const { createMessageAdapter } = require('@slack/interactive-messages');
const cloneDeep = require('lodash.clonedeep');
const cors = require('cors');
const bot = require('./src/slackbot');
const api = require('./src/routes/api');
const connection = require('./src/config/connection')
const axios = require('axios');
// Create the server
const app = express();
const SLACK_SECRET_SIGNED = process.env.SLACK_SECRET_SIGNED
const slackEvents = createEventAdapter(SLACK_SECRET_SIGNED);
const slackInteractions = createMessageAdapter(SLACK_SECRET_SIGNED);
slackInteractions.start().catch(error => console.log(error));
slackEvents.on('app_mention', (event) => {
bot.introduceToUser(event);
});
slackEvents.on('message', (event) => {
// Filter out messages from this bot itself or updates to messages
if (event.subtype === 'bot_message' || event.subtype === 'message_changed') {
return;
}
bot.handleDirectMessage(event);
});
// Helper functions
function findAttachment(message, actionCallbackId) {
return message.attachments.find(a => a.callback_id === actionCallbackId);
}
function acknowledgeActionFromMessage(originalMessage, actionCallbackId, ackText) {
const message = cloneDeep(originalMessage);
const attachment = findAttachment(message, actionCallbackId);
delete attachment.actions;
attachment.text = `:white_check_mark: ${ackText}`;
return message;
}
function findSelectedOption(originalMessage, actionCallbackId, selectedValue) {
const attachment = findAttachment(originalMessage, actionCallbackId);
return attachment.actions[0].options.find(o => o.value === selectedValue);
}
function findSelectedbuttons(originalMessage, actionCallbackId, selectedValue) {
const attachment = findAttachment(originalMessage, actionCallbackId);
return attachment.actions.find(o => o.value === selectedValue);
}
// Action handling
slackInteractions.action('order:start', (payload, respond) => {
// Create an updated message that acknowledges the user's action (even if the result of that
// action is not yet complete).
const updatedMessage = acknowledgeActionFromMessage(payload.original_message, 'order:start',
'I\'m getting your appointment started for you.');
const selectedType = findSelectedbuttons(payload.original_message, 'order:start', payload.actions[0].value);
// set the user user response
bot.setUserResponse("how_are_you", `${selectedType.text.toLowerCase()}`);
// Start an order, and when that completes, send another message to the user.
bot.startAppointment(payload)
.then(respond)
.catch(console.error);
// The updated message is returned synchronously in response
return updatedMessage;
});
slackInteractions.action('order:select_day', (payload, respond) => {
const selectedType = findSelectedOption(payload.original_message, 'order:select_day', payload.actions[0].selected_options[0].value);
const updatedMessage = acknowledgeActionFromMessage(payload.original_message, 'order:select_day',
`Your appointment day is *${selectedType.text.toLowerCase()}.*`);
// set the user user response
bot.setUserResponse("appointment_day", `${selectedType.text.toLowerCase()}`);
bot.selectAppointmentDay(payload.user.id, selectedType.value)
.then((response) => {
// Keep the context from the updated message but use the new text and attachment
updatedMessage.text = response.text;
if (response.attachments && response.attachments.length > 0) {
updatedMessage.attachments.push(response.attachments[0]);
}
return updatedMessage;
})
.then(respond)
.catch(console.error);
return updatedMessage;
});
slackInteractions.action('order:select_time', (payload, respond) => {
try {
const selectedType = findSelectedOption(payload.original_message, 'order:select_time', payload.actions[0].selected_options[0].value);
const updatedMessage = acknowledgeActionFromMessage(payload.original_message, 'order:select_time',
`Your appointment time is *${selectedType.text.toLowerCase()}*.`);
// set the user user response
bot.setUserResponse("appointment_time", `${selectedType.text.toLowerCase()}`);
bot.selectUserHobby(payload, selectedType.value).then(response => {
updatedMessage.text = response.text;
if (response.attachments && response.attachments.length > 0) {
updatedMessage.attachments.push(response.attachments[0]);
}
return updatedMessage;
})
.then(respond)
.catch(console.error);
return updatedMessage;
} catch (error) {
console.log(`Time Error :::::: ${error}`);
}
});
slackInteractions.action('order:select_number_scale', (payload, respond) => {
try {
const selectedType = findSelectedOption(payload.original_message, 'order:select_number_scale', payload.actions[0].selected_options[0].value);
const updatedMessage = acknowledgeActionFromMessage(payload.original_message, 'order:select_number_scale',
`Your number scale is *${selectedType.text.toLowerCase()}*.`);
// set the user user response
bot.setUserResponse("digit_scale", `${selectedType.text.toLowerCase()}`);
bot.openThankYou(payload, selectedType.value)
.then((response) => {
// Keep the context from the updated message but use the new text and attachment
updatedMessage.text = response.text;
if (response.attachments && response.attachments.length > 0) {
updatedMessage.attachments.push(response.attachments[0]);
}
return updatedMessage;
})
.then(respond)
.catch(console.error);
return updatedMessage;
} catch (error) {
console.log(`Number Scale error ::::: ${error}`)
}
});
slackInteractions.action('order:select_hobby', (payload, respond) => {
try {
const selectedType = findSelectedbuttons(payload.original_message, 'order:select_hobby', payload.actions[0].value);
const updatedMessage = acknowledgeActionFromMessage(payload.original_message, 'order:select_hobby',
`Your hobby is *${selectedType.text.toLowerCase()}*.`);
// set the user user response
bot.setUserResponse("favorite_hobby", `${selectedType.text.toLowerCase()}`);
bot.selectTypeForDigitScale(payload, selectedType.value)
.then((response) => {
// Keep the context from the updated message but use the new text and attachment
updatedMessage.text = response.text;
if (response.attachments && response.attachments.length > 0) {
updatedMessage.attachments.push(response.attachments[0]);
}
return updatedMessage;
})
.then(respond)
.catch(console.error);
return updatedMessage;
} catch (error) {
console.log(`Hobby error ::::: ${error}`)
}
});
app.use(cors());
app.use('/api', api);
app.use((err,req,res,next)=>{
console.log(err);
return next(err);
});
app.get('/', (req, res) =>{
res.send({status:'running'});
});
app.get('/auth/redirect', (req, res) =>{
var options = {
uri: 'https://slack.com/api/oauth.access?code='
+req.query.code+
'&client_id='+process.env.CLIENT_ID+
'&client_secret='+process.env.CLIENT_SECRET+
'&redirect_uri='+process.env.REDIRECT_URI,
method: 'GET'
}
axios.get(`https://slack.com/api/oauth.access?code='${req.query.code}&client_id=${process.env.CLIENT_ID}&client_secret=${process.env.CLIENT_SECRET}&redirect_uri=${process.env.REDIRECT_URI}`).then(response=>{
var JSONresponse = JSON.parse(response)
if (!JSONresponse.ok){
res.send("Error encountered: \n"+JSON.stringify(JSONresponse)).status(200).end()
}else{
res.send("Success!")
}
})
})
app.use('/slack/events', slackEvents.requestListener());
app.use('/slack/actions', slackInteractions.requestListener());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
module.exports = app;