-
Notifications
You must be signed in to change notification settings - Fork 723
/
ActivityBot.cs
257 lines (242 loc) · 10.7 KB
/
ActivityBot.cs
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace MeetingEvents.Bots
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AdaptiveCards;
using MeetingEvents.Models;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Teams;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Schema.Teams;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
public class ActivityBot : TeamsActivityHandler
{
private BotState _conversationState;
public ActivityBot(ConversationState conversationState)
{
_conversationState = conversationState;
}
/// <summary>
/// Activity Handler for Meeting Participant join event
/// </summary>
/// <param name="meeting"></param>
/// <param name="turnContext"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected override async Task OnTeamsMeetingParticipantsJoinAsync(MeetingParticipantsEventDetails meeting, ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
{
await turnContext.SendActivityAsync(MessageFactory.Attachment(createAdaptiveCardInvokeResponseAsync(meeting.Members[0].User.Name, " has joined the meeting.")));
return;
}
/// <summary>
/// Activity Handler for Meeting Participant leave event
/// </summary>
/// <param name="meeting"></param>
/// <param name="turnContext"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected override async Task OnTeamsMeetingParticipantsLeaveAsync(MeetingParticipantsEventDetails meeting, ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
{
await turnContext.SendActivityAsync(MessageFactory.Attachment(createAdaptiveCardInvokeResponseAsync(meeting.Members[0].User.Name, " left the meeting.")));
return;
}
/// <summary>
/// Sample Adaptive card for Meeting participant events.
/// </summary>
private Attachment createAdaptiveCardInvokeResponseAsync(string userName, string action)
{
AdaptiveCard card = new AdaptiveCard(new AdaptiveSchemaVersion("1.4"))
{
Body = new List<AdaptiveElement>
{
new AdaptiveRichTextBlock
{
Inlines = new List<AdaptiveInline>
{
new AdaptiveTextRun
{
Text = userName,
Weight = AdaptiveTextWeight.Bolder,
Size = AdaptiveTextSize.Default,
},
new AdaptiveTextRun
{
Text = action,
Weight = AdaptiveTextWeight.Default,
Size = AdaptiveTextSize.Default,
}
},
Spacing = AdaptiveSpacing.Medium,
}
}
};
return new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card,
};
}
/// <summary>
/// Activity Handler for Meeting start event
/// </summary>
/// <param name="meeting"></param>
/// <param name="turnContext"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected override async Task OnTeamsMeetingStartAsync(MeetingStartEventDetails meeting, ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
{
// Save any state changes that might have occurred during the turn.
var conversationStateAccessors = _conversationState.CreateProperty<MeetingData>(nameof(MeetingData));
var conversationData = await conversationStateAccessors.GetAsync(turnContext, () => new MeetingData());
conversationData.StartTime = meeting.StartTime;
await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await turnContext.SendActivityAsync(MessageFactory.Attachment(GetAdaptiveCardForMeetingStart(meeting)));
}
/// <summary>
/// Activity Handler for Meeting end event.
/// </summary>
/// <param name="meeting"></param>
/// <param name="turnContext"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected override async Task OnTeamsMeetingEndAsync(MeetingEndEventDetails meeting, ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
{
var conversationStateAccessors = _conversationState.CreateProperty<MeetingData>(nameof(MeetingData));
var conversationData = await conversationStateAccessors.GetAsync(turnContext, () => new MeetingData());
await turnContext.SendActivityAsync(MessageFactory.Attachment(GetAdaptiveCardForMeetingEnd(meeting, conversationData)));
}
/// <summary>
/// Sample Adaptive card for Meeting Start event.
/// </summary>
private Attachment GetAdaptiveCardForMeetingStart(MeetingStartEventDetails meeting)
{
AdaptiveCard card = new AdaptiveCard(new AdaptiveSchemaVersion("1.2"))
{
Body = new List<AdaptiveElement>
{
new AdaptiveTextBlock
{
Text = meeting.Title + "- started",
Weight = AdaptiveTextWeight.Bolder,
Spacing = AdaptiveSpacing.Medium,
},
new AdaptiveColumnSet
{
Columns = new List<AdaptiveColumn>
{
new AdaptiveColumn
{
Width = AdaptiveColumnWidth.Auto,
Items = new List<AdaptiveElement>
{
new AdaptiveTextBlock
{
Text = "Start Time : ",
Wrap = true,
},
},
},
new AdaptiveColumn
{
Width = AdaptiveColumnWidth.Auto,
Items = new List<AdaptiveElement>
{
new AdaptiveTextBlock
{
Text = Convert.ToString(meeting.StartTime.ToLocalTime()),
Wrap = true,
},
},
},
},
},
},
Actions = new List<AdaptiveAction>
{
new AdaptiveOpenUrlAction
{
Title = "Join meeting",
Url = meeting.JoinUrl,
},
},
};
return new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card,
};
}
/// <summary>
/// Sample Adaptive card for Meeting End event.
/// </summary>
private Attachment GetAdaptiveCardForMeetingEnd(MeetingEndEventDetails meeting, MeetingData conversationData)
{
TimeSpan meetingDuration = meeting.EndTime - conversationData.StartTime;
var meetingDurationText = meetingDuration.Minutes < 1 ?
Convert.ToInt32(meetingDuration.Seconds) + "s"
: Convert.ToInt32(meetingDuration.Minutes) + "min " + Convert.ToInt32(meetingDuration.Seconds) + "s";
AdaptiveCard card = new AdaptiveCard(new AdaptiveSchemaVersion("1.2"))
{
Body = new List<AdaptiveElement>
{
new AdaptiveTextBlock
{
Text = meeting.Title + "- ended",
Weight = AdaptiveTextWeight.Bolder,
Spacing = AdaptiveSpacing.Medium,
},
new AdaptiveColumnSet
{
Columns = new List<AdaptiveColumn>
{
new AdaptiveColumn
{
Width = AdaptiveColumnWidth.Auto,
Items = new List<AdaptiveElement>
{
new AdaptiveTextBlock
{
Text = "End Time : ",
Wrap = true,
},
new AdaptiveTextBlock
{
Text = "Total duration : ",
Wrap = true,
},
},
},
new AdaptiveColumn
{
Width = AdaptiveColumnWidth.Auto,
Items = new List<AdaptiveElement>
{
new AdaptiveTextBlock
{
Text = Convert.ToString(meeting.EndTime.ToLocalTime()),
Wrap = true,
},
new AdaptiveTextBlock
{
Text = meetingDurationText,
Wrap = true,
},
},
},
},
},
}
};
return new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card,
};
}
}
}