-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.js
69 lines (56 loc) · 1.9 KB
/
simple.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
const builder = require('botbuilder')
const restify = require('restify')
const dotenv = require('dotenv')
const fs = require('fs')
const path = require('path')
const { GlipConnector } = require('../lib')
dotenv.config()
let botsData = {}
const botsDataFile = path.join(__dirname, '.cache')
if (fs.existsSync(botsDataFile)) {
botsData = JSON.parse(fs.readFileSync(botsDataFile, 'utf-8'))
}
const server = restify.createServer()
server.use(restify.plugins.queryParser())
server.use(restify.plugins.bodyParser())
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url)
})
const connector = new GlipConnector({
botLookup: (botId) => {
const botEntry = botsData[botId]
return botEntry
},
verificationToken: process.env.GLIP_BOT_VERIFICATION_TOKEN,
clientId: process.env.GLIP_CLIENT_ID,
clientSecret: process.env.GLIP_CLIENT_SECRET,
server: process.env.GLIP_API_SERVER,
redirectUrl: `${process.env.GLIP_BOT_SERVER}/oauth`,
webhookUrl: `${process.env.GLIP_BOT_SERVER}/webhook`,
replyOnlyMentioned: false
})
// For public glip bot
server.get('/oauth', connector.listenOAuth())
//For private glip bot
server.post('/oauth', connector.listenOAuth())
server.post('/webhook', connector.listen())
const bot = new builder.UniversalBot(connector)
bot.on('installationUpdate', (event) => {
console.log(`New bot installed: ${event.sourceEvent.TokenData.owner_name}`)
botsData[event.sourceEvent.TokenData.owner_id] = {
identity: event.address.bot,
token: event.sourceEvent.TokenData
}
fs.writeFileSync(botsDataFile, JSON.stringify(botsData)) // save token
})
bot.dialog('/', function (session) {
console.log('Get message from glip:', session.message)
session.send({
text: `You said: ${session.message.text}`,
attachments: [{
type: 'Card',
fallback: 'Text',
text: session.message.text,
}]
})
});