2

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)

Kirill Belov
  • 618
  • 6
  • 17

0 Answers0