TelegramBots / Telegram.Bot

.NET Client for Telegram Bot API
https://telegrambots.github.io/book
MIT License
3.22k stars 690 forks source link

json serialization error #873

Closed mazizovv closed 4 years ago

mazizovv commented 4 years ago

Error during json serialization. If in json there are chat.type, date, and entities fields, then the system cannot serialize it and generates an error 400 - bad request.

I checked, if you remove these fields from json, then serialization is successful.

NuGet Package Version: (e.g. 13.0.1) Version 15.3.0

.NET Version: (e.g. 4.7, Core 1.1, Mono, etc.) Core 3.1

IDE: (e.g. VS2017, VS Code, etc.) VS2019

App: (e.g. Desktop, iOS, Android, etc.) Asp.Net Core WebApi

mazizovv commented 4 years ago

This request goes fine, I changed the values ​​of the chat.type and messageEntity.type fields to a number. Removed the date field, since it also does not convert

{ "update_id": 126023807, "message": { "message_id": 22, "from": { "id": 427363877, "is_bot": false, "first_name": "Maruf", "last_name": "Azizov", "language_code": "ru" }, "chat": { "id": 427363877, "first_name": "Maruf", "last_name": "Azizov", "type": 0 }, "text": "/start", "entities": [ { "offset": 0, "length": 6, "type": 2 } ] } }

tuscen commented 4 years ago

Could you provide raw json update that fails to deserialize?

tuscen commented 4 years ago

I see that you're using NET Core 3.1 and web api. If you're using webhook, you need to add newtonsoft.json as json formatter. See there https://telegrambots.github.io/book/FAQ.html#i-have-serialization-issues-or-null-values-in-update-object-in-my-webhook-what-do-i-do. It might be the culprit of your problem.

mazizovv commented 4 years ago

this raw json is not serialized

{"update_id":126023825, "message":{"message_id":51,"from":{"id":427363877,"is_bot":false,"first_name":"Maruf","last_name":"Azizov","language_code":"ru"},"chat":{"id":427363877,"first_name":"Maruf","last_name":"Azizov","type":"private"},"date":1581672183,"text":"/start","entities":[{"offset":0,"length":6,"type":"bot_command"}]}}

mazizovv commented 4 years ago

I see that you're using NET Core 3.1 and web api. If you're using webhook, you need to add newtonsoft.json as json formatter. See there https://telegrambots.github.io/book/FAQ.html#i-have-serialization-issues-or-null-values-in-update-object-in-my-webhook-what-do-i-do. It might be the culprit of your problem.

this did not help, I added the library. After adding this package to services at startup, the error changed to 500

tuscen commented 4 years ago

Could you show me your Startup class and code where you're deserializing incoming updates?

mazizovv commented 4 years ago

[HttpPost] public async Task Post([FromBody]Update update) { var client = await Bot.GetBotClientAsync(); var result = await client.SendTextMessageAsync(427363877, "Hello"); return Ok();
}

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

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        //services.AddMvc()
        //    .AddNewtonsoftJson(option =>
        //        option.SerializerSettings.ContractResolver =
        //            new CamelCasePropertyNamesContractResolver());
        services.AddControllers();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        //app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

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

        Bot.GetBotClientAsync().Wait();
    }
}`
tuscen commented 4 years ago

You didn't add json fomatter. Try adding this:

services.AddControllers().AddNewtonsoftJson();

mazizovv commented 4 years ago

Yes you are right. Thank you!

I have 1 more question that is off topic. What could be the problem if the request for the message comes from the telegram servers, but I cannot send a message via the webhook to the bot. There are no errors, everything is fine, but the messages do not reach the bot.

mazizovv commented 4 years ago

if I send a request through the browser with the sendMessage command, then everything will reach the bot normally but not through the webhook

tuscen commented 4 years ago

Answering webhook requests is not fully supported yet (e.g. requests with streams can't be sent as a response). To do that you need to set IsWebhookRequest property to true before sending the request.

If you have unrelated to the issue questions, please, join our group https://t.me/joinchat/B35YY0QbLfd034CFnvCtCA. We use issue tracker only for bug reports and feature requests.

mazizovv commented 4 years ago

thanks for the help