Skip to content

Using Telegram as a Database

You can use Telegram as a Database, thanks to TelegramDB.


This feature is still in beta and this is just a pre-release.

Needed Variables

  • DB_SESSION - String Session for a Telegram account.

  • DB_CHAT_ID - Create a new telegram channel for database. Set Telegram ID for that channel as this.

What is a DataPack?

Like there are Tables in SQL, TelegramDB has DataPacks. It is just a fancy term to scare you.

Create a DataPack

from pystark import Stark
from pystark.database.telegram_db import DataPack, Member, Session

# Create a DataPack like you create a Table in Postgres (SQLAlchemy).
class TestData(DataPack):
    __datapack_name__ = "test"

    id = Member(int, is_primary=True)
    name = Member(str)
    aim = Member(str)

    def __init__(self, id, name=None, aim=None): = id = name
        self.aim = aim


Query a DataPack

# Import Session object from pystark
from pystark.database.telegram_db import Session
# "" is a file in "database" folder, where class TestData is located.

from database.test_data import TestData

# Commit to DataPack.
test = TestData(1, name="Stark", aim="Create Time Machine")

# Get info from DataPack.
user = TestData(1)  # pass primary key.
if Session.get(user):
    print("User exists!")
    print("ID: ",
    print("Name: ",
    print("Aim: ", user.aim)
    print("No user with ID: 1")

Load DataPacks and Enable Telegram DB

After adding variables specified above, you need to create datapack(s) using Python Classes. For a datapack to be loaded, you need to import it in some plugin. Also, you obviously you need to import it to use it anyway.


# Let's say I've a file named `` with class `DataPack1`.
# This file is located in a folder named `dbs` in same directory as ''.
from dbs.a import DataPack1

This will enable the database and load DataPack1 named datapack.

Back to top