forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBotActivityControllerBase.cs
More file actions
108 lines (102 loc) · 5.49 KB
/
Copy pathBotActivityControllerBase.cs
File metadata and controls
108 lines (102 loc) · 5.49 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// It is common practice to switch on the type of inbound Activity in the bot logic. This class implements that
/// type switch for the common Message and ConversationUpdate Activities.
/// </summary>
public class BotActivityControllerBase : BotControllerBase
{
/// <summary>
/// This is an implementation of OnTurnAsync that provides the typical switch on Activity type.
/// </summary>
/// <param name="turnContext">A <see cref="ITurnContext"/> containing all the data needed
/// for processing this conversation turn. </param>
/// <param name="cancellationToken">(Optional) A <see cref="CancellationToken"/> that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A <see cref="Task"/> that represents the work queued to execute.</returns>
protected override Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken)
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
return OnMessageActivityAsync(turnContext, cancellationToken);
}
else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
{
return OnConversationUpdateActivityAsync(turnContext, cancellationToken);
}
else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
{
return OnConversationUpdateActivityAsync(turnContext, cancellationToken);
}
else
{
return OnOtherActivityAsync(turnContext, cancellationToken);
}
}
/// <summary>
/// Override this method to add custom processing for Message Activities.
/// </summary>
/// <param name="turnContext">A <see cref="ITurnContext"/> containing all the data needed
/// for processing this conversation turn. </param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A <see cref="Task"/> that represents the work queued to execute.</returns>
protected virtual Task OnMessageActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
/// <summary>
/// Override this method to add custom processing for ConversationUpdate Activities. The base implementation
/// supports the common pattern of specific processing per newly added member.
/// </summary>
/// <param name="turnContext">A <see cref="ITurnContext"/> containing all the data needed
/// for processing this conversation turn. </param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A <see cref="Task"/> that represents the work queued to execute.</returns>
protected virtual async Task OnConversationUpdateActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken)
{
if (turnContext.Activity.MembersAdded != null)
{
foreach (var member in turnContext.Activity.MembersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
await OnMemberAddedAsync(member, turnContext, cancellationToken);
}
}
}
}
/// <summary>
/// Override this method to add custom processing for each added member. For example a Welcome message.
/// </summary>
/// <param name="member">A <see cref="ChannelAccount"/> corresponding to the added member.</param>
/// <param name="turnContext">A <see cref="ITurnContext"/> containing all the data needed
/// for processing this conversation turn. </param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A <see cref="Task"/> that represents the work queued to execute.</returns>
protected virtual Task OnMemberAddedAsync(ChannelAccount member, ITurnContext turnContext, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
/// <summary>
/// Override this method to add custom processing for any other type of Activity not covered in this sample.
/// </summary>
/// <param name="turnContext">A <see cref="ITurnContext"/> containing all the data needed
/// for processing this conversation turn. </param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A <see cref="Task"/> that represents the work queued to execute.</returns>
protected virtual Task OnOtherActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}