Skip to content

Commit

Permalink
Merge pull request #982 from OfficeDev/v-pritka/rsc-token-changes
Browse files Browse the repository at this point in the history
Changed api calls to use access token at server side
  • Loading branch information
Prithvi-MSFT committed Nov 2, 2023
2 parents 5d9da38 + acecfe3 commit 1bceaf8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 69 deletions.
76 changes: 72 additions & 4 deletions samples/graph-rsc/nodeJs/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ app.set('views', __dirname);
const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });

var token;
var recipientId;

app.use('/', indexRouter);

app.get('/configure', function (req, res) {
Expand All @@ -31,12 +34,77 @@ app.get('/rscdemo', function (req, res) {
});

app.get('/sendNotification', function (req, res) {
var tenantId = process.env.TenantId
auth.getAccessToken(tenantId).then(async function (token) {
res.render('./views/sendNotification', { token: JSON.stringify(token) });
});
res.render('./views/sendNotification');
});

app.post('/sendFeedNotification', function (req, res) {
recipientId = req.data.reciepientUserId;
token = auth.getAccessToken(tenantId).then(async function (token) {
await getInstalledAppList(token, recipientId);
})
});

// Get installed app id.
function getAppId(appList) {
var list = appList;
var i;
for (i = 0; i < list.length; i++) {
if (list[i].teamsAppDefinition['displayName'] == "RSC-GraphAPI NodeJs") {
return list[i].id;
}
}
}

// Fetch the list of installed apps for user
async function getInstalledAppList(accessToken, reciepientUserId) {

const config = {
headers: {
Authorization: "Bearer " + accessToken
}
};

axios.get("https://graph.microsoft.com/v1.0/users/" + reciepientUserId + "/teamwork/installedApps/?$expand=teamsAppDefinition", config)
.then(async (res) => {
var appId = getAppId(res.value);
await sendActivityFeedNotification(token, reciepientUserId, appId);
})
.catch(err => console.log(err))
}

// Send activity feed notification to user
async function sendActivityFeedNotification(accessToken, reciepientUserId, appId) {

var postData = {
topic: {
source: "entityUrl",
value: `https://graph.microsoft.com/beta/users/${reciepientUserId}/teamwork/installedApps/${appId}`
},
activityType: "taskCreated",
previewText: {
content: "New Task Created"
},
templateParameters: [
{
name: "taskName",
value: "test"
}
]
};

const config = {
headers: {
Authorization: "Bearer " + accessToken
}
};

axios.get(`https://graph.microsoft.com/beta/users/${reciepientUserId}/teamwork/sendActivityNotification`, postData, config)
.then((res) => {
console.log("Success");
})
.catch(err => console.log(err))
}

app.listen(3978 || 3978, function () {
console.log('app listening on port 3978!');
});
80 changes: 15 additions & 65 deletions samples/graph-rsc/nodeJs/views/sendNotification.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<script>
var appId;
var token = <%-token%>;
// Initialize teams js sdk
$(document).ready(function () {
Expand All @@ -33,75 +32,26 @@
microsoftTeams.people.selectPeople({ setSelected: setSelected, openOrgWideSearchInChatOrChannel: openOrgWideSearchInChatOrChannel, singleSelect: singleSelect }).then((people) => {
if (people) {
let reciepientUserId = people[0].objectId;
getInstalledAppList(token, reciepientUserId)
sendNotification(reciepientUserId)
}
});
}
// Get installed app id.
function getAppId(appList) {
var list = appList;
var i;
for (i = 0; i < list.length; i++) {
if (list[i].teamsAppDefinition['displayName'] == "RSC-GraphAPI NodeJs") {
return list[i].id;
}
}
}
// Fetch the list of installed apps for user
function getInstalledAppList(accessToken, reciepientUserId) {
function sendNotification(reciepientUserId) {
$.ajax({
url: "https://graph.microsoft.com/v1.0/users/" + reciepientUserId + "/teamwork/installedApps/?$expand=teamsAppDefinition",
type: "GET",
beforeSend: function (request) {
request.setRequestHeader("Authorization", "Bearer " + accessToken);
},
success: function (result) {
appId = getAppId(result.value);
sendActivityFeedNotification(token, reciepientUserId, appId)
},
error: function (xhr, textStatus, errorThrown) {
console.log("textStatus: " + textStatus + ", errorThrown:" + errorThrown);
},
});
}
// Send activity feed notification to user
function sendActivityFeedNotification(accessToken, reciepientUserId, appId) {
var postData = {
topic: {
source: "entityUrl",
value: `https://graph.microsoft.com/beta/users/${reciepientUserId}/teamwork/installedApps/${appId}`
},
activityType: "taskCreated",
previewText: {
content: "New Task Created"
},
templateParameters: [
{
name: "taskName",
value: "test"
}
]
};
$.ajax({
url: `https://graph.microsoft.com/beta/users/${reciepientUserId}/teamwork/sendActivityNotification`,
type: "POST",
contentType: 'application/json',
data: JSON.stringify(postData),
beforeSend: function (request) {
request.setRequestHeader("Authorization", "Bearer " + accessToken);
},
success: function (profile) {
console.log(profile);
},
error: function (xhr, textStatus, errorThrown) {
console.log("textStatus: " + textStatus + ", errorThrown:" + errorThrown);
},
});
type: 'POST',
url: '/sendFeedNotification',
dataType: 'json',
data: {
'reciepientUserId': reciepientUserId,
},
success: function (responseJson) {
console.log("Message send successfully");
},
error: function (xhr, textStatus, errorThrown) {
console.log("textStatus: " + textStatus + ", errorThrown:" + errorThrown);
}
});
}
</script>

Expand Down

0 comments on commit 1bceaf8

Please sign in to comment.