I have a telegram bot that I want to use to start another telegram bot. You cannot run two telegram bots within a single python script, so I have two python scripts: main.py and gs_main.py. When I run them concurrently in two seperate screen sessions everything is fine. However, starting gs_main.py from main.py is causing me headaches. My last try is as follows:
In main.py I have the following command to start gs_main.py:
def gostart(update: Update, context: CallbackContext) -> None:
user = update.effective_user
global goserver
if user.id == USER_ID:
goserver = os.system("./start_goserver.sh")
The contents of start_goserver.sh are as follows:
cd /home/ubuntu/gosharing
python3 gs_main.py
However, this seems to open gs_main.py as a subprocess of main.py since I am getting the following error:
Error while getting Updates: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
No error handlers are registered, logging exception.
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/ext/updater.py", line 646, in _network_loop_retry
if not action_cb():
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/ext/updater.py", line 597, in polling_action_cb
updates = self.bot.get_updates(
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/ext/extbot.py", line 222, in get_updates
updates = super().get_updates(
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/bot.py", line 130, in decorator
result = func(*args, **kwargs)
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/bot.py", line 2861, in get_updates
self._post(
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/bot.py", line 295, in _post
return self.request.post(
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/utils/request.py", line 361, in post
result = self._request_wrapper(
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/utils/request.py", line 283, in _request_wrapper
raise Conflict(message)
telegram.error.Conflict: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
My question is, how do I run gs_main.py without it being a subprocess of main.py?
2
Answers
I manually decoupled the two scripts as follows. In my main.py I changed the command to this:
Then, in /home/ubuntu/gosharing I created a second main.py with the following code:
It works :). Might not be the most elegant solution but it does the trick.
You could try to send your both process to background if you use linux.
If you like to run both with a simple command, you could create a third script like this:
You could find more information about spawnl function here:
Python 3 – spawnl