Avatar Ilya Mateyko

go.astrophena.name/tools Module

GitHub repository | Commit (8bd93ad)

Personal tools.

starlet

Install this program:

$ go install go.astrophena.name/tools/cmd/starlet@latest

Contents

Starlet is a Telegram bot runner powered by Starlark.

It fetches bot logic written in Starlark from a specified GitHub Gist, executes it within a Starlark interpreter, and handles incoming Telegram updates via webhooks. Starlet provides a sandboxed environment with pre-defined modules for interacting with Telegram, Gemini AI, key-value caching, and more.

When a Telegram update arrives, Starlet parses it, converts the JSON payload into a Starlark dictionary, and invokes the handle function defined in the user's bot.star script. Errors during execution are caught and reported back to the bot owner via Telegram, using a customizable error template.

Starlet is designed for deployment on platforms like Render and includes features like self-pinging to prevent idling on free tiers.

Usage

$ starlet [directory]

If a directory is provided, Starlet runs in development mode. Otherwise, it runs in production mode.

Development Mode

When running in development mode, Starlet serves a bot debugger interface at http://localhost:3000 (or the address specified by the ADDR environment variable). This interface allows you to interact with your bot in a chat-like UI and inspect the Telegram API calls it makes.

In this mode, instead of fetching code from a GitHub Gist, Starlet loads it from the provided directory. It also watches for file changes in the directory and automatically reloads the bot when a file is modified, created, or deleted.

The Telegram Bot API calls are intercepted, and responses are mocked. This allows for local development and testing without needing to expose your bot to the internet or use a real Telegram bot token.

The directory should have the same structure as the GitHub Gist.

Starlark Environment

See https://github.com/astrophena/tools/blob/master/cmd/starlet/internal/bot/doc.md.

GitHub Gist Structure

The bot's code and configuration reside in a GitHub Gist, which must contain:

Any other files (e.g., .json, .txt) can be included and accessed from bot.star using:

files.read("filename.ext")

Entry Point

The bot.star script *must* define a function named handle that accepts a single argument:

def handle(update):
    # update is a Starlark dictionary representing the received Telegram update JSON.
    # Bot logic goes here.
    pass

Optionally, you can define an on_load function:

def on_load():
    # Code here runs every time the bot code is loaded from the Gist,
    # including the initial startup and after a reload via the debug interface.
    # Useful for setup tasks like setting bot commands.
    pass

Starlet calls handle for each incoming Telegram update webhook. It calls on_load after successfully loading or reloading the code from the Gist.

Environment Variables

Starlet is configured using environment variables:

Required:

Optional:

Debug Interface

When not in production mode, or when accessed by the authenticated bot owner in production mode, Starlet provides a debug interface at /debug:

Authentication for the debug interface in production mode uses Telegram Login Widget. The bot owner must authenticate via Telegram. The login callback URL should be set to https://<your-bot-host>/login in BotFather (/setdomain).

Gemini Proxy

Starlet includes an optional, built-in Gemini API proxy at /gemini. This proxy is intended for the author's other pet projects and is not essential for the bot's core functionality. It allows other applications to access the Gemini API without exposing the actual API key. Access to the proxy is protected by JWTs. The server uses the secret key defined in the GEMINI_PROXY_SECRET_KEY environment variable to sign and verify tokens. A new token can be generated on the /debug/gemini-token page.