Skip to content

Helper for sending InputMedia objects #934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

noplanman
Copy link
Member

@noplanman noplanman commented Mar 9, 2019

Neat little internal helper to simplify the passing of files to media related methods.

Basically, it automatically generated multipart fields with the attached files and links them automagically.

todo: Same procedure for thumb fields.

Request::sendMediaGroup([
    'chat_id'  => $chat_id,
    'media'    => [
        new InputMediaPhoto(['media' => '/path/to/some/photo.png']), // direct path as string is now possible too!
        new InputMediaVideo(['media' => Request::encodeFile('/path/to/my/video.mp4'))]),
        new InputMediaPhoto(['media' => Request::encodeFile('/path/to/another/photo.png']),
    ],
]);

instead of

Request::sendMediaGroup([
    'chat_id'  => $chat_id,
    'photo_1'  => Request::encodeFile('/path/to/some/photo.png'),
    'photo_2'  => Request::encodeFile('/path/to/another/photo.png'),
    'my_video' => Request::encodeFile('/path/to/my/video.mp4'),
    'media'    => [
        new InputMediaPhoto(['media' => 'attach://photo_1']),
        new InputMediaVideo(['media' => 'attach://my_video']),
        new InputMediaPhoto(['media' => 'attach://photo_2']),
    ],
]);

and

Request::editMessageMedia([
    'chat_id'    => $chat_id,
    'message_id' => $message_id,
    'media'      => new InputMediaPhoto([
        'caption' => 'New Caption!',
        'media'   => '/path/to/some/photo.png',
    ]),
]);

instead of

Request::editMessageMedia([
    'chat_id'    => $chat_id,
    'message_id' => $message_id,
    'photo_1'    => Request::encodeFile('/path/to/some/photo.png'),
    'media'      => new InputMediaPhoto([
        'caption' => 'New Caption!',
        'media'   => 'attach://photo_1',
    ]),
]);

…p()` and `Request::editMediaMessage()` methods.
@noplanman noplanman requested a review from jacklul March 9, 2019 02:26
Copy link
Collaborator

@jacklul jacklul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sendMediaGroup seems to return array of Update objects, I believe it should be array of Message objects instead

@noplanman
Copy link
Member Author

@jacklul Absolutely agreed, as stated in the API too. Will do the same for any other methods that return an array of objects 👍

@noplanman
Copy link
Member Author

@jacklul Please take a look now. Have also made the whole return object process more bulletproof by remembering the current action, instead of trying to guess it by object fields.

@noplanman
Copy link
Member Author

noplanman commented Mar 10, 2019

@jacklul What do you think of extending this even further to all file-related methods and making it only necessary to pass the local file path, instead of Request::encodeFile(...) when sending a local file? Or rather, support both, and if a string is sent, then check if a local file exists or if it's a URL.

@noplanman noplanman requested a review from jacklul March 10, 2019 13:33
@jacklul
Copy link
Collaborator

jacklul commented Mar 11, 2019

@noplanman That sounds good

@noplanman
Copy link
Member Author

@jacklul Check out the latest commit, basically allows absolute paths to be passed as strings for all available InputFile fields 🎉

Request::sendPhoto([
   ...
   'photo' => '/path/to/photo.jpg',
   ...
]);

@noplanman noplanman changed the title WIP Helper for sending InputMedia objects Helper for sending InputMedia objects Mar 23, 2019
@noplanman noplanman merged commit a87a9e6 into php-telegram-bot:develop Mar 23, 2019
@noplanman noplanman deleted the 811-helper_for_media_groups branch March 23, 2019 22:43
@aaamina
Copy link

aaamina commented Jul 2, 2019

Fatal error: Class Longman\TelegramBot\Entities\InputMedia\InputMediaPhoto contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Longman\TelegramBot\Entities\InputMedia\InputMedia::getType, Longman\TelegramBot\Entities\InputMedia\InputMedia::getMedia, Longman\TelegramBot\Entities\InputMedia\InputMedia::setMedia) in G:\project\divar_bot\vendor\longman\telegram-bot\src\Entities\InputMedia\InputMediaPhoto.php on line 37

after new changes, I keep getting this error

@noplanman
Copy link
Member Author

@aaamina Thanks for reporting this! I'm working on a fix right now 👍

(Next time, please open a new issue, makes it easier to find in the future, thanks 😃)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants