DEV Community

Benyamin Khalife
Benyamin Khalife

Posted on • Edited on

Download photo from Telegram Bot !

In the last Changes in 2.0.0.beta2 version, we can download photos sent in the Bot from Telegram servers and save them on our own server

Install the Botfire library using Composer:

composer require botfire/botfire:2.0.0.beta2
Enter fullscreen mode Exit fullscreen mode

To download a photo:

// Where to store photos on the server
$dir = __DIR__.'/photos/';

Bot::message()->photo()->last()->download($dir);
Enter fullscreen mode Exit fullscreen mode

By executing the download method as in the example above, the file is saved in the path we specified in $dir

With the forEach method, we can iterate through all the images of different sizes.

    Bot::message()->photo()->forEach(function(Photo $photo)use($dir){
        $name = $photo->getWidth().'_@name';
        $photo->download($dir, $name);
    });
Enter fullscreen mode Exit fullscreen mode

In the line $name = $photo->getWidth().'_@name'; we customize the filename. We add the width value to the beginning of the filename.
The @name expression in the download function is replaced with the original file name, giving us more flexibility in renaming the image.

I think the same structure would be suitable for other file types as well. So audio, video, and other types should be implemented and appropriate comments and documentation should be created for them.

Top comments (0)