A
A
Amigun2021-06-09 16:46:07
Python
Amigun, 2021-06-09 16:46:07

How to run a telegram bot file from another .py file?

Good afternoon! If we simplify the condition as simply as possible, then I have a lot of telegram bots: main_bot and bot_1, bot_2, ..., bot_n. I run main_bot.py, and I want to start, restart, stop (these are all separate commands) my other bots through telegrams. How can this be implemented?

I have already tried using the following commands in main_bot.py :
1. - didn't work for me, everything works without errors, but bot_n does not respond to commands, main_bot still works 2. - does not work on Linux 3. - as in the first case, The bot is not responding to commands. Moreover, if the first thing to put in the bot_n.py file is some kind of printos.system('python bot_n.py')
os.startfile('bot_n.py')
subprocess.Popen(['python', 'bot_n.py'])then he, of course, will display the text in the terminal, but does not respond to commands in the telegram.

Since I have not yet succeeded in starting the bot, I have not yet tried to restart / stop.

What else do I need to try to achieve my goal? Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Amigun, 2021-06-09
@Amigun

For those who have the same problem as me. Here's how I solved it:
I had a hypothesis that bot_n.py worked out all prints and closed. To make sure of this, I wrote a bash script that would open bot_n.py for me, and I ran this bash script from my main_bot.py on command. I opened the terminal and immediately closed.
It turns out that I forgot to add the following to bot_n.py:

if __name__ == '__main__':
    executor.start_polling(dp)

Now bot_n.py is running and the bot is running. But at this time main_bot.py does not work. So you need to add multithreading. That's all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question