forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (87 loc) · 2.87 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// index.js is used to setup and configure your bot
// Import required packages
const path = require('path');
const express = require('express');
const cors = require('cors');
const { SimpleGraphClient } = require('./simpleGraphClient');
const { polyfills } = require('isomorphic-fetch');
// Read botFilePath and botFileSecret from .env file.
const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });
const PORT = process.env.PORT || 3978;
const server = express();
server.use(cors());
server.use(express.json());
server.use(express.urlencoded({
extended: true
}));
server.engine('html', require('ejs').renderFile);
server.set('view engine', 'ejs');
server.set('views', __dirname);
// Create HTTP server.
server.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});
// Endpoint to fetch Auth tab page.
server.get('/tab', (req, res, next) => {
res.render('./views/tab')
});
// Pop-up dialog to ask for additional permissions, redirects to AAD page
server.get('/auth-start', function (req, res) {
var scope = req.url.split('=')[1];
var data = {
clientId:process.env.MicrosoftAppId,
scope:scope
};
res.render('./views/auth-start', { data: JSON.stringify(data) });
});
// End of the pop-up dialog auth flow, returns the results back to parent window
server.get('/auth-end', function (req, res) {
res.render('./views/auth-end');
});
// Get user details.
server.post('/GetUserPhoto', async (req, res) => {
var accessToken = req.body.accessToken;
const client = new SimpleGraphClient(accessToken);
var userImage = await client.getUserPhoto();
var userData;
await userImage.arrayBuffer().then(result => {
imageString = Buffer.from(result).toString('base64');
img2 = "data:image/png;base64," + imageString;
userData = {
image: img2,
}
});
var responseMessage = new Promise((resolve, reject) => {
resolve(userData)
});
responseMessage.then(function (result) {
res.json(result);
}, function (err) { // Error: "It broke"
res.json(err);
});
});
// Get user mails.
server.post('/GetUserMails', async (req, res) => {
var accessToken = req.body.accessToken;
const client = new SimpleGraphClient(accessToken);
const usermails = await client.getMailAsync();
var responseMessage = Promise.resolve(usermails.value);
responseMessage.then(function (result) {
res.json(result);
}, function (err) {
console.log(err); // Error: "It broke"
res.json(err);
});
});
server.post('/decodeToken',(req, res)=>{
var token = req.body.accessToken;
if(token !== null || token !== undefined){
const base64String = token.split('.')[1];
const decodedValue = JSON.parse(Buffer.from(base64String,
'base64').toString('ascii'));
res.json(decodedValue);
}
})