[Python] Phython Code for BOT returning incorrect result [closed]

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    This Bot should be working this way simply but I can't locate where things went wrong. TBH, I am not a developer but a beginner learner.

    1. A user opens the chat and sends the bot a link this link has a fixed string in the beginning "https://www.code/+"
    2. The bot checks if the link has this string in the beginning of the link at the start and returns Invalid if the link doesn't have it.
    3. if it does have this fixed string the bot will make a check on the link and see if it returns a join message, if yes then it will return active to the user in the chat if not then it will return expired.

    The issue is that the bot does only make the check if the fixed string is there then returns active without checking if the join response was there!

    I appreciate your help in advance!

    %pip install python-telegram-bot
    %pip install telebot
    import telebot
    import re
    import requests
    import time
    import random
    # Set your API key
    API_KEY = "XYZ"

    # Initialize the bot
    bot = telebot.TeleBot(API_KEY)

    def is_active(link):
    attempts = 5
    while attempts > 0:
    try:
    response = requests.get(link)
    response.raise_for_status()
    if "join" in response.text:
    return False
    return True
    except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:
    attempts -= 1
    print(f"Attempt {attempts}: {str(e)}")
    time.sleep(random.uniform(1, 2))
    return False

    # Handle incoming messages
    @bot.message_handler(func=lambda message: True)
    def handle_message(message):
    # Get the message text
    text = message.text

    # Check if the message contains a link
    if re.search(r'https://www.code/+', text):
    # Extract the link from the message
    link = re.search(r'https://www.code/+', text).group(0)
    # Check if the link if it's active
    if is_active(link):
    bot.send_message(message.chat.id, "The provided link is active.")
    else:
    bot.send_message(message.chat.id, "The provided link is expired.")
    else:
    bot.send_message(message.chat.id, "No valid link found in the message.")

    # Start the bot
    bot.polling()

    Continue reading...

Compartilhe esta Página