-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
86 lines (68 loc) · 2.34 KB
/
bot.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
// __ __ ___ ___
// |__) / \ | |__/ | |
// |__) \__/ | | \ | |
// This is the main file for the mona bot.
const path = require('path')
const { Botkit } = require('botkit')
const { BotkitCMSHelper } = require('botkit-plugin-cms')
const { MongoDbStorage } = require('botbuilder-storage-mongodb')
const { removeCodeFromMessage } = require('./lib/middleware')
const adapter = require('./adapter')
const { tokenCache, userCache } = require('./cache')
require('dotenv').config()
let storage = null
if (process.env.MONGO_URI) {
storage = new MongoDbStorage({
url: process.env.MONGO_URI,
database: process.env.MONGO_DB_NAME,
})
}
const controller = new Botkit({
debug: true,
webhook_uri: '/api/messages',
adapter,
storage,
})
if (process.env.cms_uri) {
controller.usePlugin(new BotkitCMSHelper({
cms_uri: process.env.cms_uri,
token: process.env.cms_token,
}))
}
// Once the bot has booted up its internal services, you can use them to do stuff.
controller.ready(() => {
// load traditional developer-created local custom feature modules
controller.loadModules(path.join(__dirname, 'features'))
/* catch-all that uses the CMS to trigger dialogs */
if (controller.plugins.cms) {
controller.on('message,direct_message', async (bot, message) => {
let results = false
results = await controller.plugins.cms.testTrigger(bot, message)
if (results !== false) {
// do not continue middleware!
return false
}
return null
})
}
})
controller.webserver.get('/install', (req, res) => {
// getInstallLink points to slack's oauth endpoint and includes clientId and scopes
res.redirect(controller.adapter.getInstallLink())
})
controller.webserver.get('/install/auth', async (req, res) => {
try {
const results = await controller.adapter.validateOauthCode(req.query.code)
console.log('FULL OAUTH DETAILS', results) // eslint-disable-line no-console
// Store token by team in bot state.
tokenCache[results.team_id] = results.bot.bot_access_token
// Capture team to bot id
userCache[results.team_id] = results.bot.bot_user_id
res.json('Success! Bot installed.')
} catch (err) {
console.error('OAUTH ERROR:', err) // eslint-disable-line no-console
res.status(401)
res.send(err.message)
}
})
controller.middleware.ingest.use(removeCodeFromMessage)