/// <summary> /// Gets invoked to handle the incoming request /// </summary> /// <param name="context"></param> public async Task Invoke(HttpContext context) { if (!( context.Request.Method == HttpMethods.Post && _botManager.WebhookUrl.EndsWith(context.Request.Path) )) { await _next.Invoke(context); return; } string data; using (var reader = new StreamReader(context.Request.Body)) { data = await reader.ReadToEndAsync(); } _logger.LogTrace($"Update Data:`{data}`"); Update update = null; try { update = JsonConvert.DeserializeObject <Update>(data); } catch (JsonException e) { _logger.LogWarning($"Unable to deserialize update payload. {e.Message}"); } if (update == null) { context.Response.StatusCode = StatusCodes.Status400BadRequest; return; } try { await _botManager.HandleUpdateAsync(update); context.Response.StatusCode = StatusCodes.Status200OK; } catch (Exception e) { _logger.LogError($"Error occured while handling update `{update.Id}`. {e.Message}"); context.Response.StatusCode = StatusCodes.Status500InternalServerError; } }