A Python library with a simple and familiar interface for working with the official Rubika bot API
Project description
PyRubikaBotAPI
A powerful and intuitive Python library for building Rubika bots – from simple echo bots to advanced interactive assistants.
Installation
Requires Python 3.10 or higher.
pip install PyRubikaBotAPI
To upgrade pip first:
python -m pip install --upgrade pip
Quick Start
- Create a bot via @BotFather and get your token.
- Write your first bot:
from rubibot import RubiBot
bot = RubiBot("YOUR_BOT_TOKEN")
@bot.message_handler(commands=["start"])
def start(message):
bot.send_message(message.chat_id, "Hello! Welcome to my Rubika bot.")
bot.polling()
Run the script and your bot is live!
Core Features
Message Handlers
Handle incoming messages based on commands, content type, or custom logic. Handlers are executed in order, and only the first matching handler runs for each message.
# Handle /start and /help
@bot.message_handler(commands=["start", "help"])
def handle_commands(message):
...
# Handle all stickers
@bot.message_handler(content_types=["sticker"])
def sticker_handler(message):
...
# Catch-all for any text message
@bot.message_handler()
def echo(message):
bot.reply_to(message, f"You said: {message.text}")
Supported content types: text, file, location, sticker, contact, poll
Supported update types: NewMessage, UpdatedMessage, RemovedMessage, StartedBot, StoppedBot
Sending Messages & Replies
send_message(chat_id, text, ...)– Send text (max ~4096 chars). Supports inline/chat keypads, silent notifications, reply IDs.reply_to(message, text, ...)– Convenience method to reply directly to a user's message.
bot.send_message(chat_id, "Hello World!")
bot.reply_to(message, "Thanks for your message!")
Media & Files
Send photos, videos, voices, GIFs, and other files. All methods accept a file path (str) or a file-like object (BufferedReader).
| Method | Max Size | Formats |
|---|---|---|
send_photo |
10 MB | PNG, JPG, GIF, WEBP |
send_voice |
– | MP3 |
send_video |
50 MB | MP4 |
send_gif |
– | MP4 (no sound) |
send_file |
50 MB | Any |
bot.send_photo(chat_id, "image.png", text="Check this out!")
bot.send_video(chat_id, "video.mp4", text="Watch this")
Interactive Keypads
Two types of keypads are available:
- ChatKeypad – Persistent buttons at the bottom of the chat.
- InlineKeypad – Buttons directly under a message.
from rubibot.types import KeypadSimpleButton, KeypadRow, InlineKeypad, ChatKeypad
# Create buttons
btn1 = KeypadSimpleButton("Option 1", "id1")
btn2 = KeypadSimpleButton("Option 2", "id2")
row = KeypadRow().add(btn1, btn2)
inline_kb = InlineKeypad().add(row)
chat_kb = ChatKeypad(resize_keyboard=True).add(row)
# Send with inline buttons
bot.send_message(chat_id, "Choose:", inline_keypad=inline_kb)
# Send with chat keypad
bot.send_message(chat_id, "Menu:", chat_keypad=chat_kb)
# Remove chat keypad
from rubibot.types import ChatKeypadRemove
bot.send_message(chat_id, "Keypad removed", chat_keypad=ChatKeypadRemove())
For advanced inputs, use KeypadCalendarButton, KeypadNumberPickerButton, KeypadStringPickerButton, KeypadLocationButton, KeypadTextboxButton. (See documentation for details.)
Polls & Contacts
from rubibot.types import ChatPoll
poll = ChatPoll("Your favorite language?")
poll.add_options("Python", "JavaScript", "Go")
bot.send_poll(chat_id, poll)
bot.send_contact(chat_id, "Ali", "Rezaei", "+989123456789")
Message Management
- Forward:
bot.forward(from_chat_id, to_chat_id, message_id) - Edit:
bot.edit_message(chat_id, message_id, new_text="...", new_inline_keypad=...) - Delete:
bot.delete_message(chat_id, message_id)
Bot & Chat Information
bot.get_me()– Get bot details (username, avatar, description, share URL).bot.get_chat(chat_id)– Get chat info (title, username, type).
info = bot.get_me()
print(info.username)
chat = bot.get_chat("chat_id")
print(chat.title)
Commands Registration
from rubibot.types import BotCommand
bot.set_commands(
BotCommand("/start", "Start the bot"),
BotCommand("/help", "Show help menu")
)
Running the Bot
Polling (for development / testing)
bot.polling(t=1, limit=10) # Check updates every 1 second
⚠️ Polling is not optimized for production. Use webhooks instead.
Webhooks (production)
bot.set_webhook("https://your-server.com/webhook")
Then process incoming JSON manually with process_new_updates:
from rubibot.updates import Update
# Inside your web framework endpoint:
update = Update(request.json)
bot.process_new_updates([update])
Working with Updates & Models
Message Object
When a handler is called, you receive a Message object with useful attributes:
chat_id, message_id, text, sender_id, file, location, sticker, contact, poll, aux (button data), forwarded_from, etc.
@bot.message_handler()
def handler(message):
if message.has_file():
file_id = message.file.id
url = bot.get_file(file_id)
data = bot.download_file(url)
# Save or process the file
Aux Data (Button Clicks)
When a user taps a button, message.aux.button_id gives the ID you assigned. Use it to branch logic.
Next‑Step Handlers
You can register a one‑time handler for the very next message in a specific chat or from a specific user – useful for multi‑step flows:
bot.register_next_step_message_handler_by_chat_id(chat_id, my_callback)
Complete Example
from rubibot import RubiBot
from rubibot.types import (
Message, KeypadSimpleButton, KeypadRow,
ChatKeypad, ChatKeypadRemove, BotCommand
)
bot = RubiBot("YOUR_TOKEN")
@bot.message_handler(commands=["start"])
def start(message: Message):
btn = KeypadSimpleButton("Say Hello", "hello_btn")
row = KeypadRow().add(btn)
kb = ChatKeypad(resize_keyboard=True).add(row)
bot.send_message(message.chat_id, "Welcome!", chat_keypad=kb)
@bot.message_handler()
def handle_all(message: Message):
if message.aux and message.aux.button_id == "hello_btn":
bot.reply_to(message, "Hello! Keypad removed.", chat_keypad=ChatKeypadRemove())
else:
bot.reply_to(message, f"You said: {message.text}")
@bot.message_handler(content_types=["file"])
def file_handler(message: Message):
url = bot.get_file(message.file.id)
data = bot.download_file(url)
with open("received_file", "wb") as f:
f.write(data)
bot.send_message(message.chat_id, "File saved!")
if __name__ == "__main__":
bot.set_commands(
BotCommand("start", "Start the bot")
)
bot.polling()
Documentation & Community
- Rubika Channel: PyRubikaBotAPI
- GitHub Repository: alireza-sadeghian/PyRubikaBotAPI
More examples and detailed API docs are available there.
Author
Alireza Sadeghian
📧 alireza.amid110@gmail.com
License: MIT
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
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 pyrubikabotapi-1.0.2.tar.gz.
File metadata
- Download URL: pyrubikabotapi-1.0.2.tar.gz
- Upload date:
- Size: 21.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a60235d0b4af0ca57fe35db33d1d1bdb2f78109e5384018d886db8fa7a3548d
|
|
| MD5 |
1f329f850e38c6812277f447368fd9d9
|
|
| BLAKE2b-256 |
f69b3bef94c7458bee5c0746608c9fcfdd02079dec1a0ef627ecc29e47e33836
|
File details
Details for the file pyrubikabotapi-1.0.2-py3-none-any.whl.
File metadata
- Download URL: pyrubikabotapi-1.0.2-py3-none-any.whl
- Upload date:
- Size: 23.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a72b4f8ea3327f226f1b6a6a64a989c58f1609ed68f9ce893b2064eada8582c7
|
|
| MD5 |
6b0f8c2cb41191660f619ea3d0048750
|
|
| BLAKE2b-256 |
2d01a3c2657cc01a1da71182c9d7292d7f99246c572ac0582d7e8d2aa90ddd22
|