Closed lentrodev closed 2 weeks ago
I'd be glad to do that, but unfortunately the Bot API doesn't have any machine readable documentation. Current documentation is just an html page without any classes, it's just a bunch of divs and p so one would have to rely on hueristics to differentiate between types and methods, etc. I asked bot support for a machine readable docs and they answered they don't have plans for that in near future since the team have other priorities.
@tuscen I had successfull attempts with parsing official Telegram Bot API documentation, but it wasn't good for using it as scheme for implementing requests and API models, so I abandoned this project. My idea applies to already implemented methods, so source generator will take already defined extesion methods in TelegramBotClientExtensions class and just parse they signatures and will create classes and interfaces based on them.
There's one disadvantage with using builders this way: builders are convenient when you use only their methods to set parameters. But about required parameters? If you set them in builder ctor it won't be as convenient. Otherwise you loose compile type safety.
@tuscen Anyway, if one of the required parameters is missing, Telegram will result an error when sending a request, so developer will see which parameter he forgot to specify. Or be can add some validation before sending a request, which will be a more correct solution, I think. I can test different solutions and propose a most correct one.
We're trying to have as much compile time safety as we reasonably can though.
@tuscen Okay, I'll think about that, will propose a solution ASAP.
To my mind, you can just use "required" when Telgram.Bot will be migrated to .NET 7. So problems with required parameters will be disappeared. And it looks no worse than the version with the builder.
public class SendTextMessageParameters
{
public required ChatId ChatId { get; init;} // or just set
public required string Text { get; init;} // or just set
public bool SomethingElse {get; init;}
}
In code
var param = new SendTextMessageParameters
{
ChatId = 2,
Text = "Vee"
}
We are now using required
keyword and source generation (just not MS source generator) by parsing the official documentation.
Automatically generated request parameters classes
I'd like to propose you to implement automatic generation of request parameters classes for each method available, using source generation.
For example, for the method
SendTextMessageAsync
will be created a new class namedSendTextMessageParameters
and interfaceISendTextMessageParameters
. Whole structure will look like this:And for make a request using this parameters, will be created overload for each method in the library.
In the
TelegramBotClientParametersExtensions
class:In your code:
I think, this will will help users with building and reusing this parameters for future requests.