This is an automated email from the ASF dual-hosted git repository. zhfeng pushed a commit to branch camel-3.18.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.18.x by this push: new 937c60f7435 CAMEL-18049 Fix autoregister when using Telegram webhook (#8178) 937c60f7435 is described below commit 937c60f7435961bf869c9bb4d141f1abd120a3b2 Author: Zineb BENDHIBA <bendhiba.zi...@gmail.com> AuthorDate: Thu Aug 18 04:26:51 2022 +0200 CAMEL-18049 Fix autoregister when using Telegram webhook (#8178) --- .../component/telegram/model/WebhookInfo.java | 78 ---------------------- .../service/TelegramServiceRestBotAPIAdapter.java | 12 +--- .../telegram/TelegramWebhookRegistrationTest.java | 26 +++----- 3 files changed, 13 insertions(+), 103 deletions(-) diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/WebhookInfo.java b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/WebhookInfo.java deleted file mode 100644 index 86de58273fd..00000000000 --- a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/model/WebhookInfo.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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; - -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonProperty; - -/** - * Contains information about a webhook configuration. - */ -@JsonIgnoreProperties(ignoreUnknown = true) -public class WebhookInfo { - - @JsonProperty("url") - private String url; - - @JsonProperty("max_connections") - private Integer maxConnections; - - @JsonProperty("allowed_updates") - private List<String> allowedUpdates; - - public WebhookInfo() { - } - - public WebhookInfo(String url) { - this.url = url; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public Integer getMaxConnections() { - return maxConnections; - } - - public void setMaxConnections(Integer maxConnections) { - this.maxConnections = maxConnections; - } - - public List<String> getAllowedUpdates() { - return allowedUpdates; - } - - public void setAllowedUpdates(List<String> allowedUpdates) { - this.allowedUpdates = allowedUpdates; - } - - @Override - public String toString() { - return "WebhookInfo{" - + "url='" + url + '\'' - + ", maxConnections=" + maxConnections - + ", allowedUpdates=" + allowedUpdates - + '}'; - } -} diff --git a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/service/TelegramServiceRestBotAPIAdapter.java b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/service/TelegramServiceRestBotAPIAdapter.java index e70c39d10ed..4a1934e1f3f 100644 --- a/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/service/TelegramServiceRestBotAPIAdapter.java +++ b/components/camel-telegram/src/main/java/org/apache/camel/component/telegram/service/TelegramServiceRestBotAPIAdapter.java @@ -60,7 +60,6 @@ import org.apache.camel.component.telegram.model.SendLocationMessage; import org.apache.camel.component.telegram.model.SendVenueMessage; import org.apache.camel.component.telegram.model.StopMessageLiveLocationMessage; import org.apache.camel.component.telegram.model.UpdateResult; -import org.apache.camel.component.telegram.model.WebhookInfo; import org.apache.camel.component.telegram.model.WebhookResult; import org.apache.camel.support.GZIPHelper; import org.apache.camel.util.IOHelper; @@ -182,16 +181,9 @@ public class TelegramServiceRestBotAPIAdapter implements TelegramService { @Override public boolean setWebhook(String url) { - final String uri = baseUri + "/setWebhook"; - final RequestBuilder request = new RequestBuilder("POST") + final String uri = baseUri + "/setWebhook?url=" + url; + final RequestBuilder request = new RequestBuilder("GET") .setUrl(uri); - final WebhookInfo message = new WebhookInfo(url); - try { - final String body = mapper.writeValueAsString(message); - request.setBody(body); - } catch (JsonProcessingException e) { - throw new RuntimeCamelException("Could not serialize " + message); - } WebhookResult res = sendSyncRequest(request.build(), WebhookResult.class); return res.isOk() && res.isResult(); } diff --git a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramWebhookRegistrationTest.java b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramWebhookRegistrationTest.java index f5223ea3193..9c4a2a5cbb7 100644 --- a/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramWebhookRegistrationTest.java +++ b/components/camel-telegram/src/test/java/org/apache/camel/component/telegram/TelegramWebhookRegistrationTest.java @@ -21,7 +21,6 @@ import java.util.concurrent.TimeUnit; import org.apache.camel.ServiceStatus; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.telegram.model.WebhookInfo; import org.apache.camel.component.telegram.model.WebhookResult; import org.apache.camel.component.telegram.util.TelegramMockRoutes; import org.apache.camel.component.telegram.util.TelegramMockRoutes.MockProcessor; @@ -35,7 +34,6 @@ import org.junit.jupiter.api.Test; import static org.awaitility.Awaitility.waitAtMost; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; /** * Tests a producer that sends media information. @@ -44,7 +42,7 @@ public class TelegramWebhookRegistrationTest extends TelegramTestSupport { @Test public void testAutomaticRegistration() throws Exception { - final MockProcessor<WebhookInfo> mockProcessor = getMockRoutes().getMock("setWebhook"); + final MockProcessor<String> mockProcessor = getMockRoutes().getMock("setWebhook?url=http://my-domain.com/my-test"); mockProcessor.clearRecordedMessages(); try (final DefaultCamelContext mockContext = new DefaultCamelContext()) { mockContext.addRoutes(getMockRoutes()); @@ -64,29 +62,27 @@ public class TelegramWebhookRegistrationTest extends TelegramTestSupport { @Override public void configure() { from("direct:telegram").to("telegram:bots?authorizationToken=mock-token"); - from("webhook:telegram:bots?authorizationToken=mock-token").to("mock:endpoint"); + from("webhook:telegram:bots?authorizationToken=mock-token&webhookPath=/my-test&webhook-external-url=http://my-domain.com") + .to("mock:endpoint"); } }); context().start(); { - final List<WebhookInfo> recordedMessages = mockProcessor.awaitRecordedMessages(1, 5000); + final List<String> recordedMessages = mockProcessor.awaitRecordedMessages(1, 5000); assertEquals(1, recordedMessages.size()); - assertNotEquals("", recordedMessages.get(0).getUrl()); + assertEquals("", recordedMessages.get(0)); } mockProcessor.clearRecordedMessages(); + context().stop(); - { - final List<WebhookInfo> recordedMessages = mockProcessor.awaitRecordedMessages(1, 5000); - assertEquals(1, recordedMessages.size()); - assertEquals("", recordedMessages.get(0).getUrl()); - } + } } @Test public void testNoRegistration() throws Exception { - final MockProcessor<WebhookInfo> mockProcessor = getMockRoutes().getMock("setWebhook"); + final MockProcessor<String> mockProcessor = getMockRoutes().getMock("setWebhook?url=http://my-domain.com/my-test"); mockProcessor.clearRecordedMessages(); try (final DefaultCamelContext mockContext = new DefaultCamelContext()) { mockContext.addRoutes(getMockRoutes()); @@ -137,9 +133,9 @@ public class TelegramWebhookRegistrationTest extends TelegramTestSupport { String.class, "running") .addEndpoint( - "setWebhook", - "POST", - WebhookInfo.class, + "setWebhook?url=http://my-domain.com/my-test", + "GET", + String.class, TelegramTestUtil.serialize(result)); }