hertzbeat icon indicating copy to clipboard operation
hertzbeat copied to clipboard

[Question] Sending messages to Telegram groups/channels

Open ToropovAV opened this issue 1 year ago • 14 comments

Question

Will it be realized to send messages to Telegram channels/groups and specific topics of Telegram groups, as for example it is realized in Grafana?. The current functionality of sending messages to one specific user is obviously less demanded than sending to a Telegram group

Image

ToropovAV avatar Feb 14 '25 07:02 ToropovAV

Hi, I believe Telegram uses the same type of ID for individuals, groups, and channels. So, from the sender's POV, the process for sending a message is the same for all three. In Telegram parlance, it's typically called the "chat ID" or "peer ID."

The peer id is a unique 64-bit ID used to identify a specific user, chat or channel.

https://core.telegram.org/api/peers#peer-id

jgafnea avatar Feb 14 '25 13:02 jgafnea

Image

It seems has supported.

thefullgc avatar Feb 15 '25 05:02 thefullgc

I apologize, I was trying to post to a closed thread, so I assumed that posting was only available on USER_ID. Checked again, yes, sending does work to both channels and Telegram groups, but it is still not available to send to specific Telegram topics. I suggest adding the "message_thread_id" option to allow sending messages to a specific Telegram group topic.

ToropovAV avatar Feb 17 '25 06:02 ToropovAV

@tomsun28

Hello, I believe I can handle this task effectively. Would you be able to assign it to me?

After reviewing the code, I propose the following modifications to support this feature:

Backend:

  • Add a messageThreadId property to the TelegramBotNotifyDTO class

Frontend:

  • Add an optional input field for entering the message_thread_id

bigcyy avatar Mar 12 '25 06:03 bigcyy

I've completed the code implementation, but I'm encountering an issue during testing.

According to the Telegram Bot API documentation:

Unique identifier for the target message thread (topic) of the forum; for forum supergroups only

After researching, I discovered that message threads (topics) are only supported in supergroups, which requires a group with more than 200 members to be converted to a supergroup format.

Would anyone be willing to help me test this functionality? I have a Telegram bot ready for testing. Perhaps you could add me to a group with topics enabled so I can conduct my tests. Alternatively, if you have any other suggestions for testing this feature, I'd appreciate your input.

bigcyy avatar Mar 12 '25 08:03 bigcyy

Hi. No problem, I can add your bot to my test supergroup, please send me the nickname.

ToropovAV avatar Mar 12 '25 12:03 ToropovAV

Hi. No problem, I can add your bot to my test supergroup, please send me the nickname.

Thank you! My bot's username is @cy_demo123_bot. To function properly in your supergroup, it needs permission to send messages.

Next, I’ll first search on Google to find out how to obtain the message_thread_id. If you already know how to get it and could share the information with me, I would really appreciate it!

bigcyy avatar Mar 12 '25 12:03 bigcyy

Bot added

supergroup ID is: -1002352333424

and two different topics: message_thread_id=2 message_thread_id=4

ToropovAV avatar Mar 12 '25 12:03 ToropovAV

Example of sending curl --location 'https://api.telegram.org/<bot_token>/sendMessage'
--header 'Content-Type: application/x-www-form-urlencoded'
--data-urlencode 'chat_id=-1002352333424'
--data-urlencode 'text=test'
--data-urlencode 'message_thread_id=2'

ToropovAV avatar Mar 12 '25 13:03 ToropovAV

Messages came first in message_thread_id=2 then message_thread_id=4 in turn

ToropovAV avatar Mar 12 '25 13:03 ToropovAV

Also the message came in the General topic, probably when message_thread_id was not set

ToropovAV avatar Mar 12 '25 13:03 ToropovAV

Messages came first in message_thread_id=2 then message_thread_id=4 in turn

Thank you! 😊

bigcyy avatar Mar 12 '25 13:03 bigcyy

Thank you for that)

ToropovAV avatar Mar 12 '25 13:03 ToropovAV

I’ll be submitting a PR soon.

bigcyy avatar Mar 12 '25 13:03 bigcyy