发送多张图片,使用MediaGroup功能。

huangapple go评论77阅读模式
英文:

Send multiple images with MediaGroup

问题

我正在使用这个库: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 格式的图片。有什么解决办法吗?

英文:

I'm using this library: https://github.com/rubenlagus

I want to send multiple images in one message. How can I do it? I know about SendMediaGroup, but seems like I don't understand how it works.

For example, I've URL of the image, and it works with single SendPhoto method, but I can't configure it with MediaGroup. It requires List of InputMedia.

public void handlePhoto(Update update) {
        String image1 = &quot;https://lh3.googleusercontent.com/af9mFH4XinZ7f6dx-Ygm9molYPAcMHhhZyQ0udDBd9S9-44v_VBdeA0rjSlQyJRpQg=w1920-h937-rw&quot;;
        String image2 = &quot;https://lh3.googleusercontent.com/mo0CZaV_aGflOPB8Tzo697l1WoZuoYUN9TiPMWq0zE29v_I99n1Qg185MfHrU-53nxAG=w1920-h937-rw&quot;;
        String image3 = &quot;https://lh3.googleusercontent.com/FEiHmVyoT1MU3rbAxSkE_aNDuXBuo3YHQOnqfMAfehS-d4k6CvxuyxpX6KKSbJp3Xv28=w1920-h937-rw&quot;;
       
        List&lt;InputMedia&gt; media = new ArrayList&lt;&gt;();
        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();
        }
    }

I'm getting this error:

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 means that Telegram (or this API) can't handle .webp images. Any tricks?

答案1

得分: 1

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

英文:

Telegram API supports .webp format. Maybe you violated one of these rules:
`The photo must be at most 10 MB in size. The photo's width and height must not exceed 10000 in total. Width and height ratio must be at most 20.'

答案2

得分: 0

以下是翻译后的内容:

我正在使用以下依赖项:

    <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("无法发送超过 10 张照片");
    }
}

如果只有 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("无法发送照片消息", 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("无法使用媒体组发送照片", e);
    }
}
英文:

I'm using such dependencies:

&lt;dependency&gt;
	&lt;groupId&gt;org.telegram&lt;/groupId&gt;
	&lt;artifactId&gt;telegrambots&lt;/artifactId&gt;
	&lt;version&gt;6.3.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
	&lt;groupId&gt;org.telegram&lt;/groupId&gt;
	&lt;artifactId&gt;telegrambotsextensions&lt;/artifactId&gt;
	&lt;version&gt;6.3.0&lt;/version&gt;
&lt;/dependency&gt;

<br>SendMediaGroup can only send 2-10 items. So we need to process this case. I'm using this code:

public void sendMultiplePhotoMessage(ChatId chatId, List&lt;UserContent&gt; contents) {

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

        int contentSize = contents.size();

        if (contentSize == 1) {
            this.sendPhotoMessage(chatId, contents.get(0));
        } else if (contentSize &gt;= 2 &amp;&amp; contentSize &lt;= 10){
            this.sendMediaGroup(chatId, contents);
        } else {
            throw new NotImplementedException(&quot;Can&#39;t proceed more than 10 photos&quot;);
        }
    }

<br>If only 1 photo, we will send it with 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(&quot;Can&#39;t send photo message&quot;, e);
        }
    }

<br>If we have 2-10 photos, send them with SendMediaGroup. In code below we are loading files from our server.

private void sendMediaGroup(ChatId chatId, List&lt;UserContent&gt; contents) {
        List&lt;InputMedia&gt; medias = contents.stream()
            .map(userContent -&gt; {

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

                InputMedia inputMedia = InputMediaPhoto.builder()
                    .media(&quot;attach://&quot; + 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(&quot;Can&#39;t send photos with media group&quot;, e);
        }
    }

huangapple
  • 本文由 发表于 2020年8月28日 21:43:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63635009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定