Python | Machine Learning | Coding | R
66.8K subscribers
1.23K photos
86 videos
151 files
885 links
Help and ads: @hussein_sheikho

Discover powerful insights with Python, Machine Learning, Coding, and R—your essential toolkit for data-driven solutions, smart alg

List of our channels:
https://t.iss.one/addlist/8_rRW2scgfRhOTc0

https://telega.io/?r=nikapsOH
Download Telegram
This media is not supported in your browser
VIEW IN TELEGRAM
🖥 Want your Telegram bot to respond using ChatGPT?

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
5👍1