skip to Main Content

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


  1. Chosen as BEST ANSWER

    I manually decoupled the two scripts as follows. In my main.py I changed the command to this:

    def gostart(update: Update, context: CallbackContext) -> None:
        user = update.effective_user
        global goserver
        
        if user.id == USER_ID:
            os.system("echo 1 > /home/ubuntu/gosharing/run")
    

    Then, in /home/ubuntu/gosharing I created a second main.py with the following code:

    import os
    
    run = 0
    prev = 0
    
    while True:
        file = open("run", "rb")
        try:
            run = int(file.readline())
        except:
            pass
        file.close()
        
        if (prev == 0) & (run == 1):
            os.system("pkill -f gs_main.py && python3 gs_main.py > log &")
            print("starting server")
        if (prev == 1) & (run == 0):
            os.system("pkill -f gs_main.py")
            print("killing server")
        
        prev = run
    

    It works :). Might not be the most elegant solution but it does the trick.


  2. You could try to send your both process to background if you use linux.

    1. start your first script
    2. press: CTRL+Z
    3. type: bg
    4. start your second script
    5. press: CTRL+Z
    6. type: bg

    If you like to run both with a simple command, you could create a third script like this:

    import os
    
    os.spawnl(os.P_DETACH, python, 'script_1.py')
    os.spawnl(os.P_DETACH, python, 'script_2.py')
    

    You could find more information about spawnl function here:

    Python 3 – spawnl

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search