小能豆

I am trying to make a split command which sends links and splits them for my telegram bot

py

I need Telegram links in bulk so that I can leech the files. The split command splits the links in 4 links per message. All I need to send is the start and the end link.

Now for the setname command I’m expecting that the bot sends -n {new_name} after every link. Everything works fine till here but I want to add a thing such that if I include {episode} in the setname command in that place, I want numbers to be there like 01 02 03 … and I just can’t think of a code.

for now the bot send links like this https://t.me/Season_3_Fire_Force/8 https://t.me/Season_3_Fire_Force/9 https://t.me/Season_3_Fire_Force/10 I want Them to send Links Like this https://t.me/Season_3_Fire_Force/8 -n [AW] S01-E01 Fire Force [720p] [Dual].mkv https://t.me/Season_3_Fire_Force/9 -n [AW] S01-E02 Fire Force [720p] [Dual].mkv https://t.me/Season_3_Fire_Force/10 -n [AW] S01-E03 Fire Force [720p] [Dual].mkv And I can edit that using a command which is /setname

import telegram
from telegram import Update
import requests
from telegram.ext import Updater, CommandHandler, ConversationHandler, CallbackQueryHandler, MessageHandler, Filters, CallbackContext
import os
import keep_alive
import re

# Replace 'YOUR_BOT_TOKEN' with your actual bot token
TOKEN = os.getenv('token')

# Define conversation states
QUALITY_SELECTION, SYNOPSIS, SPLIT = range(3)

# Initialize the Telegram bot
bot = telegram.Bot(token=TOKEN)



# Define a function to handle the /split command
def split(update: Update, context: CallbackContext) -> None:
    update.message.reply_text("Please send the start link.")
    # Set the state to wait for the start link
    context.user_data['state'] = 'start'

    return SPLIT

# Define a function to handle the received links
def handle_links(update: Update, context: CallbackContext) -> None:
    user_state = context.user_data.get('state')
    if user_state == 'start':
        start_link = update.message.text.strip()
        # Set the start link in user data
        context.user_data['start_link'] = start_link
        update.message.reply_text("Great! Now, please send the end link.")
        # Set the state to wait for the end link
        context.user_data['state'] = 'end'
    elif user_state == 'end':
        end_link = update.message.text.strip()
        # Get the start link from user data
        start_link = context.user_data.get('start_link', '')

        # Use regular expressions to extract chat ID and message ID
        match_start = re.match(r'https://t.me/(\w+)/(\d+)', start_link)
        match_end = re.match(r'https://t.me/(\w+)/(\d+)', end_link)

        if match_start and match_end:
            chat_id, start_message_id = match_start.groups()
            chat_id, end_message_id = match_end.groups()

            # Generate links from start to end
            links = [f"https://t.me/{chat_id}/{i}" for i in range(int(start_message_id), int(end_message_id) + 1)]

            # Send links in chunks of 4
            for i in range(0, len(links), 4):
                chunk = links[i:i+4]
                message = '\n'.join(chunk)
                update.message.reply_text(message)

            # Reset the user state
            context.user_data['state'] = None
        else:
            update.message.reply_text("Invalid link format. Please use /split to start the process.")
    else:
        update.message.reply_text("Please use /split to start the process.")


# Define a function to handle the /setname command
def set_name(update, context):
    setname = ' '.join(context.args)
    context.user_data['setname'] = setname
    update.message.reply_text(f"Setname updated to: {setname}")

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



    conv_handler_split = ConversationHandler(
        entry_points=[CommandHandler('split', split)],
        states={
            SPLIT: [MessageHandler(Filters.text & ~Filters.command, handle_links)],
        },
        fallbacks=[]
    )

    setname_handler = CommandHandler('setname', set_name)
    dispatcher.add_handler(conv_handler_split)
    dispatcher.add_handler(setname_handler)

    # Start the bot
    keep_alive.keep_alive()
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

阅读 69

收藏
2023-12-04

共1个答案

小能豆

To achieve the desired behavior of generating links with sequential episode numbers and including the setname command, you can modify the handle_links function as follows:

# Define a function to handle the received links
def handle_links(update: Update, context: CallbackContext) -> None:
    user_state = context.user_data.get('state')
    setname = context.user_data.get('setname', '')

    if user_state == 'start':
        start_link = update.message.text.strip()
        # Set the start link and setname in user data
        context.user_data['start_link'] = start_link
        context.user_data['setname'] = setname
        update.message.reply_text("Great! Now, please send the end link.")
        # Set the state to wait for the end link
        context.user_data['state'] = 'end'
    elif user_state == 'end':
        end_link = update.message.text.strip()
        # Get the start link and setname from user data
        start_link = context.user_data.get('start_link', '')
        setname = context.user_data.get('setname', '')

        # Use regular expressions to extract chat ID and message ID
        match_start = re.match(r'https://t.me/(\w+)/(\d+)', start_link)
        match_end = re.match(r'https://t.me/(\w+)/(\d+)', end_link)

        if match_start and match_end:
            chat_id, start_message_id = match_start.groups()
            chat_id, end_message_id = match_end.groups()

            # Generate links with setname and sequential episode numbers
            links = [f"{link} -n {setname.replace('{episode}', str(i))}" for i, link in enumerate(range(int(start_message_id), int(end_message_id) + 1), 1)]

            # Send links in chunks of 4
            for i in range(0, len(links), 4):
                chunk = links[i:i+4]
                message = '\n'.join(chunk)
                update.message.reply_text(message)

            # Reset the user state
            context.user_data['state'] = None
        else:
            update.message.reply_text("Invalid link format. Please use /split to start the process.")
    else:
        update.message.reply_text("Please use /split to start the process.")

This modification adds support for the {episode} placeholder in the setname command and replaces it with sequential episode numbers when generating the links.

2023-12-04