telegram-bot-api icon indicating copy to clipboard operation
telegram-bot-api copied to clipboard

Change the way file inputs are handled

Open vudaltsov opened this issue 8 months ago • 0 comments

Hi, thank you for the library.

I use it in an AMPHP project with a custom transport leveraging https://amphp.org/http-client.

I've noticed that in all transports the same Telegram-specific logic is repeated every time and could be easily abstracted away:

  • http-method resolution,
  • finding files in data,
  • json-encoding non-scalar fields.

As an implementor of a transport I do not want to care about these details. I just want to operate on the level of http abstractions and to send the almost ready request. Here's the interface I propose:

interface TransportInterface
{
    public function get(string $url): ApiResponse;

    /**
     * @param array<string, string> $headers
     */
    public function post(string $url, string $body, array $headers): ApiResponse;

    /**
     * @param array<string, string> $fields Non-scalar fields are already JSON encoded!
     * @param non-empty-array<string, InputFile> $files
     * @param array<string, string> $headers
     */
    public function postWithFiles(string $url, array $fields, array $files, array $headers): ApiResponse;

    // ... download
}

This will drastically simplify transports.

vudaltsov avatar May 18 '25 05:05 vudaltsov