Example #1
0
        public async Task HandleAsync(IUpdateContext context, UpdateDelegate next)
        {
            await next(context).ConfigureAwait(false);

            if (!context.Items.ContainsKey(nameof(WebhookResponse)))
            {
                return;
            }

            dynamic request = context.Items[nameof(WebhookResponse)];

            if (context.IsWebhook())
            {
                var httpContext = (HttpContext)context.Items[nameof(HttpContext)];

                if (httpContext.Response.HasStarted)
                {
                    // ToDo use logger and ignore
                    throw new InvalidOperationException("");
                }

                string      method      = request.MethodName;
                HttpContent httpContent = request.ToHttpContent();

                string json = await httpContent.ReadAsStringAsync()
                              .ConfigureAwait(false);

                json = json.Insert(json.Length - 1, $@",""method"":""{method}""");

                httpContext.Response.StatusCode  = 201;
                httpContext.Response.ContentType = "application/json";
                await httpContext.Response.WriteAsync(json, Encoding.UTF8)
                .ConfigureAwait(false);
            }
            else
            {
                await context.Bot.Client.MakeRequestAsync(request)
                .ConfigureAwait(false);
            }
        }
Example #2
0
 /// <summary>
 /// Gets the cancellation token for the current webhook HTTP request, if available,
 /// otherwise returns a default empty token.
 /// </summary>
 public static CancellationToken GetCancellationTokenOrDefault(this IUpdateContext context) =>
 context.IsWebhook()
         ? ((HttpContext)context.Items[nameof(HttpContext)]).RequestAborted
         : CancellationToken.None;