python-telegram-botmessageid

Delete outgoing message sent by telegram bot (Telegram, python)


i am stuck in my code as i do not know how to input/derive the message_id of the outgoing message forwarded by my bot.

Background: This is just a part of my code which i would subsequently integrate into the main code. Here, i am testing the functionality of forwarding messages + deleting them. I am able to successfully forward them out but i am stuck at deleting them. i am able to give the input of the chat_id but not able to do so for the message_id to delete. Is there a way to do it especially when i am gonna integrate to my main script which can have a few groups to manage. Please assist the noob in me. Thank you!

My script:

import logging
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
    Updater,
    CommandHandler,
    MessageHandler,
    Filters,
    ConversationHandler,
    CallbackContext,
)

TOKEN = "PUT TOKEN HERE" #INPUT HERE 

# Enable logging
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

logger = logging.getLogger(__name__)

MSG, DELETE_MSG = range(2)


def start(update: Update, context: CallbackContext) -> int:
    update.message.reply_text(
        'Hi! Please post the message you would like to share:')
    return MSG


def send(update: Update, context: CallbackContext) -> int:
    user = update.message.from_user
    logger.info("Message of %s: %s", user.first_name, update.message.text)
    print(update.message.message_id)
    send = update.message.forward(chat_id= 'PUT CHAT ID OF OUTGOING GROUP HERE') #INPUT HERE 
    update.message.reply_text("Please delete")

    return DELETE_MSG

def delete_msg(update: Update, context: CallbackContext) -> int:
    user = update.message.from_user
    logger.info("edit of %s: %s", user.first_name, update.message.text) 
    
    update.message.delete(chat_id='PUT CHAT ID OF OUTGOING GROUP HERE', 
                        message_id='IM STUCK HERE') #INPUT HERE 
    return ConversationHandler.END


def cancel(update: Update, context: CallbackContext) -> int:
    user = update.message.from_user
    logger.info("User %s canceled the conversation.", user.first_name)
    update.message.reply_text('Bye! I hope we can talk again some day.', reply_markup=ReplyKeyboardRemove())
    return ConversationHandler.END


def main() -> None:
    updater = Updater(TOKEN, use_context=True)

    dispatcher = updater.dispatcher
    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            MSG: [MessageHandler(~Filters.command, send)],
            DELETE_MSG: [MessageHandler(~Filters.command, delete_msg)]
        },
        fallbacks=[CommandHandler('cancel', cancel)],
    )

    dispatcher.add_handler(conv_handler)
    
    updater.start_polling()
    updater.idle()


if __name__ == '__main__':
    main()

Solution

  • update.message.reply_text("Please delete") must be a variable and, then, you'll be able to context.bot.deleteMessage the message_id of that. Just like this:

    must_delete = update.message.reply_text("Please delete")
    context.bot.deleteMessage (message_id = must_delete.message_id,
                               chat_id = update.message.chat_id)
    

    Give it a try and let me know if this worked for you!!