如何获取telegram群聊的主题id?

问题描述 投票:0回答:2

最近 Telegram 在 Bot API 版本 6.3 中添加了对组中主题的支持,并将此支持添加到 python-telegram-bot 版本 13.15 中(请查找更改日志 https://docs.python-telegram-bot.org/en/stable/changelog .html

目前尚不清楚如何获取已启用主题的 Grpups 中的主题的主题 ID (message_thread_id)。

有什么建议吗?

我正在尝试找到一种方法来获取电报群聊的主题ID。请注意我说的是message_thread_id,请不要与chat_id混淆。

telegram python-telegram-bot
2个回答
20
投票

您可以尝试从消息链接获取

message_thread_id

  1. 从应用程序向您需要的主题发送消息
  2. 右键单击已发送的消息并选择“复制消息链接”
  3. 将链接粘贴到某处
  4. 你会看到这样的东西:
    https://t.me/c/1112223334/25/33
  5. 链接中的值
    25
    (长数字后的值)将是
    message_thread_id

我假设

-100
+
1112223334
- 将等于 chat_id

后面的数字是

message_thread_id

最后一个应该是

message_id


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]

如果您要发送到群组或频道,请键入“”。如果您想回复某个主题,请添加 message_id。

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

会很好地工作。

© www.soinside.com 2019 - 2024. All rights reserved.