yurnov
yurnov

Reputation: 143

How to get topic id for telegram group chat?

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.

Upvotes: 14

Views: 26382

Answers (3)

violetflare
violetflare

Reputation: 103

I didn't find it using the way mentioned in the main answer, but you can find the topic id in another way.

  1. Open Console and use Inspector
  2. Navigate to your topic element which is in the left sidebar
  3. The elements should have a data-thread-id attribute which is what we're looking for here

enter image description here

Upvotes: 0

Amit Ghosh
Amit Ghosh

Reputation: 1606

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.

Upvotes: -1

kramar
kramar

Reputation: 431

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

Upvotes: 34

Related Questions