Простая и удобная библиотека для создания Telegram-ботов.
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
Release history Release notifications | RSS feed
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.2.tar.gz
(7.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
extergram-0.4.2-py3-none-any.whl
(10.7 kB
view details)
File details
Details for the file extergram-0.4.2.tar.gz.
File metadata
- Download URL: extergram-0.4.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a35bd4d82bb44c17f7fcbf6cbe6d4d58ff5adeaabbd6550992c8b012b7f3ad35
|
|
| MD5 |
99e45c2cfb2a271454c933fffb01392d
|
|
| BLAKE2b-256 |
79e47c41041c5c3d24c4b2110c9c8b88270cc704cbed42ab4f92a74d78959257
|
File details
Details for the file extergram-0.4.2-py3-none-any.whl.
File metadata
- Download URL: extergram-0.4.2-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c6a91516cf2d073a38fb223b6091d9f032dcb390730fc4838ea403d62bba055
|
|
| MD5 |
30be4448147e25712a958cc47d4c004d
|
|
| BLAKE2b-256 |
288eddca90e72092555024d5b9d96245021df3543175b73fa1bab278ea500500
|