DEV Community

Cover image for I built a self-hosted Telegram AI assistant to keep my Gemini API key private. It's now open-source for everyone.
kobaltGIT
kobaltGIT

Posted on

I built a self-hosted Telegram AI assistant to keep my Gemini API key private. It's now open-source for everyone.

The Problem: Where Does My Data Go?

I'm a huge fan of Google's Gemini API, but I've always had one major concern when using third-party Telegram bots: "What happens to my API key and conversation history?" Handing over a key linked to my billing account to an unknown, closed-source service is a huge risk.

I wanted a solution where I was in complete control of my own data. Since I couldn't find a project that met my standards, I built it myself.

Today, I'm excited to share MyGemini. It's a Telegram bot that offers two distinct paths: one for users who prioritize maximum privacy and another for those who value convenience.


Two Paths - Choose Your Own:

Path 1: Maximum Privacy (Self-Host It)

For those who, like me, don't want to trust anyone with their data. The entire project is open-source under an MIT license.

>> Check out the code on GitHub: https://github.com/kobaltgit/MyGemini

You can run an exact copy of this bot on your own server or even a local machine. When you do this:

  • Your API key never leaves your system.
  • All conversation history is stored in your local database.
  • You have 100% control over the code and your data.

This is the ideal choice for developers and anyone serious about privacy.

Path 2: Convenience & Quick Start (Use My Public Bot)

If you're comfortable with open-source projects and just want to start using a powerful AI assistant right now, I'm hosting a public instance on my server.

>> Try MyGemini Bot Now: https://t.me/mgem_bot <<

In this version, you will need to provide your Google AI API key, but with a crucial difference: you're doing so with the knowledge that the bot is fully open-source. You can go to the GitHub repo at any time and verify that the code does exactly what it promises: securely encrypts your key and uses it only for sending requests to Google.

Key Bot Features:

  • Multi-Dialog Context: Create independent chats for different topics (work, hobbies, learning) so their contexts never get mixed up.
  • Custom "Personas": Instantly transform the bot into a "Python Expert," a "Historian," or a "Copywriter" with a single click.
  • Image & Voice Understanding: Send a picture or record a voice note—the bot will understand and respond.
  • Live Web Search: Can use Google Search to provide up-to-date answers on current events.

A Look Under the Hood (For the Tech-Savvy)

I built this with Python 3.10+ and focused on creating a clean and scalable async architecture.

  • Async All The Way: The bot is built on asyncio, using the async version of pyTelegramBotAPI and aiohttp for non-blocking requests to the Gemini REST API.
  • Clean Configuration: Secrets are managed via .env, and there's a detailed logging.yaml for granular control over logs.
  • Non-Blocking Database: It uses a simple sqlite3 database, but all operations are run in a separate thread via asyncio.to_thread to keep the event loop free.
  • Robust Message Handling: I'm using MarkdownTextSplitter from LangChain to safely chunk long messages and telegramify-markdown to ensure everything is correctly escaped for Telegram's MarkdownV2 parser.

This project has been a fantastic learning experience, and it's now a tool I use daily.

Whether you just want a powerful AI assistant in your Telegram or you're a developer looking for a solid project to fork and build upon, I'd love for you to check it out.

Any feedback is welcome, and a star on GitHub would be amazing! Thanks for reading.

Top comments (0)