๐Ÿ”„ Auto Reconnect

๐Ÿ”„ Auto Reconnect

Concept

Itโ€™s a bummer when you let your bot running before going to sleep only to wake up and see that it crashed a few minutes after you closed your eyes, who canโ€™t relate to that? But donโ€™t give up, everything has a solution and for this case is pretty simple. For today, I will show how to make you bot reconnect after crashing. Keep in mind that you have to investigate why your bot is crashing, this is not a solution, just a way around the problem. ๐Ÿ˜‰

Required Libraries

  • highrise-bot-sdk
  • time

Code

Hereโ€™s what we will need to import into our bot:
from highrise.__main__ import * import time
In the run_bot.py file.
Why we are importing from the __main__ file? Thatโ€™s because thereโ€™s where the magic happens, whe are use to perform the command highrise <file_name>:<class_name> <room_id> <token_key>, well, this command is handled by __main__ file and it is what makes the bot run, there for is where the bot starts and stops.
To re-run the code after crashing we can use a very simple trick, that is to make it run in a loop. To that letโ€™s create a new file called run_bot.py:
In the run_bot.py file, we can see that itโ€™s in the same folder as our bot.py file.
In the run_bot.py file, we can see that itโ€™s in the same folder as our bot.py file.
Perfect, now we need to write our code, the bot runs based on a few informations:
  • Bot file name
  • Bot class name
  • Room Id
  • Bot Token
Now letโ€™s create those variables in our code:
from highrise.__main__ import * import time bot_file_name = "bot" bot_class_name = "Bot" room_id = "61186670bd5272731251a06f" bot_token = "96cfd73016e193a33f335eab464fbff3c621ea6eb666516c13e58e7646666097"
In the run_bot.py file.
Ok, now we have to create a BotDefinition object, this object will be used by the run function from the SDK. Note that the bot file and class name will be used in the BotDefinition to get the BaseBot class attribute from the Bot code:
from highrise.__main__ import * import time bot_file_name = "bot" bot_class_name = "Bot" room_id = "61186670bd5272731251a06f" bot_token = "96cfd73016e193a33f335eab464fbff3c621ea6eb666516c13e58e7646666097" my_bot = BotDefinition(getattr(import_module(bot_file_name), bot_class_name)(), room_id, bot_token)
In the run_bot.py file.
After it we can proceed to make a infite loop and use the function to run the bot from the __main__ file within a try & except block to make sure that, if the bot crashes, the error will be handled and it will proceed to run again after a few seconds:
from highrise.__main__ import * import time bot_file_name = "bot" bot_class_name = "Bot" room_id = "61186670bd5272731251a06f" bot_token = "96cfd73016e193a33f335eab464fbff3c621ea6eb666516c13e58e7646666097" my_bot = BotDefinition(getattr(import_module(bot_file_name), bot_class_name)(), room_id, bot_token) while True: try: definitions = [my_bot] arun(main(definitions)) except Exception as e: print(f"An exception occourred: {e}") time.sleep(5)
In the run_bot.py file.
After it, your code should be something like this one:
Image example.
Image example.
Now your bot will reconnect if your code crashes! To run your bot all you need to do is write in your terminal the following:
py run_bot.py
In the terminal/console.
An additional tip that I will give is that, inside of the __main__ file you can change the line 169 from:
frame = await ws.receive(READ_TIMEOUT) if frame.type in [WSMsgType.CLOSE, WSMsgType.CLOSED]: print( f"ERROR connection with ID: {session_metadata.connection_id} closed." ) # Close frame ka_task.cancel() return
In the __main__ file, to access it, in your imports you can hold CTRL and click on __main__.
to:
frame = await ws.receive(READ_TIMEOUT) if frame.type in [WSMsgType.CLOSE, WSMsgType.CLOSED]: print( f"ERROR connection with ID: {session_metadata.connection_id} closed." ) # Close frame ka_task.cancel() raise ConnectionResetError
In the __main__ file, to access it, in your imports you can hold CTRL and click on __main__.
Image example on how the code from __main__ will look like after the changes.
Image example on how the code from __main__ will look like after the changes.
This will make the bot reconnect instead of closing in case of TIMEOUT errors.
ย 
Built with Potion.so