L
L
lemonlimelike2020-05-30 23:36:02
Python
lemonlimelike, 2020-05-30 23:36:02

How to make a script start and stop function?

Hello! I am writing a telegram bot. There was such a need to both start the bot and turn it off. That is, I write / start - starts, / stop - stops. And such an idea arose. There will be only 2 scripts, one will receive commands and different messages, and the second script will be launched depending on the incoming command. That is, I write / start in the bot, the first script listens to this - it receives the / start command, then a couple more messages and the second script is launched. Then, if you need to stop it, then I write / stop, the first script receives this command and stops the second script. Maybe the idea is nonsense, but how to implement it? How to run scripts from other files (scripts)? In python

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2020-05-31
@lemonlimelike

The first question is why?
However,

@bot.message_handler(commands=['start', 'stop'])
def control(message):
    
    if message.text == '/start':
        proc = subprocess.Popen(['python', 'second.py'], creationflags=subprocess.CREATE_NEW_CONSOLE)

        with open('pid.txt', 'w') as f:
            f.write(str(proc.pid))

    elif message.text == '/stop':
        
        with open('pid.txt', 'r') as f:
            script_pid = int(f.read())

        os.kill(script_pid, 9)

F
fojetin, 2020-05-31
@fojetin

Hey!
I think it would be easier to turn the bot on/off with one of the built-in features.
You can make a switchable bool parameter. And all bot commands, except /on /off, are run through the middleware, which checks whether the bot is on or off.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question