Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON value could not be converted to Telegram.Bot.Types.Enums.ChatType #865

Closed
lvyyln opened this issue Jan 29, 2020 · 7 comments
Closed

Comments

@lvyyln
Copy link

lvyyln commented Jan 29, 2020

I found a bug to report
When i am trying to deserealize data from body json can't convert ChatType

Steps to reproduce:
1.Sending command 'start'
2.Waiting while json will be deserealized
3.Then happend 'bad request'

image

NuGet Package Version: 15.2.0

.NET Version: Core 3.1

IDE: Rider

App: Desktop

@tuscen
Copy link
Member

tuscen commented Jan 29, 2020

Are you using System.Text.Json? The error message looks like from it.

@lvyyln
Copy link
Author

lvyyln commented Jan 30, 2020

No, i am not using System.Text.Json

@tuscen
Copy link
Member

tuscen commented Jan 30, 2020

Could you add more context? Is it a webhook on ASP.NET Core 3.1 or long polling mode? If it is webhook could you provide the code from startup and where you're deserializing webhook request?

@lvyyln
Copy link
Author

lvyyln commented Jan 30, 2020

So this is a webhook on ASP.NET Core 3.1

Startup :

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        Bot.GetBotClientAsync().Wait();
    }

   
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
    }
}

GetBotClientAsync :

    private static TelegramBotClient botClient;
    private static List<Command> commandsList;

    public static IReadOnlyList<Command> Commands => commandsList.AsReadOnly();

    public static async Task<TelegramBotClient> GetBotClientAsync()
    {
        if (botClient != null)
        {
            return botClient;
        }

        commandsList = new List<Command>();
        commandsList.Add(new StartCommand());

        botClient = new TelegramBotClient(AppSettings.Key);
        
        string hook = string.Format(AppSettings.Url,"api/message/update"); 
        await botClient.SetWebhookAsync(hook);

        return botClient;
    }

And for deserializing i'm using frombody attribute
My controller:

    public async Task<OkResult> Post([FromBody]Update update)
    {
        var commands = Bot.Commands;
        var message = update.Message;
        var botClient = await Bot.GetBotClientAsync();

        foreach (var command in commands)
        {
            if (command.Contains(message))
            {
                await command.Execute(message, botClient);
                break;
            }
        }
        return Ok();
        
    }

@tuscen
Copy link
Member

tuscen commented Jan 30, 2020

Ok, so you do use System.Text.Json, you just didn't know it. ASP.NET Core 3.0+ replaced Newtonsoft.Json as the default json formatter with STJ. But the client still doesn't support it yet. What you need to do is to install this nuget package and add it as the default json formatter. This should solve the problem.

services.AddControllers().AddNewtonsoftJson();

@lvyyln
Copy link
Author

lvyyln commented Jan 31, 2020

Thanks for help, everything is working, and maybe you write somewhere that for ASP.NET Core 3.0+ developers should use

services.AddControllers().AddNewtonsoftJson();

@tuscen tuscen closed this as completed Jan 31, 2020
@sergiycheck
Copy link

Thank you I had the same problem and it worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants