This media is not supported in your browser
VIEW IN TELEGRAM
Do it in a couple of minutes: just install the python-telegram-bot library, add your #OpenAI #API key and bot token, and the bot will start replying to all messages using #ChatGPT.
from telegram import Update
from telegram.ext import ApplicationBuilder, MessageHandler, filters, ContextTypes
from openai import OpenAI
Specify your keys
OPENAI_API_KEY = "sk-..."
TELEGRAM_TOKEN = "123456789:ABC..."
client = OpenAI(api_key=OPENAI_API_KEY)
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_text = update.message.text
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": user_text}]
)
await update.message.reply_text(response.choices[0].message.content)
app = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
app.run_polling()
https://t.iss.one/CodeProgrammer
Please open Telegram to view this post
VIEW IN TELEGRAM
❤4👍1