Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supported reply_to_message_id for Telegram topics. #3560

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

VadimRezvov
Copy link

@VadimRezvov VadimRezvov commented Oct 16, 2023

This PR brings support to sending messages to specific Telegram topic,
see https://telegram.org/blog/topics-in-groups-collectible-usernames

Topic is identified by id of the first message in this topic.
In order to find out the id of the first message in a topic, in the Telegram desktop client,
right-click on the first message in the topic, select "Copy Message Link".
Get a link like:
https://t.me/c/1234567890/1234/1235
The second part of the link is the id of the first message in the topic:
1234

@VadimRezvov VadimRezvov force-pushed the feature/telegram_topic_support branch from 41a7817 to b95fc69 Compare October 16, 2023 21:49
@VadimRezvov
Copy link
Author

Dear maintainers, I supported this for my project, but I think that it will be useful for Telegram users.
If you have some comments, write me and I will definitely take them into account.

@alexunderboots
Copy link

+1

@VadimRezvov VadimRezvov force-pushed the feature/telegram_topic_support branch from 18ee4ae to fbca708 Compare November 5, 2023 17:49
@VadimRezvov
Copy link
Author

Make some changes as the result of review.
Replace reply_to_message_id with message_thread_id.

@asonnleitner
Copy link

Hi @gotjosh, just checking in on the status of this PR. Looking forward to any updates, as it seems quite useful. Thanks!

@iishabakaev
Copy link

@simonpasquier, @w0rm, @gotjosh
Hello friends, could you please review this PR?
Its will be very helpful for telegram users

@formorter
Copy link

hello friends it will be so good if you review this PR, i need send alerts to my telegram thread

@nicknamenotknown
Copy link

nicknamenotknown commented Dec 4, 2023

Hi all, this very good function, @simonpasquier, @w0rm. @gotjosh please review it.

@sbueringer
Copy link
Contributor

@nicknamenotknown I'm not a reviewer or maintainer in this repository

@nicknamenotknown
Copy link

@nicknamenotknown I'm not a reviewer or maintainer in this repository

Sorry, it's my mistake.

@asonnleitner
Copy link

@grobinson-grafana, why are your PRs getting reviewed and merged almost instantly, while this one has been languishing here for three months?

@iishabakaev
Copy link

@simonpasquier hello! I see your PRs was merged last time.
Can you help with this PR and communicate with repository maintainers, please?
This is a very small feature but very helpful

@Brukkil
Copy link

Brukkil commented Jan 21, 2024

+1

@Hitman95
Copy link

Hitman95 commented Feb 7, 2024

+1

@pavlozt
Copy link

pavlozt commented Mar 18, 2024

This change is trivial and will not affect anything. Why not accept it?

Grafana already have this feature grafana/alerting#143

@vrevkov
Copy link

vrevkov commented Mar 22, 2024

This MR adds very useful feature, please accept it. :)

@grobinson-grafana
Copy link
Contributor

@VadimRezvov as a non Telegram user, what is the difference between reply_to_message_id and message_thread_id? I read the Telegram Bot API docs, and I'm still not sure I understand the difference.

We also have this PR here that uses the same configuration option message_thread_id as the ThreadID, instead of as ReplyTo as done in this PR.

@TheArturthur
Copy link

TheArturthur commented Mar 23, 2024

@VadimRezvov what is the difference between reply_to_message_id and message_thread_id?

We also have this PR here that uses the same configuration option message_thread_id as the ThreadID, instead of as ReplyTo as done in this PR.

@grobinson-grafana, as user @raphielscape said in the PR you linked, when answering the same question:

If you mean #3560, this is not duplicate, message_thread_id inclusion is different compared to reply_to_message_id, even though the idea is the same (sending message to a certain topic), message_thread is the intended way to sending a message to a certain topic, reply_to_message, as the fields name says, are more targeted to replying to a certain message ID rather than sending messages.

So, though it serves the same purpose of sending a message to a telegram supergroup thread, it could also reply to a message on any chat, channel or group.

@grobinson-grafana
Copy link
Contributor

@VadimRezvov what is the difference between reply_to_message_id and message_thread_id?
We also have this PR here that uses the same configuration option message_thread_id as the ThreadID, instead of as ReplyTo as done in this PR.

@grobinson-grafana, as user @raphielscape said in the PR you linked, when answering the same question:

If you mean #3560, this is not duplicate, message_thread_id inclusion is different compared to reply_to_message_id, even though the idea is the same (sending message to a certain topic), message_thread is the intended way to sending a message to a certain topic, reply_to_message, as the fields name says, are more targeted to replying to a certain message ID rather than sending messages.

