Skip to main content

Easy-to-use synchronous interface for telegram-bot library with async backend and intuitive handlers.

Project description

TgrEzLi

TgrEzLi is a Python library based telegram-bot that simplifies Telegram bot development by providing a synchronous interface, runs in background with non-blocking operations and offers intuitive handlers for messages, commands, callbacks, and API requests.

Installation

pip install TgrEzLi

Usage

# Importing the library
from TgrEzLi import TEL, TgMsg, TgCmd, TgCb, TgArgs, TReq
# or
from TgrEzLi import *

# Initialization
bot = TEL()

# Some options
# Activate/Deactivate log saving
bot.setSaveLog(True)

# Set custom HTTP host and port; by default they are "localhost" and 9999
bot.setHost("127.0.0.1")
bot.setPort(8080)

# Connecting
# You can connect one or multiple chats
TOKEN = "123456789:ABCDEFGHIJKLMNO"
CHAT_DICT = {
    "chat1": "123456789",
    "chat2": "789456123"
}

# You can use the .connect() method that takes token and chat dictionary as argument.
bot.connect(TOKEN, CHAT_DICT)

# You can alsouse the .signup() and .login() methods to insert token and chats only once and secure them with a password
bot.signup(TOKEN, CHAT_DICT, 'password') # only once; it'll store encrypted data in a tgrdata.cy file
bot.login('password') # when already 'signed up'

# Basic commands to send Messages, Images, Files, Position, Log, Handlers, Bot Info
# Each method has a chat parameter, by defailt it's set to the first chat added to the dictionary
# You can either insert a specific chat name or a List 
bot.sendMsg(text, chat)
bot.sendImg(photo_path, caption, chat)
bot.sendFile(file_path, caption, chat)
bot.sendPosition(latitude, longitude, chat)
bot.replyToMsg(text, msg_id, chat_id)
bot.sendLog(limit, chat)
bot.sendInfo(chat)
bot.sendRegisteredHandlers(chat)

# Methods to create and send Inline Keyboards (simply called Buttons)
bot.sendButtons(text, buttons, chat)

# Handlers for Messages, Commands, and Callbacks
# Each method has a chat parameter, by defailt it's set to the first chat added to the dictionary
# You can either insert a specific chat name or a List 
@bot.onMessage(chat)
@bot.onCommand(command, chat)
@bot.onCallback(chat)
@bot.onApiReq(endpoint, args, host, port)

# Data Interfaces
# Each Handler has it's own integrated interface to easly access received data

# TgMsg gets received messages data for .onMessage() handler
TgMsg.text
    .msgId
    .chatId
    .userId
    .userName
    .timestamp
    .raw_update 

# TgCmd gets data and parameters for .onCommand() handler
TgCmd.command
    .args       # gives text outside the command
    .msgId
    .chatId
    .userId
    .userName
    .timestamp
    .raw_update

# TgCb gets data for .onCallback() handler
TgCb.text
    .value
    .msgId
    .chatId
    .userId
    .userName
    .timestamp
    .raw_update

# TgArgs gets parameters for .onApiReq() handler
TgArgs.get(key, default)


# Treq makes it really easy to send an api request
# It takes the endpoint path like "\action"
# .host and .port are optional, by default "localhost" and 9999
# you can add multiple .arg() methods to add parameters names and values
# or you can use .body() to send the full body
TReq(endpoint)\
    .host(host)\
    .port(port)\
    .arg(name, value)\
    .body(body_dict)\
    .send()

## Examples ##

# basic commands
bot.sendMsg("Message Test")
bot.sendImg("img.jpg", "Image Test", 'chat1')
bot.sendFile("file.pdf", "File Test", ['chat1', 'chat2'])
bot.sendPosition(45.4642, 9.1900, ['chat1', 'chat2'])


# Message Handler
@bot.onMessage()
def on_message_default_chat():
    bot.sendMsg(f"Hi {TgMsg.userName}! You wrote: {TgMsg.text}")
    reply = f"Message Id: {TgMsg.msgId} User Id: {TgMsg.userId} Raw: {TgMsg.raw_update}"
    bot.replyToMsg(reply, msg_id=TgMsg.msgId)


# Command Handler
@bot.onCommand("/start")
def handle_start():
    bot.sendMsg(f"Welcome to chat 1 {TgCmd.userName}!")
    if TgCmd.args : bot.sendMsg(f"You also wrote: {TgCmd.args}!")

@bot.onCommand("/start", 'chat2')
def handle_start():
    bot.sendMsg(f"Welcome to chat 2 {TgCmd.userName}!")


# Buttons
@bot.onCommand("/buttons", 'chat1')
def show_buttons():
    buttons = [
        [{"text": "Button 1", "value": "red"}],
        [{"text": "Button 2", "value": "yellow"}]
    ]
    bot.sendButtons("Scegli un colore:", buttons, "chat1")

# Callback Handler
@bot.onCallback("chat1")
def on_callback_chat1():
    match TgCb.value:
        case "red":
            bot.sendMsg(f"You pressed {TgCb.text} -> {TgCb.value}!", "chat1")
        case "yellow":
            bot.sendMsg(f"You pressed {TgCb.text} -> {TgCb.value}!", "chat1")


# Api Requests
@bot.onApiReq('/action', args=['chat','msg'])
def action():
    id = TgArgs.get('chat')
    msg = TgArgs.get('msg')
    bot.sendMsg(msg, id)

# TReq usage
TReq('/action')\
    .host('127.0.0.1')\
    .port(8080)\
    .arg('chat_id', 'chat1')\
    .arg('msg', 'Hello from TReq!')\
    .send()

while True:
    pass

License

MIT License

Copyright (c) 2025 eaannist

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Development status

TgrEzLi is a work-in-progress personal project. Suggestions, feature requests, and constructive feedback are highly welcome. Feel free to open an issue or submit a pull request.

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

tgrezli-0.1.2.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

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

tgrezli-0.1.2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file tgrezli-0.1.2.tar.gz.

File metadata

  • Download URL: tgrezli-0.1.2.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for tgrezli-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2f4847afff78edba128c2d504e9b5f89f6163f9a331d7451997f5b1fd94c9290
MD5 f6a2ed74ba28819d4b4d49084b5acbc4
BLAKE2b-256 2c6f43f51490a9a3f53d96e45202a3efbd03d165965b0f86fda18943b85a77e3

See more details on using hashes here.

File details

Details for the file tgrezli-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: tgrezli-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for tgrezli-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f9a95c0386be93225fb388918018f0b69ccf81ac8384b6254b03a89153b9f1a6
MD5 0e58a87b0733798766d809813f44eb6e
BLAKE2b-256 1b72d94ce51dd65c80048fbc0cd7b57f694b5e9fa28d32f397c0244bc086d024

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