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

sendMediaGroup()

Open Itsmaqsud opened this issue 4 years ago • 16 comments

Hi, i want send a group of photos or videos as an album. But your method not working:

$media = [];
$media[] = [
	'type'	=> 'photo',
	'media'	=> InputFile::create('https://example.com/file1.jpg')
];

$media[] = [
	'type'	=> 'photo',
	'media'	=> InputFile::create('https://example.com/file2.jpg')
];

$media[] = [
	'type'	=> 'video',
	'media'	=> InputFile::create('https://example.com/file.mp4')
];

$param = [
	'chat_id' 	=>  $chat_id,
	'media'     => $media
];
Telegram::sendMediaGroup($param);

Laravel => 7.9 SDK => 3.0.0

Maybe I made a mistake? Please, help me!

Itsmaqsud avatar May 04 '20 19:05 Itsmaqsud

@irazasyed please, answer me

Itsmaqsud avatar May 04 '20 22:05 Itsmaqsud

I'll look into this shortly.

jonnywilliamson avatar May 05 '20 12:05 jonnywilliamson

@jonnywilliamson Error debug:

A path to local file, a URL, or a file resource should be uploaded using "Telegram\Bot\FileUpload\InputFile::create($pathOrUrlOrResource, $filename)" for "media" property.

But InputFile::create() is used.

Itsmaqsud avatar May 05 '20 15:05 Itsmaqsud

We're in the process of a big announcement very very shortly. This issue will be dealt with very soon if you could please just bare with us a little longer. It'll be worth the wait.

jonnywilliamson avatar May 07 '20 11:05 jonnywilliamson

Yes, sure. I'm wait :) Thanks!

Itsmaqsud avatar May 07 '20 11:05 Itsmaqsud

interested in this as well

reggie84 avatar May 10 '20 02:05 reggie84

i am waiting too :)

etas34 avatar May 12 '20 11:05 etas34

Hey. I don’t know if my decision is right, but the following helped me:

$inputMediaPhoto = [ 'type' => 'photo', 'media' => '...', 'parse_mode' => 'HTML' ]; $inputMediaPhoto = \GuzzleHttp\json_encode($inputMediaPhoto);

I hope you get the point.

s11e08 avatar May 17 '20 12:05 s11e08

It does not working for me at all.

@s11e08 tell me please, what did you use for media in your example? Local path, URL or resource?

sigaev-pro avatar May 24 '20 20:05 sigaev-pro

It does not working for me at all.

@s11e08 tell me please, what did you use for media in your example? Local path, URL or resource?

Я поторопился с ответом в эту тему, извини. Этот способ действительно помог мне с методом editMessageMedia, в качестве media я использовал file_id. Но сейчас проверил на sendMediaGroup и словил ошибку. Надеюсь новая версия библиотеки с фиксом всех багов выйдет скоро

s11e08 avatar May 25 '20 08:05 s11e08

Решил кто то вопрос?

     $arr = [];
        foreach ($array as $item){
            $arr[] = InputMediaPhoto::make([
                "media"=> InputFile::create($item->url),
                "caption"=>$text,
            ]);
        }    
        $add = Telegram::sendMediaGroup([
            "chat_id"=>$cid,
            "media"=>$arr,
        ]);

И пишет ошибку A path to local file, a URL, or a file resource should be uploaded using "Telegram\Bot\FileUpload\InputFile::create($pathOrUrlOrResource, $filename)" for "media" property.

@irazasyed please, answer me

dmdevelops avatar Jun 19 '20 13:06 dmdevelops

big announcement very very shortly

@jonnywilliamson any update on this ? i cannot use this method

foremtehan avatar Aug 29 '20 19:08 foremtehan

@foremtehan

Ah its very simple. We are on the cusp of a MASSIVE new release of the SDK. With some INSANE cool features. But Covid-19 managed to cause a lot of delay with lockdowns and people close to us affected.

So we are just trying to deal with that and finish off the documentation.

I can promise you it is worth the wait though.

jonnywilliamson avatar Sep 01 '20 11:09 jonnywilliamson

@foremtehan

Ah its very simple. We are on the cusp of a MASSIVE new release of the SDK. With some INSANE cool features. But Covid-19 managed to cause a lot of delay with lockdowns and people close to us affected.

So we are just trying to deal with that and finish off the documentation.

I can promise you it is worth the wait though.