So, though it serves the same purpose of sending a message to a telegram supergroup thread, it could also reply to a message on any chat, channel or group.

OK. It sounds like there might be a use case for both? I think #3638 can be reviewed once the author has signed their commits (#3638 (comment)).

R.E this PR, the choice of message_thread_id to set the reply_to_message_id field seems incorrect, and it will also conflict with #3638. I think the author of this PR should update the PR to add a separate field just for reply_to_message_id and then we can review that too.

@VadimRezvov
Copy link
Author

VadimRezvov commented Mar 23, 2024

To be short the message_thread_id replies to the first message of Telegram group ONLY in supergroups, and the reply_to_message_id (now it is reply_to_message https://core.telegram.org/bots/api#message) replies to the certain message id in the chat for any group type, so that its more multipurpose.
So that, reply_to_message is more suitable for the purpose of sending message to certain Telegram topic.

@grobinson-grafana
Copy link
Contributor

OK! I think we need to coordinate the two PRs (this and #3638).

Have you also tested this change to make sure it works? The reason I ask is the version of the telebot package used in Alertmanager uses reply_to_message_id not reply_to_message:

if opt.ReplyTo != nil && opt.ReplyTo.ID != 0 {
	params["reply_to_message_id"] = strconv.Itoa(opt.ReplyTo.ID)
}

You might also need to update the telebot package in go.mod too if it doesn't work. I'll also set aside time next week to test it.

@VadimRezvov
Copy link
Author

Ok, I will take this task, and will be back with results in nearest time.

@raphielscape
Copy link

raphielscape commented Mar 24, 2024

To be short the message_thread_id replies to the first message of Telegram group ONLY in supergroups, and the reply_to_message_id (now it is reply_to_message https://core.telegram.org/bots/api#message) replies to the certain message id in the chat for any group type, so that its more multipurpose. So that, reply_to_message is more suitable for the purpose of sending message to certain Telegram topic.

Topics is only available for Supergroups, that's what the documentation conveys, you can't enable Topics in chat that are not supergroup to begin with, so message_thread_id is the proper way to send a message to a Topic as the intended MR is for replying to a Topic rather than message, and that message thread starter ID might be changed if the chat has gone beyond 1 million, so this might not be sustainable to use reply to message as the message it replied might be lost if you have a lot of messages on the chat

@grobinson-grafana
Copy link
Contributor

Hi! 👋 #3638 has been merged to main. I would appreciate it if you could test it and make sure it works as I cannot seem to create a Telegram supergroup or topics (perhaps I just don't know how to do it?).

@veselov-v
Copy link

veselov-v commented Aug 16, 2024

Привет! 👋 #3638 был объединен с основным. Я был бы признателен, если бы вы могли протестировать его и убедиться, что он работает, так как я не могу создать супергруппу или темы Telegram (возможно, я просто не знаю, как это сделать?).

Hi, grobinson-grafana! I'm new to GitHub, can you tell me if I can download the latest version of AlertManager and test this functionality?
I'd like to test the ability to send a message to a supergroup with message_thread_id.

@gotjosh
Copy link
Member

gotjosh commented Oct 22, 2024

Looks like you haven't enabled mantainers to be able to update PRs - I was trying to make a small change and merge this PR, but it seems like I can't.

I'll leave you with the diff and if you get it done by tomorrow, we'll be able to include it in the next release:

diff --git a/notify/telegram/telegram.go b/notify/telegram/telegram.go
index 20285bed..3db06a32 100644
--- a/notify/telegram/telegram.go
+++ b/notify/telegram/telegram.go
@@ -90,11 +90,16 @@ func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, err
 		return true, err
 	}
 
-	message, err := n.client.Send(telebot.ChatID(n.conf.ChatID), messageText, &telebot.SendOptions{
-		ReplyTo:               &telebot.Message{ID: int(n.conf.MessageThreadID)},
+	options := &telebot.SendOptions{
 		DisableNotification:   n.conf.DisableNotifications,
 		DisableWebPagePreview: true,
-	})
+	}
+
+	if n.conf.MessageThreadID != 0 {
+		options.ReplyTo = &telebot.Message{ID: int(n.conf.MessageThreadID)}
+	}
+
+	message, err := n.client.Send(telebot.ChatID(n.conf.ChatID), messageText, options)
 	if err != nil {
 		return true, err
 	}

@gotjosh gotjosh mentioned this pull request Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.