Skip to main content

Простая и удобная библиотека для создания Telegram-ботов.

Project description

Extergram (English Version)

A simple and convenient library for creating Telegram bots in Python.

Installation

To install the library, use the following command in your terminal:

pip install extergram

Core Components and How to Use Them

The library provides several simple tools for creating a bot.

  1. Bot - This is the main object for your bot. You create it first by passing your token: bot = Bot('YOUR_TOKEN')

  2. Decorators - This is how you tell the bot how to react to events.

    • @bot.on_message() This decorator is placed on a function that should run whenever a user sends any message to the bot.

    • @bot.on_callback_query() This decorator is used for functions that respond to clicks on inline buttons under a message.

  3. Message and CallbackQuery Objects When your handler function is called, it receives a special object with information about the event.

    • The on_message handler receives a Message object. From it, you can get the message text (message.text) or the chat ID (message.chat.id).
    • The on_callback_query handler receives a CallbackQuery object. From it, you can find out which button was pressed (callback.data) and in which message (callback.message).
  4. ButtonsDesign - This is a builder for creating keyboards. You can add rows of buttons for the user to click.

  5. bot.polling() - This is the command to start the bot. It starts an infinite loop where the bot constantly checks for new messages and calls the appropriate handlers.

Example Usage

Here is a complete example of a simple bot. Create a file named main.py:

from extergram import Bot, ButtonsDesign, Message, CallbackQuery
import datetime

# IMPORTANT: Replace 'YOUR_BOT_TOKEN' with your actual token
bot = Bot('YOUR_BOT_TOKEN')

# First, let's create a keyboard we will use
main_menu = ButtonsDesign().add_row(
    ButtonsDesign.create_button("Show time", "show_time"),
    ButtonsDesign.create_button("About", "about")
).add_row(
    ButtonsDesign.create_button("Delete this message", "delete") # <-- New button
)

# This function will trigger when a user sends a message
@bot.on_message()
def handle_messages(message: Message):

    if message.text == '/start':
        user_name = message.from_user.first_name
        text = f"Hello, {user_name}! I am your new bot."

        bot.send_message(
            chat_id=message.chat.id,
            text=text,
            reply_markup=main_menu
        )
    else:
        bot.send_message(
            chat_id=message.chat.id,
            text=f"You wrote: {message.text}"
        )

# This function will trigger when a user clicks an inline button
@bot.on_callback_query()
def handle_callbacks(callback: CallbackQuery):

    # First, answer the callback to remove the "loading" state on the user's side
    bot.answer_callback_query(callback.id)

    if callback.data == 'show_time':
        now = datetime.datetime.now().strftime("%H:%M:%S")
        bot.edit_message_text(
            chat_id=callback.message.chat.id,
            message_id=callback.message.message_id,
            text=f"The current time is: {now}",
            reply_markup=main_menu
        )

    elif callback.data == 'about':
        bot.edit_message_text(
            chat_id=callback.message.chat.id,
            message_id=callback.message.message_id,
            text="This bot was created using the Extergram library!",
            reply_markup=main_menu
        )
    
    # <<< NEW HANDLER FOR DELETION >>>
    elif callback.data == 'delete':
        bot.delete_message(
            chat_id=callback.message.chat.id,
            message_id=callback.message.message_id
        )

# This line starts the bot's continuous operation
if __name__ == '__main__':
    bot.polling()

Running the Bot

To run the bot, simply execute the following command in your terminal:

python main.py

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

extergram-0.3.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

extergram-0.3.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file extergram-0.3.0.tar.gz.

File metadata

  • Download URL: extergram-0.3.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for extergram-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9a99c5c7c134c2fbda273aba8a64262649c053ae5eace978e3d90f02359d6a29
MD5 6b46b1f8afea07c1b377fd0858f2c1ba
BLAKE2b-256 f84c17d56d09cb030c3ecf881ffc679429a73466817f20ee44b669c07a068146

See more details on using hashes here.

File details

Details for the file extergram-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: extergram-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for extergram-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 62011a430167c45d6115636e2f73ea455389bcbf4198833e428ed8cf37db4beb
MD5 6d5629bf49d035b62b4646818bf5a670
BLAKE2b-256 cfdc88ae9a386e802505224bf1deaa0f2fde0270c66190a9b91009f96e161379

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