import os import telebot import openai from dotenv import load_dotenv # Load the environment variables from the .env file load_dotenv() # Load the OpenAI API key from the environment variable openai.api_key = os.environ.get("OPENAI_API_KEY") # Load the Telegram bot token from the environment variable bot = telebot.TeleBot(os.environ.get("TELEGRAM_BOT_TOKEN")) # Define a function to generate a response using the ChatGPT model def generate_response(message_text): # Call the OpenAI API to generate a response response = openai.Completion.create( engine="text-davinci-003", prompt=message_text, max_tokens=1024, n=1, stop=None, temperature=0.7, ) # print all the response # print(response) # Extract the generated text from the response and return it return response.choices[0].text.strip() # Define a handler function for incoming messages @bot.message_handler(func=lambda message: True) def handle_message(message): print(message.from_user.id, message.from_user.first_name, message.from_user.last_name, message.from_user.username, message.text) # check if the message comming from user id 210098655 Коля or 40196122 Я if message.from_user.id != 210098655 and message.from_user.id != 40196122: # Send a message to the user bot.reply_to(message, "Я не узнаю тебя, ты кто?") return # send confirm message to user # bot.reply_to(message, "Опять работать...") # send typing message to user bot.send_chat_action(message.chat.id, 'typing') # Generate a response using the ChatGPT model response_text = generate_response(message.text) print(response_text) # Send the response back to the user bot.reply_to(message, response_text) # Start the bot and listen for incoming messages bot.polling()