Skip to main content

An easy-to-use library for building Telegram bots.

Project description

Extergram (v0.4.2)

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

Installation

pip install extergram

Example Usage

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

import extergram
from extergram import Bot, ButtonsDesign, Message, CallbackQuery
from extergram.ext import CommandHandler, CallbackQueryHandler, MessageHandler
import datetime

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

# 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")
)

# Handler for the /start command
def start(bot_instance: Bot, message: Message):
    user_name = message.from_user.first_name
    text = f"Hello, {user_name}! I am your new bot."

    bot_instance.send_message(
        chat_id=message.chat.id,
        text=text,
        reply_markup=main_menu
    )

# Handler for any other text message
def echo(bot_instance: Bot, message: Message):
    bot_instance.send_message(
        chat_id=message.chat.id,
        text=f"You wrote: {message.text}"
    )

# Handler for inline button clicks
def handle_callbacks(bot_instance: Bot, callback: CallbackQuery):
    # First, answer the callback to remove the "loading" state
    bot_instance.answer_callback_query(callback.id)

    if callback.data == 'show_time':
        now = datetime.datetime.now().strftime("%H:%M:%S")
        bot_instance.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_instance.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
        )
    elif callback.data == 'delete':
        bot_instance.delete_message(
            chat_id=callback.message.chat.id,
            message_id=callback.message.message_id
        )

def main():
    # Register handlers
    bot.add_handler(CommandHandler("start", start))
    bot.add_handler(CallbackQueryHandler(handle_callbacks))
    bot.add_handler(MessageHandler(echo)) # Must be after CommandHandler

    # Start the bot
    bot.polling()

if __name__ == '__main__':
    main()```

## Running the Bot

`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.4.3.tar.gz (7.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.4.3-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: extergram-0.4.3.tar.gz
  • Upload date:
  • Size: 7.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.4.3.tar.gz
Algorithm Hash digest
SHA256 fba2c08cc85925bd090ec49a0264a35f1a0d685e2eae692d75085bdca923691d
MD5 84c5ab63b7049986353c00f59b7890f1
BLAKE2b-256 c41ea55d9ba3db4accc07741bb6e4dc3e91d1c4c5bf435de7d975c7f9ed5ffd6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: extergram-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 10.6 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.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a5cc7e5976d3b626bb32e7ed01a8124c7e3b428ffce2f8465c7c0e9e26a674c6
MD5 c09d904467d0680a4a689c74986bc309
BLAKE2b-256 9032dc56d5f649262272357fc2d97b5cd7f4d917e74ffe019616fece773cd2ce

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