-
Notifications
You must be signed in to change notification settings - Fork 840
/
Copy pathTeamsBot.cs
66 lines (60 loc) · 3.12 KB
/
TeamsBot.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Logging;
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// This bot is derived from the <see cref="DialogBot{T}"/> class and handles Teams-specific activities.
/// </summary>
/// <typeparam name="T">The type of Dialog to be run.</typeparam>
public class TeamsBot<T> : DialogBot<T> where T : Dialog
{
/// <summary>
/// Initializes a new instance of the <see cref="TeamsBot{T}"/> class.
/// </summary>
/// <param name="conversationState">The conversation state.</param>
/// <param name="userState">The user state.</param>
/// <param name="dialog">The dialog to be run.</param>
/// <param name="logger">The logger.</param>
public TeamsBot(ConversationState conversationState, UserState userState, T dialog, ILogger<DialogBot<T>> logger)
: base(conversationState, userState, dialog, logger)
{
}
/// <summary>
/// Handles the event when members are added to the conversation.
/// </summary>
/// <param name="membersAdded">The list of members added to the conversation.</param>
/// <param name="turnContext">The context object for this turn.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task that represents the work queued to execute.</returns>
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
foreach (var member in turnContext.Activity.MembersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
await turnContext.SendActivityAsync(MessageFactory.Text("Welcome to AuthenticationBot. Type anything to get logged in. Type 'logout' to sign-out."), cancellationToken);
}
}
}
/// <summary>
/// Handles the Teams Sign-in verification state.
/// </summary>
/// <param name="turnContext">The context object for this turn.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>A task that represents the work queued to execute.</returns>
protected override async Task OnTeamsSigninVerifyStateAsync(ITurnContext<IInvokeActivity> turnContext, CancellationToken cancellationToken)
{
_logger.LogInformation("Running dialog with signin/verifystate from an Invoke Activity.");
// The OAuth Prompt needs to see the Invoke Activity in order to complete the login process.
// Run the Dialog with the new Invoke Activity.
await _dialog.RunAsync(turnContext, _conversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
}
}
}