-
Notifications
You must be signed in to change notification settings - Fork 723
/
teamsBot.js
128 lines (108 loc) · 4.88 KB
/
teamsBot.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { TeamsActivityHandler, TeamsInfo, TurnContext, CardFactory } = require("botbuilder");
var ACData = require("adaptivecards-templating");
const notificationCardJson = require('../resources/SendTargetNotificationCard.json');
class TeamsBot extends TeamsActivityHandler {
constructor() {
super();
this.baseUrl = process.env.BaseUrl;
this.AppId = process.env.MicrosoftAppId;
// This method is invoked whenever there is any message activity in bot's chat.
this.onMessage(async (context, next) => {
var members = new Array();
let meetingId = context._activity.channelData.meeting.id;
if (context.activity.value == null) {
TurnContext.removeRecipientMention(context._activity);
if (context._activity.text.trim() == "SendNotification") {
var meetingMembers = await TeamsInfo.getPagedMembers(context);
let tenantId = context._activity.channelData.tenant.id;
for (var member in meetingMembers) {
let participantDetail = await TeamsInfo.getMeetingParticipant(context, meetingId, member.aadObjectId, tenantId);
// Select only those members that are present when the meeting starts.
if (participantDetail.meeting.inMeeting) {
members.push({ id: participantDetail.user.id, name: participantDetail.user.name })
}
}
// Send an adaptive card to the user to select members for sending targeted notifications.
await context.sendActivity({ attachments: [this.createMembersAdaptiveCard(members)] });
}
else {
await context.sendActivity("Please type `SendNotification` to send In-meeting notifications.");
}
}
else if (context.activity.value.Type == "StageViewNotification") {
var adaptiveCardChoiceSet = context.activity.value.Choice;
var selectedMembers = adaptiveCardChoiceSet.split(",");
this.stageView(context, meetingId, selectedMembers);
}
else if (context.activity.value.Type == "AppIconBadging") {
var adaptiveCardChoiceSet = context.activity.value.Choice;
var selectedMembers = adaptiveCardChoiceSet.split(",");
this.visualIndicator(context, meetingId, selectedMembers);
}
await next();
});
};
// Custom method for sending targeted meeting notifications in stage.
async stageView(context, meetingId, selectedMembers) {
// Notification payload for meeting target notification API.
let notificationInformation = {
type: "targetedMeetingNotification",
value: {
recipients: selectedMembers,
surfaces: [
{
surface: "meetingStage",
contentType: "task",
content: {
value: {
height: "300",
width: "400",
title: "Targeted meeting Notification",
url: `${this.baseUrl + "/hello.html"}`
}
}
}
]
}
}
try {
await TeamsInfo.sendMeetingNotification(context, notificationInformation, meetingId);
} catch (exception) {
console.log(exception);
}
}
// Custom method for sending App Icon Badging on tab.
async visualIndicator(context, meetingId, selectedMembers) {
// Notification payload for meeting target notification API.
let notificationInformation = {
type: "targetedMeetingNotification",
value: {
recipients: selectedMembers,
surfaces: [
{
surface: "meetingTabIcon",
}
]
}
}
try {
await TeamsInfo.sendMeetingNotification(context, notificationInformation, meetingId);
} catch (exception) {
console.log(exception);
}
}
// Create an adaptive card to send a list of in-meeting participants
createMembersAdaptiveCard(members) {
var templatePayload = notificationCardJson;
var template = new ACData.Template(templatePayload);
var cardPayload = template.expand({
$root: {
members: members
}
});
return CardFactory.adaptiveCard(cardPayload);
}
}
module.exports.TeamsBot = TeamsBot;