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.1.tar.gz (14.5 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.1-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: telekit-0.0.1.tar.gz
  • Upload date:
  • Size: 14.5 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.1.tar.gz
Algorithm Hash digest
SHA256 6dd85e7a363ada5d2603d9f5812828bc4a1ba0f2536ebd1ebd17122e7da9206e
MD5 954c22c157089043810ac1edad00d499
BLAKE2b-256 28d94f07ad8d1a77ce3bbcbed4c4f86ad9a915968b7447e64535104d3b9b926e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: telekit-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.8 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 acd2e21eaeb91a454a8a67404e317966b6cbd4c487f61b9b556aa9011c3d936e
MD5 714ed9d8c3bb8380bd1da5a7cc7745f8
BLAKE2b-256 5ef8e1fe958d842392d9c3655da162854c1d10a044b82356a66f3b7d39035e66

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