A minimalist Python library for creating Telegram bots with zero hardcoding and automatic API method support
Project description
Shingram
A minimalist Python library for creating Telegram bots with zero hardcoding and automatic API method support.
Shingram provides a lightweight wrapper for the Telegram Bot API, designed to be simple, powerful, and future-proof. The library automatically supports all Telegram API methods without requiring code updates when new methods are added.
Features
- Zero hardcoding: All Telegram API methods work automatically without library updates
- Automatic conversion: Python-style
snake_casemethod names are automatically converted to Telegram'scamelCaseformat - Event normalization: All Telegram updates are normalized into a consistent
Eventobject format - Complete update coverage: Supports all Telegram Bot API update types
- Simple API: Register handlers with decorators or direct method calls
- Automatic long polling: Built-in offset management and error handling
- Full raw data access: Complete access to original Telegram API data via
event.raw - Synchronous: No async complexity - simple, linear code
Installation
pip install shingram
Quick Start
from shingram import Bot
bot = Bot("YOUR_BOT_TOKEN")
@bot.on("command:start")
def handle_start(event):
bot.send_message(chat_id=event.chat_id, text="Hello!")
@bot.on("message")
def handle_message(event):
bot.send_message(chat_id=event.chat_id, text=f"You wrote: {event.text}")
bot.run()
Event Types
Shingram normalizes all Telegram updates into consistent Event objects. Supported event types include:
- Commands:
command:start,command:help, etc. Usecommandto catch all commands - Messages:
messagefor text messages - Edited messages:
edited_messagefor edited text messages - Channel posts:
channel_postandedited_channel_post - Inline queries:
inline_queryfor inline search queries - Callback queries:
callbackfor inline button clicks - Join/Leave events:
joinandleavefor group membership changes - Poll updates:
pollandpoll_answerfor poll-related events - Chat member updates:
chat_memberandmy_chat_member - Chat join requests:
chat_join_request - Business messages:
business_message,edited_business_message,business_connection - Message reactions:
message_reactionandmessage_reaction_count - Chat boosts:
chat_boostandremoved_chat_boost - Shipping and payment:
shipping_query,pre_checkout_query - Chosen inline results:
chosen_inline_result
All events are normalized into the same Event format for consistency.
Event Object
Every handler receives an Event object with the following fields:
@dataclass
class Event:
type: str # Event type: "command", "message", "callback", etc.
name: str # Event name: "start" for commands, "" for others
chat_id: int # Chat ID (0 if not applicable)
user_id: int # User ID (0 if not applicable)
text: str # Message text or callback data
raw: dict # Complete raw data from Telegram API
reply_to: Optional[int] # ID of replied message (if present)
chat_type: Optional[str] # "private", "group", "supergroup", "channel"
inline_query_id: Optional[str] # For inline_query events
callback_query_id: Optional[str] # For callback_query events
message_id: Optional[int] # Message ID (if available)
username: Optional[str] # User username (if available)
first_name: Optional[str] # User first name (if available)
chat_title: Optional[str] # Chat title (for groups/channels)
Error Handling
from shingram import Bot, TelegramAPIError
try:
bot.send_message(chat_id=123, text="Test")
except TelegramAPIError as e:
print(f"Telegram API error: {e}")
print(f"Error code: {e.error_code}")
print(f"Description: {e.description}")
Examples
See the examples/ directory for complete working examples:
echo_bot.py- Simple echo bot that repeats messagesinline_bot.py- Inline query bot with search functionalitykeyboard_bot.py- Bot with custom keyboard and inline buttonswebhook_flask.py- Webhook example using Flaskwebhook_fastapi.py- Webhook example using FastAPI
License
MIT License - see LICENSE for details.
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
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
File details
Details for the file shingram-0.1.0.tar.gz.
File metadata
- Download URL: shingram-0.1.0.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81967aafb70fd98d0ff82f9f7fc5c4873fafc4bd74a89646fc63dfaf2fcadb8b
|
|
| MD5 |
561c0ae2d306f253935c2c38a9041ce4
|
|
| BLAKE2b-256 |
a9cade4896c3fba4673694bc542fb2a04863a9fd07afa739e47fc6c16ba59e39
|
File details
Details for the file shingram-0.1.0-py3-none-any.whl.
File metadata
- Download URL: shingram-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ebd1d968549ded5a7a5dc8071a4940e767e96f6a7ee049328568f72a1a0a3f2
|
|
| MD5 |
cb9f5a3e24fd7520a57e4f0a7d1b1db1
|
|
| BLAKE2b-256 |
c6b8eeb36aa45f95b4366888114ad196d81cc80da4446a5fd0643ec4b815c122
|