-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathActivityExtensions.cs
48 lines (40 loc) · 1.63 KB
/
ActivityExtensions.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
using Microsoft.Bot.Connector;
using Microsoft.Bot.Schema;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Bot.Builder.Community.Middleware.AzureAdAuthentication
{
public static class ActivityExtensions
{
public static bool UserHasJustSentMessage(this Activity activity)
{
return activity.Type == ActivityTypes.Message;
}
public static bool UserHasJustJoinedConversation(this Activity activity)
{
return activity.Type == ActivityTypes.ConversationUpdate && activity.MembersAdded.FirstOrDefault().Id != activity.Recipient.Id;
}
public static async Task DoWithTyping(this IActivity activity, Func<Task> action)
{
var cts = new CancellationTokenSource();
await activity.SendTypingActivity(cts.Token);
await action.Invoke().ContinueWith(task => { cts.Cancel(); });
}
private static async Task SendTypingActivity(this IActivity iactivity, CancellationToken cancellationToken)
{
if (iactivity is Activity activity)
{
var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
while (!cancellationToken.IsCancellationRequested)
{
var isTypingReply = activity.CreateReply();
isTypingReply.Type = ActivityTypes.Typing;
await connector.Conversations.ReplyToActivityAsync(isTypingReply);
await Task.Delay(1000, cancellationToken).ContinueWith(task => { });
}
}
}
}
}