How to get topic id for telegram group chat?
Asked Answered
A

2

12

Recently Telegram added support for Topics in Groups in Bot API version 6.3 and this support added into python-telegram-bot version 13.15 (please find changelog https://docs.python-telegram-bot.org/en/stable/changelog.html)

It's not clear how to get a Topic ID (message_thread_id) for topics in Grpups where topics enabeled.

Any suggestion?

I'm trying to find a way how to get topic id for telegram group chats. Please be aware that I talking about message_thread_id, please do not confuse with chat_id.

Adorl answered 12/12, 2022 at 15:45 Comment(1)
You will get a message_thread_id through an update containing Messge object. Note that there is currently no way to "get" message_thread_id explicitlyCapernaum
G
26

You can try to get the message_thread_id from the message link

  1. send a message to the topic you need from the application
  2. right click on the sent message and choose "Copy message link"
  3. paste link somewhere
  4. you will see something like this: https://t.me/c/1112223334/25/33
  5. the value 25(value after long number) from the link will be message_thread_id

I assume that -100 + 1112223334 - will be equal chat_id

The number after will be message_thread_id

And the last one should be message_id

Gerund answered 19/1, 2023 at 21:23 Comment(3)
The answer is correct. Thanks.Rabelaisian
If a topic is deleted will the message_thread_id reassigned to another topic?Cassatt
Thanks. I just need to add -100 instead of - to my chat_id. It will be like chat_id=-100XXXXXXXWhoso
A
0
def telegram(channel, bot, text, img, topic_message_id=""):
    base_url = f'https://api.telegram.org/bot{bot}/sendMessage'
    chat_id = f'@{channel}'  # Used for regular channel messages
    
    if not text:
        text = "Refer - "
    
    # Function to send message
    def send_message(image_link):
        message_text = f"[​​​​​​​​​​​]({image_link}){text}" if image_link else text
        params = {
            'chat_id': chat_id,
            'parse_mode': 'MarkdownV2',
            'text': message_text,
        }
        # If a topic message ID is provided, add it to the parameters to reply within a topic
        if topic_message_id:
            params['reply_to_message_id'] = topic_message_id
            # For topics, the chat_id needs to be numeric. Extract numeric ID from the update if available.
            # This assumes the caller has the correct chat ID format for topics.
        
        response = requests.get(base_url, params=params)
        return response.text
    
    responses = []
    if isinstance(img, list):
        # Send a message for each image link in the list
        for image_link in img:
            response = send_message(image_link)
            responses.append(response)
    else:
        # Handle single image link or empty img
        response = send_message(img)
        responses.append(response)
    
    return responses if isinstance(img, list) else responses[0]

Type "" if you are sending to group or channel. Add the message_id if you want to reply to a topic.

telegram('unofficedtrades','your_token_here',"Amit","",32576)

will work fine.

Antoninaantonino answered 19/2 at 20:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.