Retour à l'accueil

obabot pour les bots Telegram et Max

obabot — bibliothèque Python asynchrone pour lancer des bots sur Telegram et Max avec une seule API compatible aiogram. Prend en charge FSM, filtres, claviers. Migration minimale, mode test sans tokens.

Lancement d'un bot sur Telegram et Max avec obabot
Advertisement 728x90

# obabot : Bibliothèque universelle pour bots Telegram et Max

La bibliothèque obabot vous permet de déployer un seul bot Python asynchrone sur Telegram et Max simultanément. Son API est entièrement compatible avec aiogram 3.x : elle utilise le aiogram natif pour Telegram et un adaptateur pour Max qui convertit les événements en objets compatibles. L'installation se fait en une ligne : pip install obabot aiogram>=3.0.0 umaxbot>=0.1.7.

La fonction principale create_bot() prend les tokens des plateformes et retourne un tuple (bot, dispatcher, router). Les gestionnaires de messages s'écrivent de manière uniforme, indépendamment de la plateforme.

Exemples d'initialisation

Pour Telegram :

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

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

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

await dp.start_polling(bot)

Pour Max :

from obabot import create_bot
from obabot.filters import Command

bot, dp, router = create_bot(max_token="YOUR_MAX_TOKEN")

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

await dp.start_polling(bot)

Pour les deux plateformes en parallèle :

from obabot import create_bot
from obabot.filters import Command

bot, dp, router = create_bot(
    tg_token="YOUR_TG_TOKEN",
    max_token="YOUR_MAX_TOKEN"
)

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

await dp.start_polling(bot)

L'attribut message.platform retourne "telegram" ou "max" pour une logique spécifique à la plateforme.

Google AdInline article slot

Migration depuis aiogram

Passer d'aiogram 3.x pur nécessite des changements minimes : il suffit de remplacer les imports et d'utiliser create_bot() à la place de Bot, Dispatcher et Router séparés.

Code aiogram original :

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("Salut !")

await dp.start_polling(bot)

Après migration :

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("Salut !")

await dp.start_polling(bot)

Le reste de votre code — y compris FSM, filtres et claviers — reste exactement identique.

Interface Message et méthodes de base

Tous les objets Message partagent une interface unifiée :

  • message.text — texte
  • message.from_user — expéditeur
  • message.chat — chat
  • message.message_id — ID
  • message.platform — plateforme

Méthodes :

  • await message.answer("Réponse")
  • await message.reply("Répondre au message")
  • await message.delete()
  • await message.edit_text("Nouveau texte")

FSM : machine d'états

La FSM fonctionne exactement comme dans 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("Quel est votre nom ?")

@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("Quel âge avez-vous ?")

Claviers inline et filtres

Création d'un clavier :

from obabot.types import InlineKeyboardMarkup, InlineKeyboardButton

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

await message.answer("Choisissez-en un :", reply_markup=keyboard)

Filtres :

  • @router.message(Command("start", "help")) — plusieurs commandes
  • @router.message(F.text.startswith("!")) — préfixe
  • @router.message(F.photo) — photos
  • @router.callback_query(F.data == "click") — callbacks

Tests sans tokens

test_mode=True évite tous les appels réseau :

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

Compatible Python 3.10–3.14, aiogram 3.0.0–3.24. Couverture complète des tests unitaires.

Points clés

  • Code unique : Un seul gestionnaire pour Telegram et Max — pas d'adaptateurs nécessaires.
  • Compatibilité totale : 100 % API aiogram 3.x + attribut platform.
  • FSM et filtres : Support complet des états, claviers inline, filtres magiques F.
  • Migration facile : Remplacez 2 lignes d'import et l'initialisation.
  • Tests : Mode isolé sans appels API.

— Editorial Team

Advertisement 728x90

Lire ensuite