DEV Community

Cover image for Telegram AI Agent step by step (my first experience)
utestwalter
utestwalter

Posted on

Telegram AI Agent step by step (my first experience)

Hi everyone!

I love science, and recently I explored how AI could help me stay updated with scientific news — automatically and on schedule. To do this, I built an AI agent as a Telegram bot that performs scheduled web searches and sends links directly to my Telegram channel. Additionally, this agent can send me any reminder.

It was my first real experiment with building an autonomous AI agent, and it turned out to be both fun and practical.

Here’s the tech stack I used:

  • Telegram Chatbot
  • OpenAI Assistant API
  • APScheduler
  • SerpAPI

What can this AI agent do?

  • Composes a search query based on user input
  • Performs web search on schedule
  • Sends results to a Telegram channel
  • Delivers reminders at set times
  • Works fully autonomously

Step-by-Step Implementation

Step 1 – Create a Telegram Bot

  • Open Telegram and message @BotFather/newbot
  • Choose a name and username
  • Save the provided HTTP API Token — it will be used in the code

Step 2 – Get your API Key

This depends on the LLM you plan to use. I chose ChatGPT, so I registered at OpenAI, generated an API key, and stored it securely.

Step 3 – Set up the Project in your IDE

Once you have these three:

  • Telegram bot
  • Telegram token
  • OpenAI API key

…you can start building!

I used VS Code and Python 3.10. Project structure:

  • bot_prompt_setup.py — Telegram bot functionality
  • assistant_core.py — LLM and web search logic
  • .env — stores secrets (gitignored)
  • prompts/ — saved task definitions

Step 4 – Write the Python Scripts

I used an AI assistant to help write the core logic (you can find the full code on GitHub — link below). The main functionality is:

  • bot_prompt_setup.py: bot UI, prompt generation, file creation, scheduling, task management
  • assistant_core.py: handles communication with OpenAI and SerpAPI

Security tips:

  • API keys and tokens are stored in .env (not pushed to GitHub)
  • Bot access is limited to a whitelist of Telegram user IDs

Step 5 – Run & Test the Bot

Run bot_prompt_setup.py and test the workflow end-to-end:

  • Start the bot
  • Create a task
  • Confirm the task runs correctly

I tested various task frequencies:

  • daily, weekly, monthly, once
  • with and without web search

You can also fine-tune UX: rename buttons, add hints, translate the interface.

The most important part: prompt design.

I use 4 different prompts for LLM to:

  • Parse and prepare task-specific instructions
  • Format the results nicely for the user

Step 6 – Deploy to Production

After testing and bug fixing, I deployed the agent based on the criteria:

  • Code should be accessible to other developers
  • Keys and tokens must remain secure
  • Support for always-on execution
  • Reasonable service cost

That’s why I chose GitHub + PythonAnywhere as a hosting solution.


Screenshots

Start

Image description

Search task

Image description

Reminder

Image description

Links

So, what exactly does my AI agent do?

Performs autonomous web searches, formats the results, and sends them (or reminders) on a schedule — all inside Telegram.

Convenient? Yes.
Perfect? Not yet.

Currently, the LLM controls logic and formatting, but not the search itself. As a result, I can get links that are not always relevant. That’s why for the next version I’d like to add:

  • Automatic HTML parsing
  • PDF extraction from pages
  • Clicking links and analyzing embedded content
  • Summarizing & translating content on schedule

That’s a more advanced challenge. If you’ve built something like this — I’d love to hear your experience: what parser and web-search API work best?

Thanks for reading!

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.