public static bool SendActivity(this TaskManager taskManager, ITurnContext context, string identifier, Activity activity, int millisecondsDelay, ChannelAccount from = null) { var bot = context.TurnState.Get <IBot>(); var adapter = context.TurnState.Get <BotAdapter>(); var microsoftAppId = context.TurnState.Get <IConfiguration>().GetValue <string>("MicrosoftAppId"); var referenceMiddleware = context.TurnState.Get <ReferenceMiddleware>(); var userKey = ReferenceMiddleware.GetUserKey(context.Activity); return(taskManager.Start(userKey, identifier, async(CancellationToken cancel) => { try { await Task.Delay(millisecondsDelay, cancel); } catch (TaskCanceledException ex) { return; } taskManager.Stop(userKey, identifier, false); // Get the lastest reference var reference = referenceMiddleware.GetConversationReference(context.Activity); reference = new ConversationReference { ActivityId = reference.ActivityId, User = from ?? reference.User, Bot = reference.Bot, Conversation = reference.Conversation, ChannelId = reference.ChannelId, Locale = string.IsNullOrEmpty(activity.Locale) ? reference.Locale : activity.Locale, ServiceUrl = reference.ServiceUrl }; // TODO we only cancel delay await((BotAdapter)adapter).ContinueConversationAsync(microsoftAppId, reference, async(tc, ct) => { // TODO middlewares still use ContinueConversation activity tc.Activity.Type = activity.Type; tc.Activity.Name = activity.Name; tc.Activity.Text = activity.Text; tc.Activity.Speak = activity.Speak; tc.Activity.Value = activity.Value; tc.Activity.Attachments = activity.Attachments; tc.Activity.AttachmentLayout = activity.AttachmentLayout; await bot.OnTurnAsync(tc, ct); }, default(CancellationToken)); })); }
public static bool Stop(this TaskManager taskManager, ITurnContext context, string identifier) { var userKey = ReferenceMiddleware.GetUserKey(context.Activity); return(taskManager.Stop(userKey, identifier)); }