-
Notifications
You must be signed in to change notification settings - Fork 838
/
Copy pathserver.js
112 lines (93 loc) · 3.08 KB
/
server.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
const express = require('express');
const bodyparser = require('body-parser');
const env = require('dotenv')
const path = require('path');
const auth = require('./auth');
const indexRouter = require('./routes/index');
require('isomorphic-fetch');
const axios = require('axios');
const app = express();
app.use(bodyparser.urlencoded({ extended: false }))
app.use(bodyparser.json())
app.use(express.static(__dirname + '/Styles'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.set('views', __dirname);
const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });
app.use('/', indexRouter);
app.get('/configure', function (req, res) {
res.render('./views/configure');
});
app.get('/rscdemo', function (req, res) {
var tenantId = req.url.split('=')[1];
auth.getAccessToken(tenantId).then(async function (token) {
res.render('./views/rscdemo', { token: JSON.stringify(token) });
});
});
app.get('/sendNotification', function (req, res) {
res.render('./views/sendNotification');
});
app.post('/sendFeedNotification', function (req, res) {
var recipientId = req.body.recipientUserId;
var tenantId = req.body.tenantId;
sendNotificationFlow(tenantId, recipientId).then(function(){
console.log('Notification send success');
res.status(200).send('ok')
}).catch(function(err){
res.status(500).send('error: ' + err.message)
});
});
async function sendNotificationFlow(tenantId, recipientId) {
var token = await auth.getAccessToken(tenantId);
var appId = await getAppId(token, recipientId);
await sendActivityFeedNotification(token, recipientId, appId);
}
// Get installed app id.
function findAppIdInList(appList) {
for (var i = 0; i < appList.length; i++) {
if (appList[i].teamsAppDefinition['displayName'] == "RSC-GraphAPI NodeJs") {
return appList[i].id;
}
}
}
// Fetch the list of installed apps for user
async function getAppId(accessToken, reciepientUserId) {
const config = {
headers: {
Authorization: "Bearer " + accessToken
}
};
var res = await axios.get("https://graph.microsoft.com/v1.0/users/" + reciepientUserId + "/teamwork/installedApps/?$expand=teamsAppDefinition", config)
var appId = findAppIdInList(res.data.value);
return appId;
}
// Send activity feed notification to user
async function sendActivityFeedNotification(accessToken, recipientUserId, appId) {
var postData = {
topic: {
source: "entityUrl",
value: `https://graph.microsoft.com/beta/users/${recipientUserId}/teamwork/installedApps/${appId}`
},
activityType: "taskCreated",
previewText: {
content: "New Task Created"
},
templateParameters: [
{
name: "taskName",
value: "test"
}
]
};
const config = {
headers: {
Authorization: "Bearer " + accessToken
}
};
await axios.post(`https://graph.microsoft.com/beta/users/${recipientUserId}/teamwork/sendActivityNotification`, postData, config)
console.log('Notification sent');
}
app.listen(3978 || 3978, function () {
console.log('app listening on port 3978!');
});