Skip to content

Other API features

TGPy API allows you to use TGPy internal features in your messages and modules.

import tgpy.api


tgpy.api.config provides you simple key-value store for any data. The data, as well as some TGPy settings, is saved to tgpy/config.yml.

tgpy.api.config.get(key: str, default: JSON = None) -> JSON

tgpy.api.config.set(key: str, value: JSON)

tgpy.api.config.unset(key: str)
useful when modifying objects acquired via the get method

Code processing

You can use the following functions to parse and run code.

Parse code

async parse_code(text: str) -> ParseResult(is_code: bool, original: str, transformed: str, tree: AST | None)

Checks if the given text is code and gives AST and other info

Parse a message

parse_tgpy_message(message: Message) -> MessageParseResult(is_tgpy_message: bool, code: str | None, result: str | None)

Splits Telethon message object into TGPy code and result (if present)

Run code

async tgpy_eval(code: str, message: Message = None, *, filename: str = None) -> EvalResult(result: Any, output: str)

Runs code and gets the result and the output