Advertisement
askanton

Як інтегрувати #chatgpt у #telgram | Пишемо telegram бот на #python

Dec 10th, 2022
1,733
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 1 0
  1. import telebot
  2. import openai
  3.  
  4. bot = telebot.TeleBot("***********************************************************")
  5. openai.api_key = "sk-*******************************************"
  6.  
  7. @bot.message_handler(content_types=['text'])
  8. def lalala(message):
  9.     print(message.chat.title, message.chat.username)
  10.     if message.chat.id == -1001411081071:
  11.     #print(message.text)
  12.         if "@mbajan_bot" in message.text:
  13.             message.text = (message.text).replace("@mbajan_bot ", "")
  14.             #print(message.text)
  15.             response = openai.Completion.create(model="text-davinci-003", prompt=message.text, max_tokens=1000)
  16.             full_response = response['choices'][0]['text']  # Use the text property of the first element of the choices list to access the full response
  17.             lines = full_response.splitlines()  # Split the response into individual lines
  18.             for line in lines:  # Iterate over the lines
  19.                 try:
  20.                     #print(line)
  21.                     bot.send_message(message.chat.id, line)  # Send each line back to the user as a separate message
  22.                 except Exception as e:
  23.                     print(e)
  24.     else:
  25.         bot.send_message(message.chat.id, "Вибачте, доступний тільки в группі https://t.me/python_for_dummies")
  26.  
  27. bot.polling(none_stop=True, interval=0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement