user2657708
user2657708

Reputation: 21

How to upload a file in Telegram Bott api using Http client?

I have tried to send files using multipart form data, but all I get is the following error:

{"description":"Error: Bad Request: chat_id is empty","error_code":400,"ok":false}

Here is my code snippet. Can anyone help me out where I am committing an error?

public HttpResponse<jsonnode> sendDocument(Integer chat_id,File f1) throws UnirestException {
  return Unirest.post(endpoint + token + "/sendDocument")
  header("accept", "application/json")
 .field("chat_id", chat_id)
 .field("document", f1)
 .asJson();
}

Upvotes: 2

Views: 2653

Answers (1)

alixrx
alixrx

Reputation: 211

well, it said chat_id is empty. that's an obvious error! but for your question, there is only two ways to sendDocument to telegram.

  1. that file is already is in their servers, so you should just pass the file_id in "document" field
  2. you want to upload a file from your device and as they said

Must be posted using multipart/form-data in the usual way that files are uploaded via the browser

if you are doing the upload section correct, then just make sure chat_id is not empty.

Upvotes: 0

Related Questions