Starlark Environment

These built-in functions and modules are available in the Starlark environment.

config

A module containing configuration information about the bot.

config.bot_id

The Telegram ID of the bot.

config.bot_username

The username of the bot.

config.owner_id

The Telegram ID of the bot owner.

config.version

The version of the bot.

config.is_dev

Whether the bot is running in development mode.

debug

A module containing debugging utilities.

debug.stack()

Returns a string describing the current call stack.

debug.go_stack()

Returns a string describing the Go call stack.

fail(err: str)

Terminates execution with a specified error message.

files

A module for accessing files provided to the bot.

files.read(name: str)

Reads the content of a file.

gemini

This module provides a single function, generate_content, which uses the Gemini API to generate text, optionally with an image as context.

It accepts the following keyword arguments:

For example, for a text-only prompt:

responses = gemini.generate_content(
    model="gemini-1.5-flash",
    contents=[
        ("user", "Once upon a time,"),
        ("model", "there was a brave knight."),
        ("user", "What happened next?")
    ],
    system_instructions="You are a creative story writer. Write a short story based on the provided prompt."
)

To ask a question about an image:

image_data = ... # read image file content as bytes
responses = gemini.generate_content(
    model="gemini-1.5-flash",
    contents=[
        ("user", "Describe this image in detail.")
    ],
    image=image_data
)

The responses variable will contain a list of generated responses, where each response is a list of strings representing the parts of the generated content.

kvcache

This module provides two functions for using a simple key-value cache:

markdown

A module for Markdown conversion.

markdown.convert(s: str)

Converts a Markdown string to a Telegram message struct.

module(name: str, **members)

Instantiates a module struct with the name from the specified keyword arguments.

struct(**fields)

Instantiates an immutable struct from the specified keyword arguments.

telegram

This module provides two functions for working with the Telegram Bot API: call and get_file.

The call function takes two arguments:

For example, to send a message to a chat:

response = telegram.call(
    method="sendMessage",
    args={
        "chat_id": 123456789,
        "text": "Hello, world!",
    }
)

The response variable will contain the response from the Telegram Bot API.

The get_file function takes one argument:

It returns the content of the file as bytes. For example:

file_content = telegram.get_file(file_id="...")

time

A module for time-related functions. See https://pkg.go.dev/go.starlark.net/lib/time#Module.