Skip to main content

This is the simplest module for making Telegram Bots.

Project description

TeleKit Library

Overview

TeleKit is a Python library designed to simplify common tasks for developers working with Telegram bots or Python projects in general.
It provides tools for:

  • Managing data with Vault, a lightweight interface for SQLite databases.
  • Organizing and processing text data using chapters, which allows converting .txt files into Python dictionaries for easy access.
  • Creating modular, reusable handlers and chains for structured code.

The library is designed to reduce boilerplate code and make Python development more efficient.


Quick Guide

Here is an example of defining a handler using TeleKit:

import telebot.types
import telekit
import typing


class StartHandler(telekit.Handler):

    # ------------------------------------------
    # Initialization
    # ------------------------------------------

    @classmethod
    def init_handler(cls, bot: telebot.TeleBot) -> None:
        """
        Initializes the message handler for the '/start' command.
        """
        @bot.message_handler(commands=['start'])
        def handler(message: telebot.types.Message) -> None:
            cls(message).handle()

    # ------------------------------------------
    # Handling Logic
    # ------------------------------------------

    def handle(self) -> None:
        chain: telekit.Chain = self.get_chain()
         
        chain.sender.set_title("Hello")
        chain.sender.set_message("Welcome to the bot! Click the button below to start interacting.")
        chain.sender.set_photo("https://static.wikia.nocookie.net/ssb-tourney/images/d/db/Bot_CG_Art.jpg/revision/latest?cb=20151224123450")
        chain.sender.set_effect(chain.sender.Effect.PARTY)

        def counter_factory() -> typing.Callable[[int], int]:
            count = 0
            def counter(value: int=1) -> int:
                nonlocal count
                count += value
                return count
            return counter
        
        click_counter = counter_factory()

        @chain.inline_keyboard({"⊕": 1, "⊖": -1}, row_width=2)
        def _(message: telebot.types.Message, value: int) -> None:
            chain.sender.set_message(f"You clicked {click_counter(value)} times")
            chain.edit_previous_message()
            chain.send()

        chain.send()

Chapters Example

TeleKit allows you to store large texts or structured information in .txt files and access them as Python dictionaries:

help.txt:

# intro
Welcome to TeleKit library. Here are the available commands:

# entry
/entry — Example command for handling input

# about
TeleKit is a general-purpose library for Python projects.

Usage in Python:

import telekit

chapters: dict[str, str] = telekit.chapters.read("help.txt")

print(chapters["intro"])
# Output: "Welcome to TeleKit library. Here are the available commands:"

print(chapters["entry"])
# Output: "/entry — Example command for handling input"

This approach allows separating content from code and accessing text sections programmatically.


Features

  • Easy-to-use modular handlers and chains for structured project code.
  • Vault for persistent storage of Python data structures in SQLite.
  • Chapters for converting .txt files into Python dictionaries.
  • Lightweight and minimal dependencies, fully compatible with Python 3.12 and higher.

Getting Started With Server / Main.py

# Your server.py or main.py

import telebot
import telekit

from . import handlers # All your handlers

bot = telebot.TeleBot("TOKEN")
telekit.Server(bot).polling()

Getting Started With Handlers

# handlers/start.py

import telebot.types
import telekit
import typing


class StartHandler(telekit.Handler):

    # ------------------------------------------
    # Initialization
    # ------------------------------------------

    @classmethod
    def init_handler(cls, bot: telebot.TeleBot) -> None:
        """
        Initializes the message handler for the '/start' command.
        """
        @bot.message_handler(commands=['start'])
        def handler(message: telebot.types.Message) -> None:
            cls(message).handle()

    # ------------------------------------------
    # Handling Logic
    # ------------------------------------------

    def handle(self) -> None:
        chain: telekit.Chain = self.get_chain()
         
        chain.sender.set_title("Hello")
        chain.sender.set_message("Welcome to the bot! Click the button below to start interacting.")
        chain.sender.set_photo("https://static.wikia.nocookie.net/ssb-tourney/images/d/db/Bot_CG_Art.jpg/revision/latest?cb=20151224123450")
        chain.sender.set_effect(chain.sender.Effect.PARTY)

        def counter_factory() -> typing.Callable[[int], int]:
            count = 0
            def counter(value: int=1) -> int:
                nonlocal count
                count += value
                return count
            return counter
        
        click_counter = counter_factory()

        @chain.inline_keyboard({"⊕": 1, "⊖": -1}, row_width=2)
        def _(message: telebot.types.Message, value: int) -> None:
            chain.sender.set_message(f"You clicked {click_counter(value)} times")
            chain.edit_previous_message()
            chain.send()

        chain.send()

Developer

Telegram: @TeleKitLib

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telekit-0.0.2.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telekit-0.0.2-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

Details for the file telekit-0.0.2.tar.gz.

File metadata

  • Download URL: telekit-0.0.2.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for telekit-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6d796af2849acaa79d121b749e4afdcd43a8c9fa7f766220ae45e89363abbbfa
MD5 58d5fc986c30d9ebca384ad9037d602a
BLAKE2b-256 403afd7310109087edd54ffafd4a9f9c6d2ebc846c7a782af11e9b5d1e730897

See more details on using hashes here.

File details

Details for the file telekit-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: telekit-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for telekit-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 821af13fb4a0b7cd1f6026ac2ac6e723c06eee3544ca2c7001369dc0f8fa832d
MD5 7a51478da49d1cc4a7ec315b9ab76ffa
BLAKE2b-256 3a622f5a04d00b4bf65cec4a59596ff27bbfc8a84f8d2ca43c0d752b3805fa09

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page