private async Task DeleteActivityInternal(ConversationReference cr, IEnumerable <DeleteActivityHandler> updateHandlers, Func <Task> callAtBottom) { BotAssertSlack.ConversationReferenceNotNull(cr); if (updateHandlers == null) { throw new ArgumentException(nameof(updateHandlers)); } if (updateHandlers.Count() == 0) // No middleware to run. { if (callAtBottom != null) { await callAtBottom(); } return; } // Default to "No more Middleware after this". async Task next() { // Remove the first item from the list of middleware to call, // so that the next call just has the remaining items to worry about. IEnumerable <DeleteActivityHandler> remaining = updateHandlers.Skip(1); await DeleteActivityInternal(cr, remaining, callAtBottom).ConfigureAwait(false); } // Grab the current middleware, which is the 1st element in the array, and execute it. DeleteActivityHandler toCall = updateHandlers.First(); await toCall(this, cr, next); }
/// <summary> /// Creates a conversation reference from an activity. /// </summary> /// <param name="activity">The activity.</param> /// <returns>A conversation reference for the conversation that contains the activity.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="activity"/> is <c>null</c>.</exception> public static ConversationReference GetConversationReference(Activity activity) { BotAssertSlack.ActivityNotNull(activity); ConversationReference r = new ConversationReference { ActivityId = activity.Id, User = activity.From, Bot = activity.Recipient, Conversation = activity.Conversation, ChannelId = activity.ChannelId, ServiceUrl = activity.ServiceUrl }; return(r); }
/// <summary> /// Starts activity processing for the current bot turn. /// </summary> /// <param name="context">The turn's context object.</param> /// <param name="callback">A callback method to run at the end of the pipeline.</param> /// <param name="cancelToken">A cancellation token for the task.</param> /// <returns>A task that represents the work queued to execute.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="context"/> is null.</exception> /// <remarks>The adapter calls middleware in the order in which you added it. /// The adapter passes in the context object for the turn and a next delegate, /// and the middleware calls the delegate to pass control to the next middleware /// in the pipeline. Once control reaches the end of the pipeline, the adapter calls /// the <paramref name="callback"/> method. If a middleware component doesn’t call /// the next delegate, the adapter does not call any of the subsequent middleware’s /// <see cref="IMiddleware.OnTurn(ITurnContext, MiddlewareSet.NextDelegate)"/> /// methods or the callback method, and the pipeline short circuits. /// <para>When the turn is initiated by a user activity (reactive messaging), the /// callback method will be a reference to the bot's /// <see cref="IBot.OnTurn(ITurnContext)"/> method. When the turn is /// initiated by a call to <see cref="ContinueConversation(ConversationReference, Func{ITurnContext, Task})"/> /// (proactive messaging), the callback method is the callback method that was provided in the call.</para> /// </remarks> protected async Task RunPipeline(ITurnContext context, Func <ITurnContext, Task> callback = null, CancellationTokenSource cancelToken = null) { BotAssertSlack.ContextNotNull(context); // Call any registered Middleware Components looking for ReceiveActivity() if (context.Activity != null) { await _middlewareSet.ReceiveActivityWithStatus(context, callback).ConfigureAwait(false); } else { // call back to caller on proactive case if (callback != null) { await callback(context).ConfigureAwait(false); } } }
public MiddlewareSetSlack Use(IMiddlewareSlack middleware) { BotAssertSlack.MiddlewareNotNull(middleware); _middleware.Add(middleware); return(this); }