MyRelay

← Volver al cuaderno

Build an AI Telegram Bot Without Overspending

A useful AI Telegram bot does not need a complicated backend. Start with a private assistant that accepts text, sends one model request, and returns an answer. This walkthrough takes you through that design while keeping access, conversation length, and spending under your control.

Choose one small job for your first bot

Pick a narrow purpose, such as explaining programming concepts or turning study notes into questions. Write a short instruction describing that purpose and asking for concise answers. Avoid adding document uploads, web searches, or autonomous actions until the basic conversation works.

For your first version, plan to accept only private text messages from your own Telegram account. Choose Python or JavaScript, whichever you already know, and use a Telegram library that supports receiving messages and sending replies.

Register the bot and protect its credentials

Open Telegram, find the official BotFather account, and follow its bot creation process. Choose a name and username, then save the authentication token it provides. Treat that token like a password: someone holding it can act as your bot. Telegram lets you revoke it through BotFather if necessary. ([core.telegram.org](https://core.telegram.org/bots/tutorial?utm_source=openai))

Keep the Telegram token and your model API key in environment variables or your hosting service's secret storage. Do not paste them into source control, screenshots, or chat messages. Keep the two credentials separate so you can replace either independently.

Connect one model through MyRelay

Create a MyRelay account and choose a small initial budget for testing. Its personal relay brings model access under one key and one budget, with a record of token usage that helps you understand where your money goes. ([myapirelay.com](https://myapirelay.com/))

In your application, initialize an OpenAI-compatible client with your MyRelay key and the API base supplied by MyRelay. Select Gemini 2.5 Flash as an initial candidate, using the exact model identifier shown in your account rather than guessing it from the display name. Keep the model choice in one setting so you can change it without rewriting message handling.

Prepare a request containing your short instruction and the user's message. Set an explicit output limit supported by the selected model. For a study assistant, start with brief explanations rather than unrestricted essays.

Receive a message before calling the model

Start with long polling, which retrieves incoming updates without requiring a public webhook endpoint. Telegram supports polling and webhooks as alternative delivery methods; polling will not work while a webhook is configured. Use your library's polling mode and confirm that no webhook remains from earlier experiments. ([core.telegram.org](https://core.telegram.org/bots/faq?utm_source=openai))

In the message handler, check that the update contains text, belongs to a private chat, and comes from your allowed numeric user identifier. Reject anything else before making a model request. First test the handler with a fixed reply. Only connect the AI request after that simple exchange works.

Return answers without repeating paid requests

For each accepted message, collect the text, submit one model request, extract the answer, and send it back to the same chat. Use plain text initially. Split long replies into chunks comfortably below Telegram's message limit rather than attempting to send an unlimited response. ([core.telegram.org](https://core.telegram.org/bots/api?utm_source=openai))

Track processed update identifiers to reduce duplicate handling. Telegram provides these identifiers for recognizing repeated updates, and polling clients confirm updates by advancing the offset. Check how your library handles that confirmation instead of building a second polling loop. ([core.telegram.org](https://core.telegram.org/bots/api?utm_source=openai))

Add a timeout and return a brief failure message when the model is unavailable. Avoid unlimited retries, and keep a failed Telegram delivery from automatically triggering another model generation.

Add memory only within a fixed allowance

Begin without conversation history. Once single messages work, retain only a few recent exchanges per chat and enforce a total history allowance. Add a reset action that clears stored conversation data. Store histories separately, and avoid logging private message contents by default.

Enforce a daily request allowance in your application and limit simultaneous requests. A model instruction asking for short answers is useful, but it should accompany actual request limits rather than replace them.

Test costs before leaving the bot running

Try an ordinary question, an oversized message, a second message arriving immediately, an unauthorized account, and a simulated model failure. Confirm that rejected messages never reach the model. Review request costs in MyRelay, then estimate your expected daily usage from those observations. Deploy only after the replies, failure handling, and spending fit your small initial budget.

Más para leer