|
1 | 1 | using Microsoft.AspNetCore.Mvc; |
2 | 2 | using Microsoft.AspNetCore.Mvc.Formatters; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
3 | 4 | using System.Text; |
4 | 5 | using System.Text.Json; |
5 | | -using Telegram.Bot; |
6 | 6 | using Telegram.Bot.Requests.Abstractions; |
7 | 7 | using Telegram.Bot.Types; |
8 | 8 |
|
9 | | -namespace Microsoft.Extensions.DependencyInjection |
| 9 | +namespace Telegram.Bot.AspNetCore |
10 | 10 | { |
11 | | - /// <summary>Helpers for WebApp service configuration</summary> |
12 | | - public static class TelegramBotConfigureExtensions |
13 | | - { |
14 | | - /// <summary>Configure ASP.NET MVC Json (de)serialization for Telegram.Bot types</summary> |
15 | | - /// <param name="services">The IServiceCollection to add the services to.</param> |
16 | | - public static IServiceCollection ConfigureTelegramBotMvc(this IServiceCollection services) |
17 | | - => services.Configure<MvcOptions>(options => |
18 | | - { |
19 | | - options.InputFormatters.Insert(0, _inputFormatter); |
20 | | - options.OutputFormatters.Insert(0, _outputFormatter); |
21 | | - }); |
22 | | - |
23 | | - private static readonly TelegramBotInputFormatter _inputFormatter = new(); |
| 11 | + /// <summary>Helpers for WebApp service configuration</summary> |
| 12 | + public static class Extensions |
| 13 | + { |
| 14 | + /// <summary>Configure ASP.NET MVC Json (de)serialization for Telegram.Bot types</summary> |
| 15 | + /// <param name="services">The IServiceCollection to add the services to.</param> |
| 16 | + public static IServiceCollection ConfigureTelegramBotMvc(this IServiceCollection services) |
| 17 | + => services.Configure<MvcOptions>(options => |
| 18 | + { |
| 19 | + options.InputFormatters.Insert(0, _inputFormatter); |
| 20 | + options.OutputFormatters.Insert(0, _outputFormatter); |
| 21 | + }); |
| 22 | + |
| 23 | + private static readonly TelegramBotInputFormatter _inputFormatter = new(); |
24 | 24 | private static readonly TelegramBotOutputFormatter _outputFormatter = new(); |
25 | 25 |
|
26 | | - private class TelegramBotInputFormatter : TextInputFormatter |
27 | | - { |
28 | | - public TelegramBotInputFormatter() |
29 | | - { |
30 | | - SupportedEncodings.Add(Encoding.UTF8); |
31 | | - SupportedMediaTypes.Add("application/json"); |
32 | | - } |
33 | | - |
34 | | - protected override bool CanReadType(Type type) => type == typeof(Update); |
35 | | - |
36 | | - public sealed override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) |
37 | | - { |
38 | | - var model = await JsonSerializer.DeserializeAsync(context.HttpContext.Request.Body, context.ModelType, JsonBotAPI.Options, context.HttpContext.RequestAborted); |
39 | | - return await InputFormatterResult.SuccessAsync(model); |
40 | | - } |
41 | | - } |
42 | | - |
43 | | - private class TelegramBotOutputFormatter : TextOutputFormatter |
44 | | - { |
45 | | - public TelegramBotOutputFormatter() |
46 | | - { |
47 | | - SupportedEncodings.Add(Encoding.UTF8); |
48 | | - SupportedMediaTypes.Add("application/json"); |
49 | | - } |
50 | | - |
51 | | - protected override bool CanWriteType(Type? type) => typeof(IRequest).IsAssignableFrom(type); |
52 | | - |
53 | | - public sealed override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) |
54 | | - { |
55 | | - var stream = context.HttpContext.Response.Body; |
56 | | - await JsonSerializer.SerializeAsync(stream, context.Object, JsonBotAPI.Options, context.HttpContext.RequestAborted); |
57 | | - } |
58 | | - } |
59 | | - } |
| 26 | + private class TelegramBotInputFormatter : TextInputFormatter |
| 27 | + { |
| 28 | + public TelegramBotInputFormatter() |
| 29 | + { |
| 30 | + SupportedEncodings.Add(Encoding.UTF8); |
| 31 | + SupportedMediaTypes.Add("application/json"); |
| 32 | + } |
| 33 | + |
| 34 | + protected override bool CanReadType(Type type) => type == typeof(Update); |
| 35 | + |
| 36 | + public sealed override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) |
| 37 | + { |
| 38 | + var model = await JsonSerializer.DeserializeAsync(context.HttpContext.Request.Body, context.ModelType, JsonBotAPI.Options, context.HttpContext.RequestAborted); |
| 39 | + return await InputFormatterResult.SuccessAsync(model); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private class TelegramBotOutputFormatter : TextOutputFormatter |
| 44 | + { |
| 45 | + public TelegramBotOutputFormatter() |
| 46 | + { |
| 47 | + SupportedEncodings.Add(Encoding.UTF8); |
| 48 | + SupportedMediaTypes.Add("application/json"); |
| 49 | + } |
| 50 | + |
| 51 | + protected override bool CanWriteType(Type? type) => typeof(IRequest).IsAssignableFrom(type); |
| 52 | + |
| 53 | + public sealed override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) |
| 54 | + { |
| 55 | + var stream = context.HttpContext.Response.Body; |
| 56 | + await JsonSerializer.SerializeAsync(stream, context.Object, JsonBotAPI.Options, context.HttpContext.RequestAborted); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
60 | 60 | } |
0 commit comments