Java how to send multiple images to telegram bot

38 Views Asked by At

How to send multiple images to Telegram bot?

I have a method that allows you to submit a single image and add a caption to that image.

        public void webhook(Update update) throws IOException {


            URL url = new URL("https://api.telegram.org/bot6143090937:AAHOpW0PNMK9qROsFqQ4DE-T1F_ox-IZ0dU/sendPhoto?chat_id="
                    + update.getMessage().getChatId() + "&caption=Message Message ");

            HttpsURLConnection httpConn = (HttpsURLConnection) url.openConnection();

            httpConn.setDoOutput(true);

            httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=boundary--");

            OutputStream outputStream = httpConn.getOutputStream();

            PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), true);

            new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);


            writer.append("--").append("boundary--").append("\r\n");
            writer.append("Content-Disposition: form-data; name=\"")
                    .append("photo")
                    .append("\"; filename=\"")
                    .append("image.jpg").append("\"")
                    .append("\r\n");
            writer.append("\r\n");
            writer.flush();

            File uploadFile1 = new File("C:\\img\\image.jpg");

            try (FileInputStream inputStream = new FileInputStream(uploadFile1)) {
                byte[] buffer = new byte[4096];
                int byteRead;
                while ((byteRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, byteRead);
                }

                outputStream.flush();
            }

            writer.append("\r\n").flush();
            writer.append("--").append("boundary--").append("--").append("\r\n");

            writer.flush();
            writer.close();

            httpConn.getResponseCode();


        }

But I don't know how to send more than one image, that is, a group of images.

Please show me how to send multiple images?

The telegram has the sendMediaGroup parameter https://core.telegram.org/bots/api#sendmediagroup I don't quite understand how to use it in conjunction with the code I showed above.

0

There are 0 best solutions below