This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new a416554e3532 CAMEL-22816: Fix failure for messages without chatId when 
chatId is not configured (#20715)
a416554e3532 is described below

commit a416554e353292b846e277510f9828c6962a5e1f
Author: Kirill Byvshev <[email protected]>
AuthorDate: Thu Jan 8 14:19:15 2026 +0400

    CAMEL-22816: Fix failure for messages without chatId when chatId is not 
configured (#20715)
---
 .../camel/component/telegram/TelegramProducer.java |   3 +-
 .../component/telegram/model/NoChatIdRequired.java |  28 ++++++
 .../telegram/model/OutgoingAnswerInlineQuery.java  |   2 +-
 .../model/OutgoingCallbackQueryMessage.java        |   2 +-
 .../TelegramProducerNoChatIdRequiredTest.java      | 103 +++++++++++++++++++++
 5 files changed, 135 insertions(+), 3 deletions(-)

diff --git 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramProducer.java
 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramProducer.java
index 4fd0dc0fe476..de613f921067 100644
--- 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramProducer.java
+++ 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/TelegramProducer.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.telegram;
 
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.Exchange;
+import org.apache.camel.component.telegram.model.NoChatIdRequired;
 import org.apache.camel.component.telegram.model.OutgoingMessage;
 import org.apache.camel.support.DefaultAsyncProducer;
 import org.apache.camel.util.ObjectHelper;
@@ -66,7 +67,7 @@ public class TelegramProducer extends DefaultAsyncProducer {
     private boolean sendMessage(Exchange exchange, AsyncCallback callback, 
TelegramMessage message) {
         final TelegramService service = endpoint.getTelegramService();
 
-        if (message instanceof OutgoingMessage outgoingMessage) {
+        if (message instanceof OutgoingMessage outgoingMessage && !(message 
instanceof NoChatIdRequired)) {
             if (outgoingMessage.getChatId() == null) {
                 TelegramConfiguration config = endpoint.getConfiguration();
                 LOG.debug("Chat id is null on outgoing message, trying 
resolution");
diff --git 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/NoChatIdRequired.java
 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/NoChatIdRequired.java
new file mode 100644
index 000000000000..23b683f88788
--- /dev/null
+++ 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/NoChatIdRequired.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.component.telegram.model;
+
+/**
+ * Marker interface for {@link OutgoingMessage} subclasses that do not require 
a chat ID. Introduced for backward
+ * compatibility.
+ *
+ * @see OutgoingAnswerInlineQuery
+ * @see OutgoingCallbackQueryMessage
+ */
+public interface NoChatIdRequired {
+}
diff --git 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/OutgoingAnswerInlineQuery.java
 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/OutgoingAnswerInlineQuery.java
index 5040a822bb15..f506580d40dc 100644
--- 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/OutgoingAnswerInlineQuery.java
+++ 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/OutgoingAnswerInlineQuery.java
@@ -28,7 +28,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
  *      https://core.telegram.org/bots/api#answerinlinequery</a>
  */
 @JsonInclude(JsonInclude.Include.NON_NULL)
-public class OutgoingAnswerInlineQuery extends OutgoingMessage {
+public class OutgoingAnswerInlineQuery extends OutgoingMessage implements 
NoChatIdRequired {
 
     private static final long serialVersionUID = -1928788814068921178L;
 
diff --git 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/OutgoingCallbackQueryMessage.java
 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/OutgoingCallbackQueryMessage.java
index 043ae2df1863..299700da0a5a 100644
--- 
a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/OutgoingCallbackQueryMessage.java
+++ 
b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/OutgoingCallbackQueryMessage.java
@@ -27,7 +27,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
  *      https://core.telegram.org/bots/api#answercallbackquery</a>
  */
 @JsonInclude(JsonInclude.Include.NON_NULL)
-public class OutgoingCallbackQueryMessage extends OutgoingMessage {
+public class OutgoingCallbackQueryMessage extends OutgoingMessage implements 
NoChatIdRequired {
 
     @JsonProperty("callback_query_id")
     private String callbackQueryId;
diff --git 
a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerNoChatIdRequiredTest.java
 
b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerNoChatIdRequiredTest.java
new file mode 100644
index 000000000000..c307b4ba25e8
--- /dev/null
+++ 
b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramProducerNoChatIdRequiredTest.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.telegram;
+
+import java.util.Collections;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.telegram.model.NoChatIdRequired;
+import org.apache.camel.component.telegram.model.OutgoingAnswerInlineQuery;
+import org.apache.camel.component.telegram.model.OutgoingCallbackQueryMessage;
+import org.apache.camel.component.telegram.util.TelegramMockRoutes;
+import org.apache.camel.component.telegram.util.TelegramTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * Tests that {@link NoChatIdRequired} message types work without setting a 
chat ID.
+ */
+public class TelegramProducerNoChatIdRequiredTest extends TelegramTestSupport {
+
+    @EndpointInject("direct:telegram")
+    private Endpoint endpoint;
+
+    @Test
+    public void testOutgoingAnswerInlineQueryWithoutChatId() {
+        OutgoingAnswerInlineQuery message = OutgoingAnswerInlineQuery.builder()
+                .inlineQueryId("test-inline-query-id")
+                .results(Collections.emptyList())
+                .build();
+
+        // The message should not have a chatId set
+        assertNull(message.getChatId(), "ChatId should not be set");
+
+        // This should work without throwing an exception since 
answerInlineQuery
+        // does not require a chat_id according to Telegram API
+        assertDoesNotThrow(() -> template.sendBody(endpoint, message),
+                "OutgoingAnswerInlineQuery should not require a chatId");
+    }
+
+    @Test
+    public void testOutgoingCallbackQueryMessageWithoutChatId() {
+        OutgoingCallbackQueryMessage message = new 
OutgoingCallbackQueryMessage();
+        message.setCallbackQueryId("test-callback-query-id");
+        message.setText("Callback response");
+
+        // The message should not have a chatId set
+        assertNull(message.getChatId(), "ChatId should not be set");
+
+        // This should work without throwing an exception since 
answerCallbackQuery
+        // does not require a chat_id according to Telegram API
+        assertDoesNotThrow(() -> template.sendBody(endpoint, message),
+                "OutgoingCallbackQueryMessage should not require a chatId");
+    }
+
+    @Override
+    protected RoutesBuilder[] createRouteBuilders() {
+        return new RoutesBuilder[] {
+                getMockRoutes(),
+                new RouteBuilder() {
+                    @Override
+                    public void configure() {
+                        from("direct:telegram")
+                                
.to("telegram:bots?authorizationToken=mock-token");
+                    }
+                } };
+    }
+
+    @Override
+    protected TelegramMockRoutes createMockRoutes() {
+        // Response for both answerInlineQuery and answerCallbackQuery is just 
{"ok": true, "result": true}
+        String successResponse = "{\"ok\": true, \"result\": true}";
+        return new TelegramMockRoutes(port)
+                .addEndpoint(
+                        "answerInlineQuery",
+                        "POST",
+                        OutgoingAnswerInlineQuery.class,
+                        successResponse)
+                .addEndpoint(
+                        "answerCallbackQuery",
+                        "POST",
+                        OutgoingCallbackQueryMessage.class,
+                        successResponse);
+    }
+}

Reply via email to