0

There's a method in Telegram API called sendAnimation. There are two obligatory paramteres: chat_id and animation. animation's descriptio is this:

Type: InputFile or String

Description: Animation to send. Pass a file_id as String to send an animation that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an animation from the Internet, or upload a new animation using multipart/form-data. More info on Sending Files »

I have a local .gif file that I want to send. So it looks like I need to use that multipart/form-data method. I don't understand what that method is. I checked out the InputFile type's description:

InputFile This object represents the contents of a file to be uploaded. Must be posted using multipart/form-data in the usual way that files are uploaded via the browser.

Again, they write about that multipart/form-data thing, but don't write what exactly that is.

I thought maybe I could upload a file using sendDocument method, but the uploaded document must be of type InputFile as well.

How do I make the InputFile object out of my local .gif? I can convert it to Java's InputStream, but that's about it.

1 Answer 1

1

simply multipart/form-data is just an encryption type for the sent data there are three types of encryption in forms:

  • application/x-www-form-urlencoded (the default)
  • multipart/form-data
  • text/plain

for more info about multipart/form-data check this link

I don't know what is the type of your GIF object in java but let us consider it a binary file then you would simply post this text as follows using POST request:

String url = "uploading url";
String charset = "UTF-8";
String param = "value";
File binaryFile = new File("/path/to/file.bin");
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.

URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

try {
    OutputStream output = connection.getOutputStream();
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
     // Send binary file.
    writer.append("--" + boundary).append(CRLF);
    writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
    writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
    writer.append("Content-Transfer-Encoding: binary").append(CRLF);
    writer.append(CRLF).flush();
    Files.copy(binaryFile.toPath(), output);
    output.flush(); // Important before continuing with writer!
    writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

    // End of multipart/form-data.
    writer.append("--" + boundary + "--").append(CRLF).flush();
}

// Request is lazily fired whenever you need to obtain information about response.
int responseCode = ((HttpURLConnection) connection).getResponseCode();
System.out.println(responseCode); // Should be 200
Sign up to request clarification or add additional context in comments.

2 Comments

Hello! Thank you for the reply. I'm not sure though to which address to send the gif. Meaning what should be the value of the url variable. Some telegram server? But which one?
I figured that bit out. The address is ...sendAnimation. I've found a shorter version though: stackoverflow.com/questions/29140570/…. Is your version faster or do you think it's OK to use the other one?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.