-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (61 loc) · 1.93 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
const express = require('express');
const builder = require('botbuilder');
const logger = require('morgan');
const bodyParser = require('body-parser');
const authMiddleware = require('./api/middlewares/auth');
const apiRoutes = require('./api/');
const handler = require('./handler');
const app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Create chat connector for communicating with the Bot Framework Service
const connector = new builder.ChatConnector({
appId: process.env.MS_APP_ID,
appPassword: process.env.MS_APP_PWD
});
// Receive messages from the user and respond by echoing each message back (prefixed with 'You said:')
const bot = new builder.UniversalBot(connector, handler);
app.post('/messages', connector.listen());
// API Routes
app.use('/auth', (req, res) => {
// TODO: Read the header and validate request with
// `api-key` and `api-secret` and return generated `access_token`
// const headers = req.headers;
// const apiKey = headers['api-key'];
// const apiSecret = headers['api-secret'];
res.status(200)
.send({
status: {
code: 200,
message: 'OK'
},
access_token: '(access_token)'
});
});
app.all('/api/*', [authMiddleware], (req, res, next) => {
// Set Headers for CORS
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key');
if (req.method === 'OPTIONS') {
res.status(200).end();
} else {
next();
}
});
app.use('/api', apiRoutes);
// Handle 404 requests
app.use((req, res) => {
res.status(404)
.send({
status: {
code: 404,
message: 'Not Found'
}
})
});
app.set('PORT', process.env.PORT || 3978);
app.listen(app.get('PORT'), () => console.log('Server listening on %s', app.get('PORT')))