java How to send multiple images to a chat using a telegram bot

170 Views Asked by At

How to send more than one image to a chat using a telegram bot in Java?

I have a method that sends one image, but I don't know how to send multiple images in a group. Tell me how to do it?

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

            OutputStream outputStream = httpConn.getOutputStream();

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

            new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);


            writer.append("--").append("1234567").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("1234567").append("--").append("\r\n");

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

            httpConn.getResponseCode();


        }

I know that Telegram has a sendMediaGroup parameter, but I don't quite understand how to use it. I tried to find a working example on the web, but I didn't find such an example. Can you please show me how to send a group of images?

0

There are 0 best solutions below