我正在写一个带有 OpenAI 集成的电报机器人
在使用它之前,用户通过一个简单的注册,用户留下他的 api_key。哪个存储在数据库中。我无法理解我的 FAIL0 错误的原因。我确定我的密钥已正确保存并且我可以看到它。但我认为我的导入有问题
@connection_func
def search_api(connection, user_id):
"""Searching for the api_key of a user in the database"""
with connection.cursor() as cursor:
select_query = "SELECT api_key FROM `users_id` WHERE id = %s"
cursor.execute(select_query, (user_id,))
result = cursor.fetchone()
if result is not None:
api_key = result[0]
return api_key
else:
print(f"User with id {user_id} not found in database")
return None
@bot.message_handler(content_types='text')
def sender(message):
user_id = message.from_user.id
api_key = search_api(user_id)
if api_key is not None:
openai.api_key = api_key
try:
# making requests to OpenAI using the API key
bot.send_chat_action(message.chat.id, 'typing')
# get a response from OpenAI
response_text = getText(message.text)
# send reply
bot.send_message(message.chat.id, response_text)
bot.send_chat_action(message.chat.id, 'typing')
except Exception as e:
# output the error to the console
print(f"OpenAI error: {e}")
bot.send_message(message.chat.id, text="OpenAI error. Please try again later.")
else:
bot.send_message(message.chat.id, text="Error")
我试图通过错误处理检查错误是什么,但我仍然得到结果 FAIL0