Skip to content
Open
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 @@ -47,6 +47,11 @@ public class TelegramNotifier extends AbstractContentNotifier {
*/
@Nullable private String chatId;

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mark it with @Nullable for consistency.


/**
* The token identifying und authorizing your Telegram bot (e.g.
* `123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11`)
Expand Down Expand Up @@ -75,14 +80,17 @@ protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
.fromRunnable(() -> restTemplate.getForObject(buildUrl(), Void.class, createMessage(event, 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);
}
protected String buildUrl() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing tests verify exact URL strings and parameter map contents. Adding messageThreadId will change behavior that the tests should cover. Two test cases are needed:

  • When messageThreadId is set: URL includes &message_thread_id=... and parameter map contains the value
  • When messageThreadId is NOT set (default): URL does NOT include message_thread_id and parameter map does NOT contain it (backward compatibility)

return String.format(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only include the parameter when messageThreadId != null.

Otherwise the message_thread_id parameter is always appended to the URL. You could pass null to the Telegram API (undefined behavior, likely a 400 error), or cause an NPE if Spring's UriComponentsBuilder tries to resolve {message_thread_id} with a null map value.

"%s/bot%s/sendmessage?chat_id={chat_id}&message_thread_id={message_thread_id}&text={text}&parse_mode={parse_mode}&disable_notification={disable_notification}",
this.apiUrl, this.authToken
);
}

private Map<String, Object> createMessage(InstanceEvent event, Instance instance) {
Map<String, Object> parameters = new HashMap<>();
parameters.put("chat_id", this.chatId);
parameters.put("message_thread_id", this.messageThreadId);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parameters.put("message_thread_id", this.messageThreadId);
if (this.messageThreadId != null) {
parameters.put("message_thread_id", this.messageThreadId);
}

This matches the Telegram Bot API specification where message_thread_id is explicitly documented as optional.

parameters.put("parse_mode", this.parseMode);
parameters.put("disable_notification", this.disableNotify);
parameters.put("text", createContent(event, instance));
Expand Down Expand Up @@ -114,6 +122,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