forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
218 lines (186 loc) · 7.57 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
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
// 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 msal = require('@azure/msal-node');
const axios = require('axios')
const jwt = require('jsonwebtoken');
// 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();
let multer = require('multer');
let upload = multer({ storage: multer.memoryStorage() });
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);
// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { CloudAdapter,
ConfigurationServiceClientCredentialFactory,
createBotFrameworkAuthenticationFromConfiguration, BotFrameworkAdapter, ConversationState, MemoryStorage, UserState } = require('botbuilder');
const { TeamsBot } = require('./bots/teamsBot');
const { MainDialog } = require('./dialogs/mainDialog');
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MicrosoftAppId,
MicrosoftAppPassword: process.env.MicrosoftAppPassword,
MicrosoftAppType: process.env.AppType,
MicrosoftAppTenantId: process.env.TenantId
});
const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(null, credentialsFactory);
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new CloudAdapter(botFrameworkAuthentication);
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${error}`);
// Send a trace activity, which will be displayed in Bot Framework Emulator
await context.sendTraceActivity(
'OnTurnError Trace',
`${error}`,
'https://www.botframework.com/schemas/error',
'TurnError'
);
// Uncomment below commented line for local debugging.
// await context.sendActivity(`Sorry, it looks like something went wrong. Exception Caught: ${error}`);
// Note: Since this Messaging Extension does not have the messageTeamMembers permission
// in the manifest, the bot will not be allowed to message users.
};
// Define the state store for your bot.
// See https://aka.ms/about-bot-state to learn more about using MemoryStorage.
// A bot requires a state storage system to persist the dialog and user state between messages.
const memoryStorage = new MemoryStorage();
// Create conversation and user state with in-memory storage provider.
const conversationState = new ConversationState(memoryStorage);
const userState = new UserState(memoryStorage);
// Create the main dialog.
const dialog = new MainDialog();
// Create the bot that will handle incoming messages.
const bot = new TeamsBot(conversationState, userState, dialog);
// Create HTTP server.
server.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});
// Endpoint to fetch Auth tab page.
server.get('/AuthTab', (req, res, next) => {
var clientId = process.env.FaceBookAppId;
res.render('./views/AuthTab', { clientId: clientId })
});
// Pop-up dialog to ask for additional permissions, redirects to AAD page
server.get('/auth-start', function (req, res) {
var clientId = process.env.MicrosoftAppId;
res.render('./views/auth-start', { clientId: JSON.stringify(clientId) });
});
// End of the pop-up dialog auth flow, returns the results back to parent window
server.get('/auth-end', function (req, res) {
var clientId = process.env.MicrosoftAppId;
res.render('./views/auth-end', { clientId: JSON.stringify(clientId) });
});
// End of the pop-up dialog auth flow, returns the results back to parent window
server.get('/silent-tab', function (req, res) {
var clientId = process.env.MicrosoftAppId;
res.render('./views/silent-tab', { clientId: JSON.stringify(clientId) });
});
// End of the pop-up dialog auth flow, returns the results back to parent window
server.get('/silent-start', function (req, res) {
var clientId = process.env.MicrosoftAppId;
res.render('./views/silent-start', { clientId: JSON.stringify(clientId) });
});
// End of the pop-up dialog auth flow, returns the results back to parent window
server.get('/silent-end', function (req, res) {
var clientId = process.env.MicrosoftAppId;
res.render('./views/silent-end', { clientId: JSON.stringify(clientId) });
});
// Listen for incoming requests.
server.post('/api/messages', async (req, res) => {
// Route received a request to adapter for processing
await adapter.process(req, res, (context) => bot.run(context));
});
// On-behalf-of token exchange
server.post('/getProfileOnBehalfOf', function (req, res) {
var tid = process.env.TenantId;
var token = req.body.idToken;
var scopes = ["https://graph.microsoft.com/User.Read", "openid"];
// Creating MSAL client
const msalClient = new msal.ConfidentialClientApplication({
auth: {
clientId: process.env.MicrosoftAppId,
clientSecret: process.env.MicrosoftAppPassword
}
});
var oboPromise = new Promise((resolve, reject) => {
msalClient.acquireTokenOnBehalfOf({
authority: `https://login.microsoftonline.com/${tid}`,
oboAssertion: token,
scopes: scopes,
skipCache: true
}).then(async result => {
const client = new SimpleGraphClient(result.accessToken);
const myDetails = await client.getMeAsync();
try {
var userImage = await client.getUserPhoto()
await userImage.arrayBuffer().then(result => {
console.log(userImage.type);
imageString = Buffer.from(result).toString('base64');
img2 = "data:image/png;base64," + imageString;
var userData = {
details: myDetails,
image: img2
}
resolve(myDetails);
});
}
catch (error) {
console.log(error);
}
}).catch(error => {
reject({ "error": error.errorCode });
});
});
oboPromise.then(function (result) {
res.json(result);
}, function (err) {
console.log(err); // Error: "It broke"
res.json(err);
});
});
// Listen for incoming requests.
server.post('/decodedToken', async (req, res) => {
var idToken = req.body.idToken;
const decodedToken = jwt.decode(idToken, { complete: true });
var responseMessage = Promise.resolve(decodedToken.payload);
responseMessage.then(function (result) {
res.json(result);
console.log(result);
}, function (err) {
console.log(err); // Error: "It broke"
res.json(err);
});
});
// Listen for incoming requests.
server.post('/GetUserDetails', async (req, res) => {
var accessToken = req.body.accessToken;
const client = new SimpleGraphClient(accessToken);
const myDetails = await client.getMeAsync();
var responseMessage = Promise.resolve(myDetails);
responseMessage.then(function (result) {
res.json(result);
console.log(result);
}, function (err) {
console.log(err); // Error: "It broke"
res.json(err);
});
});