Getting started
BotFleet enables you to run Python bots in the cloud without managing servers. A bot can either be scheduled for execution at specific times or executed manually via the UI, REST API and Python SDK.
During an execution, every bot has access to a persistent and dedicated storage that can be used to store and retrieve data.
Create a bot
Bots can be created from the UI on app.botfleet.ai/bots/create/ or via REST API and Python SDK. In the following sections, we will focus on the UI.
A bot consists of three key components: script, requirements, and environment variables.
-
Script is your Python script that will be executed.
-
Requirements are the Python packages that your script depends on. For example, if your script uses the
requests
package, you need to add it to the requirements. -
Environment variables are the variables that your script can access during execution.
Only script is required. Requirements and environment variables are optional.
Script
Write your Python script as you normally would. The only necessity is that the script includes a def main
function, which will be executed. The main
function can have either zero parameters or two parameters: request
and store
.
def main():
# Your code goes here
def main(request, store):
# Your code goes here
Both approaches are valid. Refer to the Request and Data Storage sections for more information.
You can use the code editor to add your script:
Here's the full script from the screenshot above:
import os
import requests
from bs4 import BeautifulSoup
NEWS_WEBSITE_URL = os.getenv("NEWS_WEBSITE_URL")
HEADLINE_TAG = os.getenv("HEADLINE_TAG")
HEADLINE_CLASS = os.getenv("HEADLINE_CLASS")
def main(request, store):
"""Scrape a news website, extract the headlines and save them in store."""
response = requests.get(NEWS_WEBSITE_URL)
soup = BeautifulSoup(response.text, "html.parser")
headline_elements = soup.find_all(HEADLINE_TAG, class_=HEADLINE_CLASS)
headlines = [element.text.strip() for element in headline_elements]
# Save the headlines in store. Learn more about store here:
# https://botfleet.ai/docs/getting-started/data-storage
store[NEWS_WEBSITE_URL] = headlines
return headlines
Requirements
If your script depends on any Python packages, you need to add them to the requirements. Each package should be on a separate line:
Requirements from the screenshot above:
requests
beautifulsoup4
Environment variables
If your script depends on any environment variables, you need to add them to the environment variables. Each variable should be on a separate line:
Environment variables from the screenshot above:
NEWS_WEBSITE_URL=https://news.ycombinator.com
HEADLINE_TAG=span
HEADLINE_CLASS=titleline
Execute a bot
After a bot is created, build process will start which will take a few seconds. Once the build is complete, you can execute the bot manually or schedule it for execution.
Scheduled
Cron expressions are used to specify the schedule. If you are not familiar with cron expressions, you can use AI to generate one for you:
Manual
Often, it's useful to execute a bot programmatically. You can do so via REST API and Python SDK.
Here's how you can execute a bot via Python SDK:
import botfleet
botfleet.api_key = "YOUR_API_KEY"
bot = botfleet.Bot.retrieve(id="BOT_ID")
execution = bot.execute()
You can copy the bot's ID from the UI:
Request
As mentioned above, the main
function can have either zero parameters or two parameters: request
and store
. request
is an object with two attributes: scheduled
and payload
.
scheduled
is a boolean that indicates whether the execution was scheduled or manual:
def main(request, store):
if request.scheduled:
print("This execution was scheduled")
else:
print("This execution was manual")
Payload
payload
is a any JSON-serializable value that was passed to the execution via REST API or Python SDK:
Here's how to pass a payload via the SDK:
import botfleet
botfleet.api_key = "YOUR_API_KEY"
bot = botfleet.Bot.retrieve(id="BOT_ID")
execution = bot.execute(payload={"foo": "bar"})
Here's how to access the payload from the script:
def main(request, store):
print(request.payload) # prints {"foo": "bar"}
Logging
You can use the print
function to log messages. You can also use the logging
module:
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
logger.addHandler(handler)
def main():
logger.info("From the logging module")
print("From the print function")
Logs can be viewed from the UI:
Logs can also be retrieved via REST API and Python SDK.
Here's how to access logs via the SDK:
import botfleet
botfleet.api_key = "YOUR_API_KEY"
bot = botfleet.Bot.retrieve(id="BOT_ID")
execution = bot.execute()
print(execution.logs)
Return value
The return value of the main
function must be JSON-serializable:
def main():
return "Hello world" # ok
def main():
return object() # error
Return values can be viewed from the UI:
Return value can also be retrieved via REST API and Python SDK.
Here's how to access return value via the SDK:
import botfleet
botfleet.api_key = "YOUR_API_KEY"
bot = botfleet.Bot.retrieve(id="BOT_ID")
execution = bot.execute()
print(execution.return_value)