-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C#] feat: add meeting handlers (#771)
## Linked issues closes: #696 ## Details Add handlers for meeting events #### Change details Add handlers under `Application.Meetings` for following events: - application/vnd.microsoft.meetingStart - application/vnd.microsoft.meetingEnd - application/vnd.microsoft.meetingParticipantJoin - application/vnd.microsoft.meetingParticipantLeave Refactor `InvokeActivityUtilities` to `ActivityUtilities` since the event activity has `Activity.Value` as well. Unit tests. ## Attestation Checklist - [X] My code follows the style guidelines of this project - I have checked for/fixed spelling, linting, and other errors - I have commented my code for clarity - I have made corresponding changes to the documentation (we use [TypeDoc](https://typedoc.org/) to document our code) - My changes generate no new warnings - I have added tests that validates my changes, and provides sufficient test coverage. I have tested with: - Local testing - E2E testing in Teams - New and existing unit tests pass locally with my changes ### Additional information > Feel free to add other relevant information below
- Loading branch information
Showing
8 changed files
with
358 additions
and
30 deletions.
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/Application/MeetingsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
using Microsoft.Bot.Builder; | ||
using Microsoft.Bot.Connector; | ||
using Microsoft.Bot.Schema; | ||
using Microsoft.TeamsAI.Tests.TestUtils; | ||
|
||
namespace Microsoft.TeamsAI.Tests.Application | ||
{ | ||
public class MeetingsTests | ||
{ | ||
[Fact] | ||
public async void Test_OnStart() | ||
{ | ||
// Arrange | ||
var adapter = new NotImplementedAdapter(); | ||
var turnContexts = CreateMeetingTurnContext("application/vnd.microsoft.meetingStart", adapter); | ||
var app = new Application<TestTurnState, TestTurnStateManager>(new() | ||
{ | ||
RemoveRecipientMention = false, | ||
StartTypingTimer = false | ||
}); | ||
var ids = new List<string>(); | ||
app.Meetings.OnStart((context, _, _, _) => | ||
{ | ||
ids.Add(context.Activity.Id); | ||
return Task.CompletedTask; | ||
}); | ||
|
||
// Act | ||
foreach (var turnContext in turnContexts) | ||
{ | ||
await app.OnTurnAsync(turnContext); | ||
} | ||
|
||
// Assert | ||
Assert.Single(ids); | ||
Assert.Equal("test.id", ids[0]); | ||
} | ||
|
||
[Fact] | ||
public async void Test_OnEnd() | ||
{ | ||
// Arrange | ||
var adapter = new NotImplementedAdapter(); | ||
var turnContexts = CreateMeetingTurnContext("application/vnd.microsoft.meetingEnd", adapter); | ||
var app = new Application<TestTurnState, TestTurnStateManager>(new() | ||
{ | ||
RemoveRecipientMention = false, | ||
StartTypingTimer = false | ||
}); | ||
var ids = new List<string>(); | ||
app.Meetings.OnEnd((context, _, _, _) => | ||
{ | ||
ids.Add(context.Activity.Id); | ||
return Task.CompletedTask; | ||
}); | ||
|
||
// Act | ||
foreach (var turnContext in turnContexts) | ||
{ | ||
await app.OnTurnAsync(turnContext); | ||
} | ||
|
||
// Assert | ||
Assert.Single(ids); | ||
Assert.Equal("test.id", ids[0]); | ||
} | ||
|
||
[Fact] | ||
public async void Test_OnParticipantsJoin() | ||
{ | ||
// Arrange | ||
var adapter = new NotImplementedAdapter(); | ||
var turnContexts = CreateMeetingTurnContext("application/vnd.microsoft.meetingParticipantJoin", adapter); | ||
var app = new Application<TestTurnState, TestTurnStateManager>(new() | ||
{ | ||
RemoveRecipientMention = false, | ||
StartTypingTimer = false | ||
}); | ||
var ids = new List<string>(); | ||
app.Meetings.OnParticipantsJoin((context, _, _, _) => | ||
{ | ||
ids.Add(context.Activity.Id); | ||
return Task.CompletedTask; | ||
}); | ||
|
||
// Act | ||
foreach (var turnContext in turnContexts) | ||
{ | ||
await app.OnTurnAsync(turnContext); | ||
} | ||
|
||
// Assert | ||
Assert.Single(ids); | ||
Assert.Equal("test.id", ids[0]); | ||
} | ||
|
||
[Fact] | ||
public async void Test_OnParticipantsLeave() | ||
{ | ||
// Arrange | ||
var adapter = new NotImplementedAdapter(); | ||
var turnContexts = CreateMeetingTurnContext("application/vnd.microsoft.meetingParticipantLeave", adapter); | ||
var app = new Application<TestTurnState, TestTurnStateManager>(new() | ||
{ | ||
RemoveRecipientMention = false, | ||
StartTypingTimer = false | ||
}); | ||
var ids = new List<string>(); | ||
app.Meetings.OnParticipantsLeave((context, _, _, _) => | ||
{ | ||
ids.Add(context.Activity.Id); | ||
return Task.CompletedTask; | ||
}); | ||
|
||
// Act | ||
foreach (var turnContext in turnContexts) | ||
{ | ||
await app.OnTurnAsync(turnContext); | ||
} | ||
|
||
// Assert | ||
Assert.Single(ids); | ||
Assert.Equal("test.id", ids[0]); | ||
} | ||
|
||
private static TurnContext[] CreateMeetingTurnContext(string activityName, BotAdapter adapter) | ||
{ | ||
return new TurnContext[] | ||
{ | ||
new(adapter, new Activity | ||
{ | ||
Type = ActivityTypes.Event, | ||
ChannelId = Channels.Msteams, | ||
Name = activityName, | ||
Id = "test.id" | ||
}), | ||
new(adapter, new Activity | ||
{ | ||
Type = ActivityTypes.Event, | ||
ChannelId = Channels.Msteams, | ||
Name = "fake.name" | ||
}), | ||
new(adapter, new Activity | ||
{ | ||
Type = ActivityTypes.Invoke, | ||
ChannelId = Channels.Msteams, | ||
Name = activityName | ||
}), | ||
new(adapter, new Activity | ||
{ | ||
Type = ActivityTypes.Event, | ||
ChannelId = Channels.Webchat, | ||
Name = activityName | ||
}), | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/Application/Meetings/Meetings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using Microsoft.Bot.Connector; | ||
using Microsoft.Bot.Schema; | ||
using Microsoft.Bot.Schema.Teams; | ||
using Microsoft.TeamsAI.State; | ||
using Microsoft.TeamsAI.Utilities; | ||
|
||
namespace Microsoft.TeamsAI | ||
{ | ||
/// <summary> | ||
/// Meetings class to enable fluent style registration of handlers related to Microsoft Teams Meetings. | ||
/// </summary> | ||
/// <typeparam name="TState">The type of the turn state object used by the application.</typeparam> | ||
/// <typeparam name="TTurnStateManager">The type of the turn state manager object used by the application.</typeparam> | ||
public class Meetings<TState, TTurnStateManager> | ||
where TState : ITurnState<StateBase, StateBase, TempState> | ||
where TTurnStateManager : ITurnStateManager<TState>, new() | ||
{ | ||
private readonly Application<TState, TTurnStateManager> _app; | ||
|
||
/// <summary> | ||
/// Creates a new instance of the Meetings class. | ||
/// </summary> | ||
/// <param name="app"></param> The top level application class to register handlers with. | ||
public Meetings(Application<TState, TTurnStateManager> app) | ||
{ | ||
this._app = app; | ||
} | ||
|
||
/// <summary> | ||
/// Handles Microsoft Teams meeting start events. | ||
/// </summary> | ||
/// <param name="handler">Function to call when a Microsoft Teams meeting start event activity is received from the connector.</param> | ||
/// <returns>The application instance for chaining purposes.</returns> | ||
public Application<TState, TTurnStateManager> OnStart(MeetingStartHandler<TState> handler) | ||
{ | ||
Verify.ParamNotNull(handler); | ||
RouteSelector routeSelector = (context, _) => Task.FromResult | ||
( | ||
string.Equals(context.Activity?.Type, ActivityTypes.Event, StringComparison.OrdinalIgnoreCase) | ||
&& string.Equals(context.Activity?.ChannelId, Channels.Msteams) | ||
&& string.Equals(context.Activity?.Name, "application/vnd.microsoft.meetingStart") | ||
); | ||
RouteHandler<TState> routeHandler = async (turnContext, turnState, cancellationToken) => | ||
{ | ||
MeetingStartEventDetails meeting = ActivityUtilities.GetTypedValue<MeetingStartEventDetails>(turnContext.Activity) ?? new(); | ||
await handler(turnContext, turnState, meeting, cancellationToken); | ||
}; | ||
_app.AddRoute(routeSelector, routeHandler, isInvokeRoute: false); | ||
return _app; | ||
} | ||
|
||
/// <summary> | ||
/// Handles Microsoft Teams meeting end events. | ||
/// </summary> | ||
/// <param name="handler">Function to call when a Microsoft Teams meeting end event activity is received from the connector.</param> | ||
/// <returns>The application instance for chaining purposes.</returns> | ||
public Application<TState, TTurnStateManager> OnEnd(MeetingEndHandler<TState> handler) | ||
{ | ||
Verify.ParamNotNull(handler); | ||
RouteSelector routeSelector = (context, _) => Task.FromResult | ||
( | ||
string.Equals(context.Activity?.Type, ActivityTypes.Event, StringComparison.OrdinalIgnoreCase) | ||
&& string.Equals(context.Activity?.ChannelId, Channels.Msteams) | ||
&& string.Equals(context.Activity?.Name, "application/vnd.microsoft.meetingEnd") | ||
); | ||
RouteHandler<TState> routeHandler = async (turnContext, turnState, cancellationToken) => | ||
{ | ||
MeetingEndEventDetails meeting = ActivityUtilities.GetTypedValue<MeetingEndEventDetails>(turnContext.Activity) ?? new(); | ||
await handler(turnContext, turnState, meeting, cancellationToken); | ||
}; | ||
_app.AddRoute(routeSelector, routeHandler, isInvokeRoute: false); | ||
return _app; | ||
} | ||
|
||
/// <summary> | ||
/// Handles Microsoft Teams meeting participants join events. | ||
/// </summary> | ||
/// <param name="handler">Function to call when a Microsoft Teams meeting participants join event activity is received from the connector.</param> | ||
/// <returns>The application instance for chaining purposes.</returns> | ||
public Application<TState, TTurnStateManager> OnParticipantsJoin(MeetingParticipantsEventHandler<TState> handler) | ||
{ | ||
Verify.ParamNotNull(handler); | ||
RouteSelector routeSelector = (context, _) => Task.FromResult | ||
( | ||
string.Equals(context.Activity?.Type, ActivityTypes.Event, StringComparison.OrdinalIgnoreCase) | ||
&& string.Equals(context.Activity?.ChannelId, Channels.Msteams) | ||
&& string.Equals(context.Activity?.Name, "application/vnd.microsoft.meetingParticipantJoin") | ||
); | ||
RouteHandler<TState> routeHandler = async (turnContext, turnState, cancellationToken) => | ||
{ | ||
MeetingParticipantsEventDetails meeting = ActivityUtilities.GetTypedValue<MeetingParticipantsEventDetails>(turnContext.Activity) ?? new(); | ||
await handler(turnContext, turnState, meeting, cancellationToken); | ||
}; | ||
_app.AddRoute(routeSelector, routeHandler, isInvokeRoute: false); | ||
return _app; | ||
} | ||
|
||
/// <summary> | ||
/// Handles Microsoft Teams meeting participants leave events. | ||
/// </summary> | ||
/// <param name="handler">Function to call when a Microsoft Teams meeting participants leave event activity is received from the connector.</param> | ||
/// <returns>The application instance for chaining purposes.</returns> | ||
public Application<TState, TTurnStateManager> OnParticipantsLeave(MeetingParticipantsEventHandler<TState> handler) | ||
{ | ||
Verify.ParamNotNull(handler); | ||
RouteSelector routeSelector = (context, _) => Task.FromResult | ||
( | ||
string.Equals(context.Activity?.Type, ActivityTypes.Event, StringComparison.OrdinalIgnoreCase) | ||
&& string.Equals(context.Activity?.ChannelId, Channels.Msteams) | ||
&& string.Equals(context.Activity?.Name, "application/vnd.microsoft.meetingParticipantLeave") | ||
); | ||
RouteHandler<TState> routeHandler = async (turnContext, turnState, cancellationToken) => | ||
{ | ||
MeetingParticipantsEventDetails meeting = ActivityUtilities.GetTypedValue<MeetingParticipantsEventDetails>(turnContext.Activity) ?? new(); | ||
await handler(turnContext, turnState, meeting, cancellationToken); | ||
}; | ||
_app.AddRoute(routeSelector, routeHandler, isInvokeRoute: false); | ||
return _app; | ||
} | ||
} | ||
} |
Oops, something went wrong.