/// <summary>
        /// Creates a turn context and runs the middleware pipeline for an incoming activity.
        /// </summary>
        /// <param name="authHeader">The HTTP authentication header of the request.</param>
        /// <param name="activity">The incoming activity.</param>
        /// <param name="callback">The code to run at the end of the adapter's middleware
        /// pipeline.</param>
        /// <returns>A task that represents the work queued to execute.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="activity"/> is <c>null</c>.</exception>
        /// <exception cref="UnauthorizedAccessException">
        /// authentication failed.</exception>
        /// <remarks>Call this method to reactively send a message to a conversation.
        /// <para>This method registers the following services for the turn.<list type="bullet">
        /// <item><see cref="IIdentity"/> (key = "BotIdentity"), a claims identity for the bot.</item>
        /// <item><see cref="IConnectorClient"/>, the channel connector client to use this turn.</item>
        /// </list></para>
        /// </remarks>
        /// <seealso cref="ContinueConversation(string, ConversationReference, Func{ITurnContext, Task})"/>
        /// <seealso cref="BotAdapter.RunPipeline(ITurnContext, Func{ITurnContext, Task}, System.Threading.CancellationTokenSource)"/>
        public async Task ProcessActivity(string authHeader, Activity activity, Func <ITurnContext, Task> callback)
        {
            BotAssertSlack.ActivityNotNull(activity);
            var claimsIdentity = await JwtTokenValidation.AuthenticateRequest(activity, authHeader, _credentialProvider, _httpClient);

            var context = new TurnContext(this, activity);

            context.Services.Add <IIdentity>("BotIdentity", claimsIdentity);
            var connectorClient = await this.CreateConnectorClientAsync(activity.ServiceUrl, claimsIdentity);

            context.Services.Add <IConnectorClient>(connectorClient);
            await base.RunPipeline(context, callback).ConfigureAwait(false);
        }
        /// <summary>
        /// Send a reply with the template
        /// </summary>
        /// <param name="context"></param>
        /// <param name="language"></param>
        /// <param name="templateId"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task ReplyWith(ITurnContext context, string templateId, object data = null)
        {
            BotAssertSlack.ContextNotNull(context);

            // apply template
            Activity boundActivity = await this.RenderTemplate(context, context.Activity?.AsMessageActivity()?.Locale, templateId, data).ConfigureAwait(false);

            if (boundActivity != null)
            {
                await context.SendActivity(boundActivity);

                return;
            }
            return;
        }
示例#3
0
        public async Task <IList <Intent> > Recognize(ITurnContext context)
        {
            BotAssertSlack.ContextNotNull(context);

            bool isEnabled = await IsRecognizerEnabled(context).ConfigureAwait(false);

            if (isEnabled)
            {
                var allRecognizedIntents = await RunRecognizer(context).ConfigureAwait(false);
                await RunFilters(context, allRecognizedIntents);

                return(allRecognizedIntents);
            }
            else
            {
                return(new List <Intent>());
            }
        }
        protected async Task RunPipeline(ICommandContext context, Func <ICommandContext, Task> callback = null, CancellationTokenSource cancelToken = null)
        {
            BotAssertSlack.ContextNotNull(context);

            // Call any registered Middleware Components looking for ReceiveActivity()
            if (context.Command != null)
            {
                await _middlewareSet.ReceiveActivityWithStatus(context, callback).ConfigureAwait(false);
            }
            else
            {
                // call back to caller on proactive case
                if (callback != null)
                {
                    await callback(context).ConfigureAwait(false);
                }
            }
        }
示例#5
0
        public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next)
        {
            BotAssertSlack.ContextNotNull(context);

            var intents = await this.Recognize(context);

            var result = new IntentRecognition();

            if (intents.Count != 0)
            {
                result.Intents = intents;
                var topIntent = FindTopIntent(intents);
                if (topIntent.Score > 0.0)
                {
                    result.TopIntent = topIntent;
                }
            }
            context.Services.Add((IRecognizedIntents)result);
            await next().ConfigureAwait(false);
        }