Source: https://ailoot.ai/docs/integrations/telegram-bot/
Canonical: https://ailoot.ai/docs/en/integrations/telegram-bot/
Language: en

[Automation](https://ailoot.ai/docs/en/integrations/#automation) 

# Telegram bot

[Markdown](https://ailoot.ai/docs/en/integrations/telegram-bot/index.md)

Bots · OpenAI Chat

| Format | Where to set it up |
| --- | --- |
| OpenAI Chat | Telegram 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 |

## [](https://ailoot.ai/docs/integrations/telegram-bot/#setup)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.

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

`ailoot-telegram-bot.py`

```python
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())
```

```bash
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](https://ailoot.ai/models/en/) — check that it supports the format you need.

## [](https://ailoot.ai/docs/integrations/telegram-bot/#notes)Good to know

Telegram frameworks have no built-in AI layer: configure the Ailoot URL in the OpenAI SDK inside the bot. With a zero balance, Ailoot returns a clear 402 error with a Request ID; show it to the bot user instead of crashing.

## [](https://ailoot.ai/docs/integrations/telegram-bot/#check)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](https://ailoot.ai/docs/en/errors/).
- 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](https://ailoot.ai/docs/en/billing/reserve/).

Application documentation: [https://github.com/openai/openai-python](https://github.com/openai/openai-python)

Still have questions? Send your Request ID and a description — we'll help you troubleshoot.[Contact support](mailto:support@ailoot.ai)

## Ailoot · The company and its project portfolio





Ailoot combines established operating experience, a large client base, stable service and ongoing KPI control.





The company and its project portfolio: millions of customers; monthly sales in the billions.





Ailoot brings that operating experience to API access for AI features in customer products, agents, applications and automations.
