Powrót do strony głównej

obabot dla botów Telegram i Max

obabot — asynchroniczna biblioteka Python do uruchamiania botów w Telegram i Max z jednolitym API zgodnym z aiogram. Obsługuje FSM, filtry, klawiatury. Migracja minimalna, tryb testowy bez tokenów.

Uruchomienie bota w Telegram i Max z obabot
Advertisement 728x90

obabot: uniwersalna biblioteka dla botów w Telegramie i Max

Biblioteka obabot umożliwia wdrożenie jednego asynchronicznego bota w Pythonie jednocześnie w Telegramie i Max. API jest w pełni kompatybilne z aiogram 3.x: dla Telegrama używany jest natywny aiogram, dla Max — adapter, który przekształca zdarzenia w kompatybilne obiekty. Instalacja wymaga jednej komendy: pip install obabot aiogram>=3.0.0 umaxbot>=0.1.7.

Główna funkcja create_bot() przyjmuje tokeny platform i zwraca krotkę (bot, dispatcher, router). Handlery wiadomości są pisane jednolicie, niezależnie od platformy.

Przykłady inicjalizacji

Dla Telegrama:

Google AdInline article slot
from obabot import create_bot
from obabot.filters import Command

bot, dp, router = create_bot(tg_token="TWÓJ_TG_TOKEN")

@router.message(Command("start"))
async def start(message):
    await message.answer(f"Witaj z {message.platform}!")

await dp.start_polling(bot)

Dla Max:

from obabot import create_bot
from obabot.filters import Command

bot, dp, router = create_bot(max_token="TWÓJ_MAX_TOKEN")

@router.message(Command("start"))
async def start(message):
    await message.answer(f"Witaj z {message.platform}!")

await dp.start_polling(bot)

Dla obu platform równocześnie:

from obabot import create_bot
from obabot.filters import Command

bot, dp, router = create_bot(
    tg_token="TWÓJ_TG_TOKEN",
    max_token="TWÓJ_MAX_TOKEN"
)

@router.message(Command("start"))
async def start(message):
    await message.answer(f"Witaj z {message.platform}!")

await dp.start_polling(bot)

Atrybut message.platform zwraca "telegram" lub "max" dla logiki zależnej od platformy.

Google AdInline article slot

Migracja z aiogram

Przejście z czystego aiogram 3.x minimalizuje zmiany: zamień importy i wywołaj create_bot() zamiast oddzielnych Bot, Dispatcher, Router.

Kod źródłowy w aiogram:

from aiogram import Bot, Dispatcher, Router
from aiogram.filters import Command
from aiogram.fsm.state import State, StatesGroup
from aiogram.fsm.context import FSMContext

bot = Bot(token="TOKEN")
dp = Dispatcher()
router = Router()
dp.include_router(router)

@router.message(Command("start"))
async def start(message):
    await message.answer("Witaj!")

await dp.start_polling(bot)

Po migracji:

Google AdInline article slot
from obabot import create_bot
from obabot.filters import Command
from obabot.fsm import State, StatesGroup, FSMContext

bot, dp, router = create_bot(tg_token="TOKEN")

@router.message(Command("start"))
async def start(message):
    await message.answer("Witaj!")

await dp.start_polling(bot)

Reszta kodu, włącznie z FSM, filtrami i klawiaturami, pozostaje niezmieniona.

Interfejs Message i podstawowe metody

Wszystkie obiekty Message implementują jednolity interfejs:

  • message.text — tekst
  • message.from_user — nadawca
  • message.chat — czat
  • message.message_id — ID
  • message.platform — platforma

Metody:

  • await message.answer("Odpowiedź")
  • await message.reply("Odpowiedz na wiadomość")
  • await message.delete()
  • await message.edit_text("Nowy tekst")

FSM: maszyna stanów

FSM jest identyczny jak w aiogram:

from obabot.fsm import State, StatesGroup, FSMContext

class Form(StatesGroup):
    name = State()
    age = State()

@router.message(Command("start"))
async def start(message, state: FSMContext):
    await state.set_state(Form.name)
    await message.answer("Jak się nazywasz?")

@router.message(Form.name)
async def process_name(message, state: FSMContext):
    await state.update_data(name=message.text)
    await state.set_state(Form.age)
    await message.answer("Ile masz lat?")

Klawiatury inline i filtry

Tworzenie klawiatury:

from obabot.types import InlineKeyboardMarkup, InlineKeyboardButton

keyboard = InlineKeyboardMarkup(inline_keyboard=[
    [
        InlineKeyboardButton(text="Przycisk 1", callback_data="btn1"),
        InlineKeyboardButton(text="Przycisk 2", callback_data="btn2"),
    ]
])

await message.answer("Wybierz:", reply_markup=keyboard)

Filtry:

  • @router.message(Command("start", "help")) — wiele komend
  • @router.message(F.text.startswith("!")) — prefiks
  • @router.message(F.photo) — zdjęcie
  • @router.callback_query(F.data == "click") — callback

Testowanie bez tokenów

Tryb test_mode=True wyklucza wywołania sieciowe:

bot, dp, router = create_bot(test_mode=True)

Wsparcie dla Python 3.10–3.14, aiogram 3.0.0–3.24. Pełne pokrycie testami jednostkowymi.

Co jest ważne

  • Jednolity kod: jeden handler dla Telegrama i Max, bez adapterów.
  • Kompatybilność: 100% API aiogram 3.x + atrybut platform.
  • FSM i filtry: pełne wsparcie stanów, klawiatur inline, magicznych filtrów F.
  • Migracja: zamiana 2 linijek importów i inicjalizacji.
  • Testowanie: izolowany tryb bez wywołań API.

— Editorial Team

Advertisement 728x90

Czytaj dalej