Its now? Ok?

APTEMOH avatar Nov 03 '20 12:11 APTEMOH

Please Go to telegram-bot-sdk/src/Traits/Http.php Edit Line number 343 Replace This code if ((! $params[$inputFileField] instanceof InputFile) **&&** (is_string($params[$inputFileField]) && ! $this->is_json($params[$inputFileField]))) { This solved my problem.

istiake5 avatar Jan 25 '22 05:01 istiake5

On an array, don't use InputFile::create. The media parameter in sendMediaGroup - serialized JSON array describing the messages to be sent.

$media = array(
  array(
    'type' => 'photo',
    'media' => 'https://example.com/file1.jpg',
    'caption' => '<b>Caption</b>',
    'parse_mode' => 'HTML'
  ),
  array(
    'type' => 'photo',
    'media' => 'https://example.com/file2.jpg',
  )
);

$response = $telegram->sendMediaGroup([
  'chat_id' => $chat_id,
  'media' => json_encode($media),
]);

gloriusglo avatar Jul 13 '22 19:07 gloriusglo

@irazasyed, I think it is necessary to make it possible to pass an array with Telegram\Bot\FileUpload\InputFile::create($pathOrUrlOrResource, $filename) objects to the sendMediaGroup() method. This is a very cool method, but instead now you have to send one file at a time, get "fileId" in the response message, delete the sent message, and then collect a special "media" array to make the send beautiful. If there is already some way to do it right - write, please! If you can fix something locally in the package so that the sendMediaGroup() method works as described, please explain, I'll fix it.

Charoday avatar May 13 '23 18:05 Charoday

I know for fact this is the current way to do it in latest version.

Telegram::bot()->sendMediaGroup(
            [
                'chat_id' => '1234567',
                'media'   => [
                    InputMediaPhoto::make()->media(InputFile::file('filename1.jpg'),
                    InputMediaPhoto::make()->media(InputFile::file('filename2.jpg'),
                    InputMediaPhoto::make()->media(InputFile::file('filename3.jpg'),
                    InputMediaPhoto::make()->media(InputFile::file('filename4.jpg'),
                ]
            ];
            

jonnywilliamson avatar May 15 '23 09:05 jonnywilliamson

InputFile::file()

Are you sure this is a right way? I don't have such method in 3.13.0 version...

art-zhitnik avatar Jun 14 '23 10:06 art-zhitnik

$media = array( array( 'type' => 'photo', 'media' => 'https://example.com/file1.jpg', 'caption' => 'Caption', 'parse_mode' => 'HTML' ), array( 'type' => 'photo', 'media' => 'https://example.com/file2.jpg', ) );

$response = $telegram->sendMediaGroup([ 'chat_id' => $chat_id, 'media' => json_encode($media), ]);

this works just change the links to a valid image ,, but it doesn't work in local images from storage it has to be over http,, any help with that??

torgodly avatar Mar 09 '24 21:03 torgodly

Я нашел решение, как отправлять локальные файлы при помощи sendMediaGroup. Решение на Laravel, но без него принцип не меняется.

$file = public_path() . "/" . 'storage/messages/45322/images/OQHLV0oEdfYw3PxBzpLkj2XKM16NUgcZK1xkCEc5.png';
$photo = InputFile::create($file);

Telegram::bot('tools')->sendMediaGroup([
    'chat_id' => env('TELEGRAM_TOOLS_ID'),
    'photo_1' => $photo,
    'media' => json_encode([
        // HTTP File
        InputMediaPhoto::make([
            'type' => 'photo',
            'media' => 'https://avatars.mds.yandex.net/i?id=69cdc84022bfdb05b07a23f9d28fb7f1f72109ea-7909006-images-thumbs&n=13',
            'caption' => '<b>New message</b>',
            'parse_mode' => 'HTML',
        ]),
        // By file_id
        InputMediaPhoto::make([
            'type' => 'photo',
            'media' => 'AgACAgIAAxkDAAOsZfvgo10RU6ebLA4yLuRMdZmTll8AArPUMRsF4NhL19kGUqyw358BAAMCAANzAAM0BA',
        ]),
        // By local
        InputMediaPhoto::make([
            'type' => 'photo',
            'media' => 'attach://photo_1',
        ]),
    ]),
]);

YouMixx avatar Mar 21 '24 08:03 YouMixx