Search code examples
pythontelegramtelegram-botpython-telegram-bot

Send messages to message_thread_id with telegram bot


I have been searching for a solution but have not been successful.

Is there a way to make sure a telegram bot sends all its messages to a specific message_thread_id(topic) with in a group? Currently, when I type a command the bot replies to the initial channel and not in the topic I would like.

Here is a code snippet of one of my commands:

async def add_point(update: Update, context: ContextTypes.DEFAULT_TYPE):
    user = update.effective_user
    chat = update.effective_chat
    is_admin = user.id in [admin.user.id for admin in await context.bot.get_chat_administrators(chat.id)]
    if not is_admin:
        await context.bot.send_message(chat_id=chat.id, text="Only admins use this command.")
        return
    
    # check if the message is a reply to another message
    if not update.message.reply_to_message:
        await context.bot.send_message(chat_id=update.effective_chat.id, text="Please reply to a player to award a point.")
        return

Thank you

I have not currently found a solution to try.


Solution

  • You need to pass your thread id to message_thread_id parameter in send_message method.

    async def add_point(update: Update, context: ContextTypes.DEFAULT_TYPE):
        user = update.effective_user
        chat = update.effective_chat
        thread_id = update.message.message_thread_id
    
        is_admin = user.id in [admin.user.id for admin in await context.bot.get_chat_administrators(chat.id)]
        if not is_admin:
            await context.bot.send_message(chat_id=chat.id, text="Only admins use this command.", message_thread_id=thread_id)
            return
    
        # check if the message is a reply to another message
        if not update.message.reply_to_message:
            await context.bot.send_message(chat_id=update.effective_chat.id, text="Please reply to a player to award a point.", message_thread_id=thread_id)
            return