2
$\begingroup$

I was create telegram-bot using Mathematica. All the possibilities of telegram api can be found here. To send images, you can use method sendPhoto. To upload image from hard disk or memory you can use multipart/form-data. But I do not get to send an image this way:

data := ExportString[Plot[Sin[x], {x, 1, 3}], "JPEG"]

chatID := $chatID (* allowed chat id *)

token := $token (* https://core.telegram.org/bots/api#authorizing-your-bot *)

body := StringTemplate["
--Asrf456BGe4h
Content-Disposition: form-data; name=\"chat_id\"

`chat_id`
--Asrf456BGe4h
Content-Disposition: form-data; name=\"photo\";filename=\"photo.jpg\" 
Content-Type: image/jpeg

`data`
--Asrf456BGe4h
"]

length := StringLength[body]

url:=StringTemplate["https://api.telegram.org/bot`token`/sendPhoto"][<|"token"->token|>]

request := HTTPRequest[url, <|
    Method -> "POST", 
    "Headers" -> {"Content-Type" -> "multipart/form-data; boundary=Asrf456BGe4h", 
        "Content-Length" -> ToString@length, 
        "Connection" -> "keep-alive", "Keep-Alive" -> "300"}, 
    "Body" -> body[<|"chat_id" -> chatID, "data" -> data|>]|>]

URLRead[request]

(* Out[..] := HTTPResponse[<|StatusCode -> 504, ...|>] *)

How to correctly compose a query to load an image using multipart/form-data (ru)

$\endgroup$
2
  • $\begingroup$ I think the multipart is the core of your problem, so this might help: mathematica.stackexchange.com/q/147526/38205 $\endgroup$
    – b3m2a1
    Commented Dec 16, 2017 at 21:36
  • $\begingroup$ If you ask this on Community i might be able to attract attention to it and possibly get some answers. $\endgroup$ Commented Dec 18, 2017 at 20:41

0

Your Answer

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

Browse other questions tagged or ask your own question.