java 使用MediaGroup发送多个图像

i34xakig  于 2023-01-29  发布在  Java
关注(0)|答案(2)|浏览(162)

我正在使用这个库:https://github.com/rubenlagus
我想在一条信息中发送多张图片,该如何操作?我知道SendMediaGroup,但似乎不了解它的工作原理。
例如,我有图像的URL,它可以使用单个SendPhoto方法,但我不能使用MediaGroup配置它。它需要InputMedia的列表。

public void handlePhoto(Update update) {
        String image1 = "https://lh3.googleusercontent.com/af9mFH4XinZ7f6dx-Ygm9molYPAcMHhhZyQ0udDBd9S9-44v_VBdeA0rjSlQyJRpQg=w1920-h937-rw";
        String image2 = "https://lh3.googleusercontent.com/mo0CZaV_aGflOPB8Tzo697l1WoZuoYUN9TiPMWq0zE29v_I99n1Qg185MfHrU-53nxAG=w1920-h937-rw";
        String image3 = "https://lh3.googleusercontent.com/FEiHmVyoT1MU3rbAxSkE_aNDuXBuo3YHQOnqfMAfehS-d4k6CvxuyxpX6KKSbJp3Xv28=w1920-h937-rw";
       
        List<InputMedia> media = new ArrayList<>();
        InputMedia photo1 = new InputMediaPhoto();
        photo1.setMedia(image1);
        InputMedia photo2 = new InputMediaPhoto();
        photo2.setMedia(image2);
        InputMedia photo3 = new InputMediaPhoto();
        photo3.setMedia(image3);
        media.add(photo1);
        media.add(photo2);
        media.add(photo3);
        SendMediaGroup mediaGroup = new SendMediaGroup();
        mediaGroup.setChatId(update.getMessage().getChatId());
        mediaGroup.setMedia(media);
        try {
            execute(mediaGroup);
        }
        catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }

我得到这个错误:
org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException: Error sending media group: [400] Bad Request: wrong type of the web page content
wrong type of the web page content意味着Telegram(或此API)无法处理. webp图像。

uidvcgyl

uidvcgyl1#

Telegram API支持.webp格式。您可能违反了以下规则之一:'照片大小最大为10 MB。照片的宽度和高度总计不得超过10000。宽高比最大为20。'

dwbf0jvd

dwbf0jvd2#

我使用这样的依赖关系:

<dependency>
    <groupId>org.telegram</groupId>
    <artifactId>telegrambots</artifactId>
    <version>6.3.0</version>
</dependency>
<dependency>
    <groupId>org.telegram</groupId>
    <artifactId>telegrambotsextensions</artifactId>
    <version>6.3.0</version>
</dependency>

SendMediaGroup只能发送2-10件物品,所以我们需要处理这个案例,我使用的代码是:

public void sendMultiplePhotoMessage(ChatId chatId, List<UserContent> contents) {

        if (CollectionUtils.isEmpty(contents)) {
            return;
        }

        int contentSize = contents.size();

        if (contentSize == 1) {
            this.sendPhotoMessage(chatId, contents.get(0));
        } else if (contentSize >= 2 && contentSize <= 10){
            this.sendMediaGroup(chatId, contents);
        } else {
            throw new NotImplementedException("Can't proceed more than 10 photos");
        }
    }

如果只有1张照片,我们将使用SendPhoto发送:

public void sendPhotoMessage(ChatId chatId, UserContent content) {
        String caption = getCaption(content);

        SendPhoto sendPhoto = SendPhoto.builder()
            .chatId(chatId.getId())
            .photo(new InputFile(new File(content.getMediaUrl())))
            .caption(caption)
            .parseMode(ParseMode.HTML)
            .build();

        try {
            chronologyBot.execute(sendPhoto);
        } catch (TelegramApiException e) {
            log.error("Can't send photo message", e);
        }
    }

如果我们有2-10张照片,用SendMediaGroup发送。在下面的代码中,我们正在从服务器加载文件。

private void sendMediaGroup(ChatId chatId, List<UserContent> contents) {
        List<InputMedia> medias = contents.stream()
            .map(userContent -> {

                String mediaName = UUID.randomUUID().toString();
                String caption = getCaption(userContent);

                InputMedia inputMedia = InputMediaPhoto.builder()
                    .media("attach://" + mediaName)
                    .mediaName(mediaName)
                    .isNewMedia(true)
                    .newMediaFile(new File(userContent.getMediaUrl()))
                    .caption(caption)
                    .parseMode(ParseMode.HTML)
                    .build();

                return inputMedia;
            }).collect(Collectors.toList());

            SendMediaGroup sendMediaGroup = SendMediaGroup.builder()
                .chatId(chatId.getId())
                .medias(medias)
                .build();

        try {
            chronologyBot.execute(sendMediaGroup);
        } catch (TelegramApiException e) {
            log.error("Can't send photos with media group", e);
        }
    }

相关问题