Home / Blog & Tutorials / Tutorials

Build & Deploy a ChatGPT / Claude AI Telegram Bot with Python in 10 Minutes

AI chatbots are currently the most popular bot category on Telegram. Whether you want to provide customer support, draft essays, generate code, or summarize long documents, combining the Telegram Bot API with OpenAI or Anthropic models is remarkably straightforward.

Full Python Implementation

# ai_bot.py
import os
import openai
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters

openai.api_key = os.environ.get("OPENAI_API_KEY")
BOT_TOKEN = os.environ.get("BOT_TOKEN")

# Simple in-memory history dictionary
user_conversations = {}

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    user_id = update.effective_user.id
    user_conversations[user_id] = [{"role": "system", "content": "You are a helpful AI assistant hosted 24/7 on BotifyHost."}]
    await update.message.reply_text("👋 Hello! I am your AI assistant. Ask me anything!")

async def handle_ai_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
    user_id = update.effective_user.id
    user_prompt = update.message.text
    
    if user_id not in user_conversations:
        user_conversations[user_id] = [{"role": "system", "content": "You are a helpful AI assistant."}]
    
    # Append user prompt
    user_conversations[user_id].append({"role": "user", "content": user_prompt})
    
    # Send typing indicator
    await update.message.chat.send_action("typing")
    
    try:
        response = openai.chat.completions.create(
            model="gpt-4o-mini",
            messages=user_conversations[user_id],
            max_tokens=600
        )
        ai_reply = response.choices[0].message.content
        user_conversations[user_id].append({"role": "assistant", "content": ai_reply})
        await update.message.reply_text(ai_reply)
    except Exception as e:
        await update.message.reply_text(f"⚠️ Error generating response: {str(e)}")

if __name__ == "__main__":
    app = ApplicationBuilder().token(BOT_TOKEN).build()
    app.add_handler(CommandHandler("start", start))
    app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_ai_message))
    print("AI Telegram Bot is active!")
    app.run_polling()

Deploying in 2 Steps

  1. Create a requirements.txt with python-telegram-bot and openai.
  2. Upload your code to BotifyHost, configure your BOT_TOKEN and OPENAI_API_KEY, and hit Run. Your AI bot will now serve users 24/7!
BotifyHost

Written by Alex Johnson

Specialist at BotifyHost Cloud Infrastructure. Passionate about Telegram MTProto protocol, Python asynchronous concurrency, and building high-performance hosting clusters for developers worldwide.

Host Your Telegram Bot 24/7 on BotifyHost

Say goodbye to laptops running overnight and sleeping web dynos. Deploy your bot in 30 seconds with 99.99% uptime, built-in code editor, and live logs.

Need Help? Chat on Telegram