docs
Automation

Telegram bot

Markdown

Bots · OpenAI Chat

Full guide
FormatWhere to set it up
OpenAI ChatTelegram frameworks have no built-in LLM layer: the message handler calls the OpenAI SDK with the Ailoot base_url, or the Anthropic SDK with base_url without /v1

Setup

Copy the configuration and replace sk-... with your key from the Ailoot Dashboard. Prefer storing the key in the environment variable AILOOT_API_KEY, if the application supports it.

Before starting, request GET /v1/models with your Ailoot key. Choose an exact model ID whose methods include POST /v1/chat/completions, then use that ID in the configuration. For agents, also check tool support and the features required by your client version.

Save the Python code as ailoot-telegram-bot.py. Install the dependencies, set both environment variables, then run the file. Send Hello to your bot and check for a text reply. If the request fails, keep its Request ID and check the original result before sending another generation.

python3 -m pip install "aiogram>=3,<4" openai

ailoot-telegram-bot.py

import asyncio
import os
from aiogram import Bot, Dispatcher, F
from aiogram.types import Message
from openai import AsyncOpenAI, APIError

async def main():
    dp = Dispatcher()
    async with AsyncOpenAI(
        base_url="https://api.ailoot.ai/v1",
        api_key=os.environ["AILOOT_API_KEY"],
        max_retries=0,
    ) as llm:
        @dp.message(F.text)
        async def reply(message: Message):
            try:
                result = await llm.chat.completions.create(
                    model="claude-sonnet-5-5",
                    messages=[{"role": "user", "content": message.text}],
                )
                content = result.choices[0].message.content or "—"
                for start in range(0, len(content), 4000):
                    await message.answer(content[start:start + 4000])
            except APIError as error:
                await message.answer(
                    f"{type(error).__name__}; Request ID: "
                    f"{getattr(error, 'request_id', None) or '—'}"
                )
        bot = Bot(token=os.environ["TELEGRAM_BOT_TOKEN"])
        await dp.start_polling(bot)

if __name__ == "__main__":
    asyncio.run(main())
export AILOOT_API_KEY="sk-..."
export TELEGRAM_BOT_TOKEN="<TELEGRAM_BOT_TOKEN>"
python3 ailoot-telegram-bot.py

Example model: claude-sonnet-5-5, deepseek-v4-pro, qwen3.8-flash. Choose any other model from the catalog — check that it supports the format you need.

Good to know

Test

  • For the OpenAI format, the URL is https://api.ailoot.ai/v1 (or the full path if the application asks for a full URL).
  • Ask a short question. If you get an error, check the code and Request ID, then consult Errors and limits.
  • 404 immediately after setup almost always means the URL is wrong: an extra or missing /v1.
  • 402 — insufficient balance for the reserve. Agents reserve more than chat: why.

Application documentation: https://github.com/openai/openai-python

Still have questions? Send your Request ID and a description — we'll help you troubleshoot.Contact support