🔥 Live offer: limited-time discount on the Pro plan Grab the deal
BotifyHost
Tutorials

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

Create a smart AI chatbot on Telegram using OpenAI GPT-4o API. Complete code example with memory handling and 24/7 hosting instructions.

#AI#ChatGPT#OpenAI#Python

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!
Ready to put this into practice? Upload your bot to BotifyHost and it stays online 24/7. Try the full dashboard free for 3 days, or host 1 bot free from our Android app.
BotifyHost
Written by the BotifyHost Team

We run Telegram and Discord bots for a living, and write up the fixes we use every day. Questions about this guide? Ask us on Telegram.

Your bot, online
while you sleep.

Deploy free in under a minute. Upgrade only when you need more power.

Need Help? Chat on Telegram