Beispiel #1
0
        /// <summary>
        /// Deserializes the incoming request using a BotMessageHandler, processes it with an <see cref="IAdapterIntegration"/>
        /// and returns an <see cref="InvokeResponse"/>.
        /// </summary>
        /// <param name="request">A <see cref="HttpRequest"/>.</param>
        /// <param name="adapter">An instance of <see cref="IAdapterIntegration"/>.</param>
        /// <param name="botCallbackHandler">An instance of <see cref="BotCallbackHandler"/>.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/>.</param>
        /// <returns>An <see cref="InvokeResponse"/> returned from the adapter.</returns>
        protected override async Task <InvokeResponse> ProcessMessageRequestAsync(HttpRequest request, IAdapterIntegration adapter, BotCallbackHandler botCallbackHandler, CancellationToken cancellationToken)
        {
            Activity activity;

            using (var memoryStream = new MemoryStream())
            {
                await request.Body.CopyToAsync(memoryStream).ConfigureAwait(false);

                // In the case of request buffering being enabled, we could possibly receive a Stream that needs its position reset,
                // but we can't _blindly_ reset in case buffering hasn't been enabled since we'll be working with a non-seekable Stream
                // in that case which will throw a NotSupportedException
                if (request.Body.CanSeek)
                {
                    request.Body.Position = 0;
                }

                memoryStream.Position = 0;

                // Get the request body and deserialize to the Activity object.
                // NOTE: We explicitly leave the stream open here so others can still access it (in case buffering was enabled); ASP.NET runtime will always dispose of it anyway
                using (var bodyReader = new JsonTextReader(new StreamReader(memoryStream, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: 1024, leaveOpen: true)))
                {
                    activity = BotMessageSerializer.Deserialize <Activity>(bodyReader);
                }
            }

            var invokeResponse = await adapter.ProcessActivityAsync(
                request.Headers["Authorization"],
                activity,
                botCallbackHandler,
                cancellationToken).ConfigureAwait(false);

            return(invokeResponse);
        }
        protected override async Task <InvokeResponse> ProcessMessageRequestAsync(HttpRequest request, BotFrameworkAdapter botFrameworkAdapter, Func <ITurnContext, Task> botCallbackHandler)
        {
            const string BotAppIdHttpHeaderName        = "MS-BotFramework-BotAppId";
            const string BotIdQueryStringParameterName = "BotAppId";

            if (!request.Headers.TryGetValue(BotAppIdHttpHeaderName, out var botAppIdHeaders))
            {
                if (!request.Query.TryGetValue(BotIdQueryStringParameterName, out botAppIdHeaders))
                {
                    throw new InvalidOperationException($"Expected a Bot App ID in a header named \"{BotAppIdHttpHeaderName}\" or in a querystring parameter named \"{BotIdQueryStringParameterName}\".");
                }
            }

            var botAppId = botAppIdHeaders.First();
            var conversationReference = default(ConversationReference);

            using (var bodyReader = new JsonTextReader(new StreamReader(request.Body, Encoding.UTF8)))
            {
                conversationReference = BotMessageSerializer.Deserialize <ConversationReference>(bodyReader);
            }

            await botFrameworkAdapter.ContinueConversation(botAppId, conversationReference, botCallbackHandler);

            return(null);
        }
        protected override async Task <InvokeResponse> ProcessMessageRequestAsync(HttpRequest request, BotFrameworkAdapter botFrameworkAdapter, Func <ITurnContext, Task> botCallbackHandler)
        {
            var activity = default(Activity);

            using (var bodyReader = new JsonTextReader(new StreamReader(request.Body, Encoding.UTF8)))
            {
                activity = BotMessageSerializer.Deserialize <Activity>(bodyReader);
            }

            var invokeResponse = await botFrameworkAdapter.ProcessActivity(request.Headers["Authorization"], activity, botCallbackHandler);

            return(invokeResponse);
        }
Beispiel #4
0
       public async Task PostAsync()
       {
           Request.EnableBuffering();
           using (var buffer = new MemoryStream())
           {
               await Request.Body.CopyToAsync(buffer);
               buffer.Position = 0L;
               using (var bodyReader = new JsonTextReader(new StreamReader(buffer, Encoding.UTF8)))
               {
                   Debug.Print(BotMessageSerializer.Deserialize(bodyReader).ToString());
                   buffer.Position = 0L;
               }
           }
           Request.Body.Position = 0;
 
         await Adapter.ProcessAsync(Request, Response, Bot);
       }