Hi guys
I want to write a telegram bot with gpt in python but I keep getting an error about the old GPT interface, can anyone help me?
import os
import logging
from dotenv import load_dotenv, find_dotenv
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes, ConversationHandler
import openai
load_dotenv(find_dotenv())
TELEGRAM_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
openai.api_key = os.getenv('OPENAI_API_KEY')
PASSWORD_STATE, MAIN_STATE, CHOOSE_GPT = range(3)
PASSWORD = 'password'
authenticated_users = set()
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
user_id = update.message.from_user.id
if user_id in authenticated_users:
await update.message.reply_text('Jesteś już zalogowany. Wybierz wersję GPT: /gpt35, /gpt4, /gpt4o')
else:
await update.message.reply_text('Proszę podać hasło:')
return PASSWORD_STATE
async def password(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
user_id = update.message.from_user.id
if update.message.text == PASSWORD:
authenticated_users.add(user_id)
await update.message.reply_text('Hasło prawidłowe! Bot odblokowany. Wybierz wersję GPT: /gpt35, /gpt4, /gpt4o')
return MAIN_STATE
else:
await update.message.reply_text('Nieprawidłowe hasło, spróbuj ponownie.')
return PASSWORD_STATE
async def choose_gpt(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
user_id = update.message.from_user.id
if user_id in authenticated_users:
if update.message.text.lower() == '/gpt35':
context.user_data['gpt_version'] = 'gpt-3.5-turbo'
await update.message.reply_text('Wybrano GPT-3.5. Możesz zadawać pytania.')
elif update.message.text.lower() == '/gpt4':
context.user_data['gpt_version'] = 'gpt-4'
await update.message.reply_text('Wybrano GPT-4. Możesz zadawać pytania.')
async def chatgpt(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
user_id = update.message.from_user.id
if user_id in authenticated_users:
model = context.user_data.get('gpt_version', 'gpt-3.5-turbo')
prompt = update.message.text
response = openai.ChatCompletion.create(
model=model,
messages=[{"role": "user", "content": prompt}]
)
await update.message.reply_text(response.choices[0]['message']['content'])
else:
await update.message.reply_text('Musisz wpisać hasło, aby używać bota. Użyj /start aby zacząć.')
async def generate_image(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
user_id = update.message.from_user.id
if user_id in authenticated_users:
prompt = update.message.text
response = openai.Image.create(
prompt=prompt,
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
await update.message.reply_photo(photo=image_url)
else:
await update.message.reply_text('Musisz wpisać hasło, aby używać bota. Użyj /start aby zacząć.')
async def handle_file(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
user_id = update.message.from_user.id
if user_id in authenticated_users:
file = update.message.document
file_id = file.file_id
new_file = await context.bot.get_file(file_id)
file_path = f"downloads/{file.file_name}"
await new_file.download_to_drive(file_path)
await update.message.reply_text(f'Plik zapisany: {file_path}')
else:
await update.message.reply_text('Musisz wpisać hasło, aby używać bota. Użyj /start aby zacząć.')
def main() -> None:
application = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
PASSWORD_STATE: [MessageHandler(filters.TEXT & ~filters.COMMAND, password)],
MAIN_STATE: [
CommandHandler('gpt35', choose_gpt),
CommandHandler('gpt4', choose_gpt),
CommandHandler('gpt4o', choose_gpt),
MessageHandler(filters.TEXT & ~filters.COMMAND, chatgpt),
],
CHOOSE_GPT: [MessageHandler(filters.TEXT & ~filters.COMMAND, choose_gpt)],
},
fallbacks=[CommandHandler('start', start)]
)
application.add_handler(conv_handler)
application.add_handler(CommandHandler('generate_image', generate_image))
application.add_handler(MessageHandler(filters.Document.ALL, handle_file))
application.run_polling()
if __name__ == '__main__':
os.makedirs('downloads', exist_ok=True)
main()