protected override async Task ProcessMessageRequestAsync(HttpRequestMessage request, BotFrameworkAdapter botFrameworkAdapter, Func <ITurnContext, Task> botCallbackHandler, CancellationToken cancellationToken) { var activity = await request.Content.ReadAsAsync <Activity>(BotMessageHandlerBase.BotMessageMediaTypeFormatters, cancellationToken); await botFrameworkAdapter.ProcessActivity( request.Headers.Authorization?.Parameter, activity, botCallbackHandler); }
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); }
public async Task <IActionResult> Post([FromBody] Activity activity) { try { await adapter.ProcessActivity(this.Request.Headers["Authorization"].FirstOrDefault(), activity, BotReceiveHandler); return(this.Ok()); } catch (UnauthorizedAccessException) { return(this.Unauthorized()); } }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (request.Method != HttpMethod.Post) { return(request.CreateResponse(HttpStatusCode.MethodNotAllowed)); } var requestContent = request.Content; var requestContentHeaders = requestContent.Headers; if (requestContentHeaders.ContentLength == 0) { return(request.CreateErrorResponse(HttpStatusCode.BadRequest, "Request body should not be empty.")); } if (!BotActivityMediaTypeFormatters[0].SupportedMediaTypes.Contains(requestContentHeaders.ContentType)) { return(request.CreateErrorResponse(HttpStatusCode.NotAcceptable, $"Expecting Content-Type of \"{BotActivityMediaTypeFormatters[0].SupportedMediaTypes[0].MediaType}\".")); } var activity = await requestContent.ReadAsAsync <Activity>(BotActivitiesHandler.BotActivityMediaTypeFormatters, cancellationToken); try { await _botFrameworkAdapter.ProcessActivity( request.Headers.Authorization?.Parameter, activity, botContext => { cancellationToken.ThrowIfCancellationRequested(); IBot bot; try { bot = (IBot)request.GetDependencyScope().GetService(typeof(IBot)); } catch { bot = null; } if (bot == null) { throw new InvalidOperationException($"Did not find an {typeof(IBot).Name} service via the dependency resolver. Please make sure you have registered your bot with your dependency injection container."); } return(bot.OnReceiveActivity(botContext)); }); return(request.CreateResponse(HttpStatusCode.OK)); } catch (UnauthorizedAccessException e) { return(request.CreateErrorResponse(HttpStatusCode.Unauthorized, e.Message)); } catch (InvalidOperationException e) { return(request.CreateErrorResponse(HttpStatusCode.NotFound, e.Message)); } }
public static IApplicationBuilder UseBotFramework(this IApplicationBuilder applicationBuilder) { var options = applicationBuilder.ApplicationServices.GetRequiredService <IOptions <BotFrameworkOptions> >().Value; var botFrameworkAdapter = new BotFrameworkAdapter(options.ApplicationId, options.ApplicationPassword); foreach (var middleware in options.Middleware) { botFrameworkAdapter.Use(middleware); } var botActivitiesPath = new PathString(options.RouteBaseUrl); botActivitiesPath.Add("/activities"); applicationBuilder.Map( botActivitiesPath, botActivitiesAppBuilder => botActivitiesAppBuilder.Run(BotRequestHandler)); return(applicationBuilder); async Task BotRequestHandler(HttpContext httpContext) { var request = httpContext.Request; var response = httpContext.Response; if (request.Method != HttpMethods.Post) { response.StatusCode = (int)HttpStatusCode.MethodNotAllowed; return; } if (request.ContentType != "application/json") { response.StatusCode = (int)HttpStatusCode.NotAcceptable; return; } if (request.ContentLength == 0) { response.StatusCode = (int)HttpStatusCode.BadRequest; return; } var activity = default(Activity); using (var bodyReader = new JsonTextReader(new StreamReader(request.Body, Encoding.UTF8))) { activity = ActivitySerializer.Deserialize <Activity>(bodyReader); } try { await botFrameworkAdapter.ProcessActivity( request.Headers["Authorization"], activity, botContext => { var bot = httpContext.RequestServices.GetRequiredService <IBot>(); return(bot.OnReceiveActivity(botContext)); }); response.StatusCode = (int)HttpStatusCode.OK; } catch (UnauthorizedAccessException) { response.StatusCode = (int)HttpStatusCode.Forbidden; } } }