skip to Main Content

I’ve created a bot to gather information in which users will forward messages to the bot from any other telegram chat/channel/group.

The forwarded messages can be of types video,photo,audio and url or any combination of these.

Below is where I am looking at:
https://core.telegram.org/bots/api#available-types

Right now I have (on Python)—>

@bot.message_handler(content_types=["video", "photo", "audio", "link"])
def send_welcome(message):
bot.reply_to(message, "got it")

BUT since there is no link content type, when the user forwards only a link, this doesn’t work.
I’m looking for some property like forward_status = True if it exists.

3

Answers


  1. Chosen as BEST ANSWER

    I have found a workaround since I've decided to collect forwarded messages in text format as well.

    @dp.message_handler(content_types = ['text'], is_forwarded = True)
    async def check(message):
        await message.reply("yay")
    

    This will identify any forwarded messages in the text format.


  2. According to the line @bot.message_handler(content_types=["video", "photo", "audio", "link"]) I suppose you are using pyTelegramBotAPI

    This package allows you to create custom filters. Here you can find examples of how to create your own filters and how to register them. Actually, the package already has such filter – telebot.custom_filters.ForwardFilter (docs, source code)

    Login or Signup to reply.
  3. Instancemessage.message_id is used to store a specific message ID in a certain user chat, then your bot should forward any message content of media, link, text or another available type, you could try whatever the bot configuration have to integrate these functions just as a simple demostration of your problem and different options from message, check message test for additional information.

    image

    import logging
    
    from telegram import __version__ as TG_VER
    
    try:
        from telegram import __version_info__
    except ImportError:
        __version_info__ = (0, 0, 0, 0, 0)  # type: ignore[assignment]
    
    if __version_info__ < (20, 0, 0, "alpha", 1):
        raise RuntimeError(
            f"This example is not compatible with your current PTB version {TG_VER}. To view the "
            f"{TG_VER} version of this example, "
            f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
        )
    from telegram import ForceReply, Update
    from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
    
    # Enable logging
    logging.basicConfig(
        format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
    )
    logger = logging.getLogger(__name__)
    
    
    # Define a few command handlers. These usually take the two arguments update and
    # context.
    async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
        """Send a message when the command /start is issued."""
        user = update.effective_user
        await update.message.reply_html(
            rf"Hi {user.mention_html()}!",
            reply_markup=ForceReply(selective=True),
        )
    
    async def forward_msg(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
        await context.bot.forward_message(chat_id=update.effective_chat.id,
                                          from_chat_id=update.effective_chat.id,
                                          message_id=update.message.message_id)  
    
    def main() -> None:
        """Start the bot."""
        # Create the Application and pass it your bot's token.
        application = Application.builder().token("ID").build()
    
        # on different commands - answer in Telegram
        application.add_handler(CommandHandler("start", start)) 
     
        messages_handler = MessageHandler(filters.TEXT, forward_msg)
        application.add_handler(messages_handler) 
    
        # Run the bot until the user presses Ctrl-C
        application.run_polling()
    
    
    if __name__ == "__main__":
        main()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search