skip to Main Content

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 = "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();
        }
    }

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?

2

Answers


  1. 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.’

    Login or Signup to reply.
  2. I’m using such dependencies:

    <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 can only send 2-10 items. So we need to process this case. I’m using this code:

    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");
            }
        }
    

    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("Can't send photo message", e);
            }
        }
    

    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<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);
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search