Claus Ibsen created CAMEL-26024:
-----------------------------------
Summary: camel-telegram - media from a byte array only, TEXT bytes
sent as a document, and answers with ok=false
Key: CAMEL-26024
URL: https://issues.apache.org/jira/browse/CAMEL-26024
Project: Camel
Issue Type: Bug
Components: camel-telegram
Affects Versions: 4.18.0, 4.22.0, 4.23.0
Reporter: Claus Ibsen
Fix For: 4.24.0
Found while checking the body types of the telegram component for the catalog
(CAMEL-24961).
h4. 1. A byte array with the media type TEXT is sent as a document
A byte array with {{CamelTelegramMediaType=TEXT}} is sent as a document named
"file": TEXT falls into the DOCUMENT branch of the switch
({{TelegramConverter}}, main 07d2e7a987bc, lines 164-165).
Checked on 4.24.0-SNAPSHOT with {{camel run}} and a fake Bot API: the request
went to {{sendDocument}} as multipart.
*Proposed change*: send the bytes as a text message, decoded in the charset of
the exchange.
h4. 2. A stream or a file cannot be sent as media
Only a byte array can be sent as media. Any other body goes through the
converter from Object, which makes it a String first ({{TelegramConverter}},
main 07d2e7a987bc, line 57), so a stream, a file or a stream cache with
{{CamelTelegramMediaType=PHOTO_PNG}} fails with "Unsupported conversion from
String to media type PHOTO_PNG" (line 93).
Checked on 4.24.0-SNAPSHOT with {{camel run}}: a ByteArrayInputStream body with
PHOTO_PNG failed with a TypeConversionException caused by that
IllegalArgumentException; the same content as a byte array was sent with
{{sendPhoto}}.
*Proposed change*: read a stream or a file as bytes when the media type header
names a media type other than TEXT.
h4. 3. A message without text is sent back as the toString() of the
IncomingMessage
For a message without text, such as a photo, the conversion of an
{{IncomingMessage}} to a String gives null ({{TelegramConverter.toString}},
main 07d2e7a987bc, line 49), so Camel falls back to its {{toString()}}. A route
that sends a received message back, as in the chat bot example of the docs,
then sends that Java text to the chat.
Checked on 4.24.0-SNAPSHOT with {{camel run}} and a fake Bot API, with {{from:
telegram:bots}} and {{to: telegram:bots}}: the photo update was sent back as
the text {{IncomingMessage{messageId=2, date=..., text='null', ...
photo=[IncomingPhotoSize{fileId='f1', ...}]...}}}.
*Proposed change*: send nothing for a message without text (as for a null
body), or fail with an error that says the message has no text.
h4. 4. An answer with ok=false is not an error
An answer with HTTP 200 and {{"ok":false}} is not an error: the body becomes a
{{MessageResult}} with {{ok=false}} and no message, and the exchange completes
({{TelegramAsyncHandler}}, main 07d2e7a987bc, lines 99-108 check the HTTP
status only).
Checked on 4.24.0-SNAPSHOT with {{camel run}} and a fake {{sendLocation}} that
answered {{{"ok":false,"description":"Bad Request: wrong latitude"}}}: the body
was {{MessageResult{ok=false, message=null}}} and no exception was raised.
*Proposed change*: fail the exchange with the description of the answer when
{{ok}} is false.
h4. Also read in the code, not run
* The request body is limited to {{bufferSize}}, 1 MB by default, in a
ByteBuffer of that size ({{TelegramBodyPublisher}}, lines 127-134), so larger
media fail with a BufferOverflowException, and every send allocates the whole
buffer.
* The multipart parts declare UTF-8, but {{TelegramBodyPublisher}} writes them
with {{getBytes()}} in the platform charset (line 57 and others).
_Claude Code on behalf of davsclaus_
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)