OpenAI Python Bot

OpenAI Python Bot


# Copy this to ai.py

# pip install telebot openai pyTelegramBotAPI

# python ai.py


import telebot

import openai


openai.api_key = 'ZZZ'

bot = telebot.TeleBot("XXX:YYY")


try:


  @bot.message_handler(func=lambda _: True)

  def handle_message(message):

    response = openai.Completion.create(

      model="text-davinci-003",

      prompt=message.text,

      temperature=0.5,

      max_tokens=1000,

      top_p=1.0,

      frequency_penalty=0.5,

      presence_penalty=0.0,

    )


    try: # add try catch here to ignore errors from this code


      bot.send_message(chat_id=message.from_user.id, text=response['choices'][0]['text'])


    except Exception as e: # catch any errors and print them out


      print(e)


  bot.polling()

except Exception as e: # catch any errors and print them out


  print(e)


Report Page