Create a ChatGPT Telegram Bot with Python
Job to be done: Create a custom Telegram bot that integrates with OpenAI's ChatGPT API for answering questions
🇳🇬 Ways to use this in Nigeria
Ideas to get you started, adapt to your situation.
- Student
Build a private Telegram bot to quickly get explanations for complex programming concepts or research topics for your CSC301 assignments, without needing to open a browser.
- Entrepreneur
Set up a Telegram bot for your online community or customer support group to instantly answer frequently asked questions about your product or service, freeing up your time.
- 9-5 employee
Deploy a private Telegram bot for your team to quickly retrieve information from internal documents or answer common IT support questions, reducing email clutter.
What you’ll get
Your own Telegram bot that answers messages using ChatGPT, so you (and anyone you allow) can chat with AI right inside Telegram. You run a ready-made open-source project, so you are configuring and launching it rather than writing it from scratch. It is intermediate: you use a terminal, two secret keys, and Docker, but each step is spelled out.
Tools you need
- Telegram (free): the messaging app where your bot lives. You create the bot through Telegram’s official “BotFather”.
- OpenAI API (paid): provides ChatGPT’s answers. It is pay-as-you-go; you need to add billing (a card) and you get an “API key”, a secret that lets your bot use it.
- Python (free): the language the project is written in (version 3.9 or newer).
- Docker (freemium): packages the bot so it runs the same on any computer. Free for individual use.
Steps
-
Create the bot in Telegram: open Telegram, search for BotFather, start a chat, and send
/newbot. Follow the prompts to name it. BotFather gives you a bot token (a secret password for your bot). Copy and keep it safe. -
Get your OpenAI API key: sign in at platform.openai.com, open the API keys section, and create a new secret key. Copy it (you only see it once) and keep it safe.
-
Download the project: in your terminal, clone the repo and enter it:
git clone https://github.com/n3d1117/chatgpt-telegram-bot.git cd chatgpt-telegram-bot -
Add your keys to the config file: copy
.env.exampleto a new file named.env, open it, and fill in your two keys. The.envfile holds your secrets so they stay out of the code:TELEGRAM_BOT_TOKEN=your_telegram_bot_token OPENAI_API_KEY=your_openai_api_key ADMIN_USER_IDS=your_telegram_user_id ALLOWED_TELEGRAM_USER_IDS=your_telegram_user_id(Setting the user IDs limits who can use the bot, so strangers do not spend your OpenAI credits.)
-
Install the Python dependencies:
pip install -r requirements.txtYou should see packages install without errors.
-
Run the bot with Docker: make sure Docker is installed and running, then in the project folder:
docker-compose upDocker builds and starts the bot; you should see logs saying it is running and connected. (On newer Docker, the command is
docker compose up.) -
Chat with your bot: in Telegram, search for your bot’s username, open it, and send a message. You should get a reply generated by ChatGPT.
Original source
Based on the chatgpt-telegram-bot project by n3d1117 on GitHub, a Python bot that connects Telegram to OpenAI’s ChatGPT.
Notes & variations
- Cost: Telegram and Python are free; Docker is free for individuals. The OpenAI API is pay-as-you-go (new accounts may get small trial credits, but you must add billing to keep using it).
- Common pitfall: forgetting to rename
.env.exampleto.env, or pasting a key wrong. If the bot will not start, recheck those values first. - Tip for better results: to use a different model, set the
OPENAI_MODELvalue in your.env(your OpenAI plan must have access to that model).