0
import telebot
import requests
import time

TELEGRAM_TOKEN = "my-token"

bot = telebot.TeleBot(TELEGRAM_TOKEN)

@bot.message_handler(commands=['search'])

def handle_search(message):

    # extract the search word from the message text
    search_word = message.text.split(" ", 1)[1]
    headers = {
        'Authorization': 'Bearer sk-token',
    }
   
    # prepare the request payload
    json_data = {

        'model': 'text-davinci-003',
        'prompt': f'{search_word}',
        'temperature': 0.8,
        'max_tokens': 2000,
    }

    # send the request to OpenAI API

    response = requests.post('https://api.openai.com/v1/completions', headers=headers, json=json_data).json()
    
    # extract the response text

    response_text = response['choices'][0]['text']

    
    # send the response text as a message
    bot.send_message(message.chat.id, response_text, reply_to_message_id=message.message_id)

def run():

    while True:
        try:
            bot.polling(none_stop=True)
        except Exception as e:
            # log the error
            print(f"Error occurred: {e}")

            # wait for 5 seconds before polling again
            time.sleep(5)

if __name__ == '__main__':
    run()

When I turn it on, it appears like this

File "main.py", line 6, in @bot.message_handler(commands=['search']) AttributeError: 'TeleBot' object has no attribute 'message_handler'

Rubén
  • 34,714
  • 9
  • 70
  • 166

0 Answers0