Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.jspecify.annotations.Nullable;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

import de.codecentric.boot.admin.server.domain.entities.Instance;
Expand All @@ -47,6 +48,11 @@ public class TelegramNotifier extends AbstractContentNotifier {
*/
@Nullable private String chatId;

/**
* Unique identifier for the target topic of the target super group
*/
@Nullable private Integer messageThreadId;

/**
* The token identifying und authorizing your Telegram bot (e.g.
* `123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11`)
Expand Down Expand Up @@ -76,13 +82,26 @@ protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
}

protected String buildUrl() {
return String.format("%s/bot%s/sendmessage?chat_id={chat_id}&text={text}&parse_mode={parse_mode}"
+ "&disable_notification={disable_notification}", this.apiUrl, this.authToken);
UriComponentsBuilder builder = UriComponentsBuilder
.fromUriString(this.apiUrl + "/bot" + this.authToken + "/sendmessage")
.queryParam("chat_id", "{chat_id}")
.queryParam("text", "{text}")
.queryParam("parse_mode", "{parse_mode}")
.queryParam("disable_notification", "{disable_notification}");

if (this.messageThreadId != null) {
builder.queryParam("message_thread_id", "{message_thread_id}");
}

return builder.build().toUriString();
}

private Map<String, Object> createMessage(InstanceEvent event, Instance instance) {
Map<String, Object> parameters = new HashMap<>();
parameters.put("chat_id", this.chatId);
if (this.messageThreadId != null) {
parameters.put("message_thread_id", this.messageThreadId);
}
parameters.put("parse_mode", this.parseMode);
parameters.put("disable_notification", this.disableNotify);
parameters.put("text", createContent(event, instance));
Expand Down Expand Up @@ -114,6 +133,14 @@ public void setChatId(@Nullable String chatId) {
this.chatId = chatId;
}

public Integer getMessageThreadId() {
return messageThreadId;
}

public void setMessageThreadId(Integer messageThreadId) {
this.messageThreadId = messageThreadId;
}

@Nullable public String getAuthToken() {
return authToken;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ void test_onApplicationEvent_trigger() {
Void.class, getParameters("DOWN"));
}

@Test
void test_includes_messageThreadId_in_url_when_set() {
notifier.setMessageThreadId(1337);

StepVerifier
.create(notifier
.notify(new InstanceStatusChangedEvent(instance.getId(), instance.getVersion(), StatusInfo.ofDown())))
.verifyComplete();

Map<String, Object> parameters = getParameters("DOWN");
parameters.put("message_thread_id", 1337);

verify(restTemplate).getForObject("https://telegram.com/bot--token-/sendmessage?chat_id={chat_id}&text={text}"
+ "&parse_mode={parse_mode}&disable_notification={disable_notification}&message_thread_id={message_thread_id}",
Void.class, parameters);
}

private Map<String, Object> getParameters(String status) {
Map<String, Object> parameters = new HashMap<>();
parameters.put("chat_id", "-room-");
Expand Down