Get Started - Learn How To Make Your Bot!

Guides

Code Snippets

Get methods

Post methods

Web API

Useful links

List of all currently Free Items

List of Emotes

Highrise Bot SDK Changelog


Concept

In this guide we will abroad how to keep your bot always running on Replit. This guide was inpired by __simo__

who sent this method in the Highrise Discord.

This method is an alternative for running simple bots but it’s restricted to the memory and cpu limitations from Replit’s free plan, one other alternative is to use the Always On tool from Replit for a few dollars.

Required Libraries

Threading and time are built-in Python libraries so there’s no need to install it using pip, to install flask you can use pip install flask

Code

Here’s the file structure that we will use for this guide:

Untitled

run.py will be the file that we will use to run our bot, to do this we will have to change the .replit configuration file to use the run.py as the file to be runned when clicking in the Run button. To do that you will need to clock on the three dots and select to show the hidden files, then you will click on .replit and change the entry point to run.py:

Untitled

Untitled

Untitled

After it’s time to code the run.py file, open it and import the following libraries:

from flask import Flask
from threading import Thread
from highrise.__**main__** import *
import time

Now let’s define the two classes used to run, the WebServerand the RunBot classes, here are their full definitions:

class WebServer():

  def __init__(self):
    self.app = Flask(__name__)

    @self.app.route('/')
    def index() -> str:
      return "Alive"

  def run(self) -> None:
    self.app.run(host='0.0.0.0', port=8080)

  def keep_alive(self):
    t = Thread(target=self.run)
    t.start()

class RunBot():
  room_id = "room id here"
  bot_token = "bot token here"
  bot_file = "bot file name here"
  bot_class = "bot class name here"

  def __init__(self) -> None:
    self.definitions = [
        BotDefinition(
            getattr(import_module(self.bot_file), self.bot_class)(),
            self.room_id, self.bot_token)
    ]  # More BotDefinition classes can be added to the definitions list

  def run_loop(self) -> None:
    while True:
      try:
        arun(main(self.definitions))

      except Exception as e:
        # Print the full traceback for the exception
        import traceback
        print("Caught an exception:")
        traceback.print_exc()  # This will print the full traceback
        time.sleep(1)
        continue

With our classes defined, all that we need to do now is to add the code with the instructions to create the class objects and start their functions when the file is Runned, to do that we will add the following code to the bottom of the file:

if __name__ == "__main__":
  WebServer().keep_alive()

  RunBot().run_loop()

Perfect, now all you have to do edit the room, token, class and file name informations with your own and click at the Run button!

Untitled

After running your code you should see something like this:

Untitled

This is the link of the WebPage that will keep the bot alive on Replit, but the code will stop running after a few hours if no activity is detected in the website. For that we will use a free tool to ping it every 5 minutes:

Cron-job.org is the free service which allows executing any URL regularly and automatically and it’s the tool that we will use to keep our Website alive:

Free cronjobs - from minutely to once a year. - cron-job.org

At their website you will need to first setup an account and validate your email, you can do that by clicking on the Start Now button in their main page:

Untitled

After creating and verifying your account you just need to create a cronjob in their website console:

Untitled

You can set any title to it, it will be used for you to identify the cronjob only.

At the url field, enter the url given by your code in replit like this one:

Untitled

Set the the Enable Job and Save responses in job history to on.

Set the Execution schedule to 5 minutes.

You can also set your notification preferences and by the end click on Create:

Untitled

Now your server should run for as long as your code is running on Replit, note that replit updates or maintaince can stop your code from running.

Full code and explanation

from flask import Flask
from threading import Thread
from highrise.__main__ import *
import time

class WebServer():

  def __init__(self):
    self.app = Flask(__name__)

    @self.app.route('/')
    def index() -> str:
      return "Alive"

  def run(self) -> None:
    self.app.run(host='0.0.0.0', port=8080)

  def keep_alive(self):
    t = Thread(target=self.run)
    t.start()

class RunBot():
  room_id = "room id here"
  bot_token = "bot token here"
  bot_file = "bot file name here"
  bot_class = "bot class name here"

  def __init__(self) -> None:
    self.definitions = [
        BotDefinition(
            getattr(import_module(self.bot_file), self.bot_class)(),
            self.room_id, self.bot_token)
    ]  # More BotDefinition classes can be added to the definitions list

  def run_loop(self) -> None:
    while True:
      try:
        arun(main(self.definitions))

      except Exception as e:
        # Print the full traceback for the exception
        import traceback
        print("Caught an exception:")
        traceback.print_exc()  # This will print the full traceback
        time.sleep(1)
        continue

if __name__ == "__main__":
  WebServer().keep_alive()

  RunBot().run_loop()

Here's a step-by-step explanation:

  1. Import Required Libraries:
  2. Create a WebServer Class:
  3. Run Method in WebServer Class:
  4. keep_alive Method in WebServer Class:
  5. Create a RunBot Class:
  6. init Method in RunBot Class:
  7. run_loop Method in RunBot Class:
  8. Main Block:

To run this code:

  1. Ensure that you have the necessary dependencies installed, including Flask and any required libraries for your bot (possibly the highrise library).
  2. Fill in the placeholders for the room ID, bot token, bot file name, and bot class name in the RunBot class with the appropriate values.
  3. Run the script. The web server will start and can be accessed at http://<your-replit-app-url>:8080/ to check if it's alive. The bot will run in the background and handle any exceptions gracefully.