diff --git a/bot.py b/bot.py index 6903638..5130df9 100644 --- a/bot.py +++ b/bot.py @@ -164,26 +164,15 @@ def send_voice_text(call): transcription_hash = call.data.split(':')[1] voice_text = transcription_dict.get(transcription_hash) + answer = bot.reply_to(call.message, "Подождите, я думаю...") + if voice_text: # Generate a response using the ChatGPT model (response_text, reason, cost) = generate_chat_response(voice_text) print(response_text) - # Send the response back to the user with markdown formatting - try: - ans = bot.send_message(chat_id=call.message.chat.id, text=response_text , parse_mode='Markdown') - if reason != "stop": - bot.reply_to(ans, f"Finish reason: {reason} \nCost: {cost}") - except Exception as e: - print(e) - bot.send_message(chat_id=call.message.chat.id, text="Что-то пошло не так c ответом. Пробую другой метод...") - - try: - bot.send_message(chat_id=call.message.chat.id, text=response_text) - except Exception as e: - print(e) - bot.send_message(chat_id=call.message.chat.id, text="Что-то пошло не так c ответом") + send_response(call.message, answer, response_text, reason, cost) else: bot.send_message(chat_id=call.message.chat.id, text="Не удалось получить текст голосового сообщения.") @@ -202,16 +191,27 @@ def handle_message(message): print(response_text) # Send the response back to the user with markdown formatting + send_response(message, answer, response_text, reason, cost) + + +def send_response(message, answer, response_text, reason, cost): try: - ans = bot.edit_message_text(chat_id=message.chat.id, message_id=answer.message_id, text=f"Finish reason: {reason} \nCost: {cost}" ) + if "stop" not in reason: + ans = bot.edit_message_text(chat_id=message.chat.id, message_id=answer.message_id, text=f"Finish reason: {reason} \nCost: {cost}" ) + bot.reply_to(ans, response_text, parse_mode='Markdown') + + else: + bot.delete_message(chat_id=message.chat.id, message_id=answer.message_id) + bot.reply_to(message, text=response_text, parse_mode='Markdown') + # Send response text with reason and cost formated - bot.reply_to(ans, response_text, parse_mode='Markdown') + except Exception as e: print(e) bot.reply_to(message, "Что-то пошло не так c ответом. Пробую другой метод...") try: - bot.edit_message_text(chat_id=message.chat.id, message_id=answer.message_id, text=response_text) + bot.reply_to(message, text=response_text) except Exception as e: print(e) bot.reply_to(message, "Что-то пошло не так c ответом...")