Through postman, I can send a txt file in telegram.
Here is the code:
But, if I try to send via b4j I get the error ResponseError. Reason: Unsupported Media Type, Response:
How can I send a telegram file using OkHttp?
I send the file like this:
I get the error: ResponseError. Reason: Bad Request, Response: {"ok":false,"error_code":400,"description":"Bad Request: there is no document in the request"}
Here is the code:
PHP:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.telegram.org/botXXX:YYY/sendDocument?chat_id=ZZZ',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('document'=> new CURLFILE('/С:/example.txt')),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Java:
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("document","/C:/example.txt",
RequestBody.create(MediaType.parse("application/octet-stream"),
new File("/C:/example.txt")))
.build();
Request request = new Request.Builder()
.url("https://api.telegram.org/botXXX:YYY/sendDocument?chat_id=ZZZ")
.method("POST", body)
.build();
Response response = client.newCall(request).execute();
B4X:
Dim jjj As HttpJob
jjj.Initialize("", Me)
Dim fd As MultipartFileData
fd.Initialize
fd.KeyName = "document"
fd.Dir = Dir
fd.FileName = Name
jjj.PostMultipart("https://api.telegram.org/botXXX:YYY/sendDocument?chat_id=ZZZ", CreateMap("document": Name), Array(fd))
Wait For (jjj) JobDone(jjj As HttpJob)
If jjj.Success Then
Log(jjj.GetString)
End If
jjj.Release
I send the file like this:
B4X:
jjj.PostString("https://api.telegram.org/botXXX:YYY/sendDocument?chat_id=ZZZ","document: "&Dir&Name)