feat: add language selection (ru and en)
This commit is contained in:
parent
c7f3e093b4
commit
f5c208ae2d
@ -80,24 +80,46 @@ class TelegramUserBot:
|
|||||||
response: str = self.gigachat_client.get_response(str(message.chat.id), command_arg)
|
response: str = self.gigachat_client.get_response(str(message.chat.id), command_arg)
|
||||||
self.logger.debug(f"Received response for chat_id={message.chat.id}")
|
self.logger.debug(f"Received response for chat_id={message.chat.id}")
|
||||||
|
|
||||||
# Stop typing animation
|
|
||||||
await client.send_chat_action(message.chat.id, ChatAction.CANCEL)
|
|
||||||
|
|
||||||
# Edit the processing message with the generated response
|
# Edit the processing message with the generated response
|
||||||
await processing_message.edit_text(response)
|
await processing_message.edit_text(response)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Log the exception details
|
|
||||||
self.logger.error(f"Error processing /ai command for chat_id={message.chat.id}: {e}", exc_info=True)
|
self.logger.error(f"Error processing /ai command for chat_id={message.chat.id}: {e}", exc_info=True)
|
||||||
|
await processing_message.edit_text("An error occurred while processing your request.")
|
||||||
# Stop typing animation in case of an error
|
finally:
|
||||||
|
# Stop indicating typing action
|
||||||
await client.send_chat_action(message.chat.id, ChatAction.CANCEL)
|
await client.send_chat_action(message.chat.id, ChatAction.CANCEL)
|
||||||
|
|
||||||
# Handle any errors and notify the user
|
def get_language(self, input_text: str) -> str:
|
||||||
await processing_message.edit_text("An error occurred while processing your request.")
|
"""
|
||||||
|
Determines the language for voice-to-text conversion based on the input parameter.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
input_text (str): The input parameter indicating the language.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The language code ('en' or 'ru').
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If an invalid language parameter is provided.
|
||||||
|
"""
|
||||||
|
language_params: dict[str, list[str]] = {
|
||||||
|
'en': ['en', 'eng', 'english'],
|
||||||
|
'ru': ['ru', 'rus', 'russian']
|
||||||
|
}
|
||||||
|
|
||||||
|
input_lower: str = input_text.lower()
|
||||||
|
for lang_code, aliases in language_params.items():
|
||||||
|
if input_lower in aliases:
|
||||||
|
return lang_code
|
||||||
|
raise ValueError(
|
||||||
|
"Invalid language parameter. Please use one of the following:\n" +
|
||||||
|
"\n".join(f"{lang_code}: {', '.join(aliases)}" for lang_code, aliases in language_params.items())
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def handle_voice_command(self, client: Client, message: Message) -> None:
|
async def handle_voice_command(self, client: Client, message: Message) -> None:
|
||||||
"""
|
"""
|
||||||
Handle the /voice command to convert a voice message to text.
|
Handle the /voice command to convert a voice message to text with optional language selection.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
client (Client): The Pyrogram Client instance.
|
client (Client): The Pyrogram Client instance.
|
||||||
@ -105,6 +127,14 @@ class TelegramUserBot:
|
|||||||
"""
|
"""
|
||||||
self.logger.info(f"Received /voice command from chat_id={message.chat.id}.")
|
self.logger.info(f"Received /voice command from chat_id={message.chat.id}.")
|
||||||
|
|
||||||
|
# Parse the language parameter (default to Russian)
|
||||||
|
command_parts: list[str] = message.text.split()
|
||||||
|
try:
|
||||||
|
language: str = self.get_language(command_parts[1]) if len(command_parts) > 1 else 'ru'
|
||||||
|
except ValueError as e:
|
||||||
|
await message.reply(str(e), quote=True)
|
||||||
|
return
|
||||||
|
|
||||||
# Check if the reply is to a voice message
|
# Check if the reply is to a voice message
|
||||||
if not (message.reply_to_message and message.reply_to_message.voice):
|
if not (message.reply_to_message and message.reply_to_message.voice):
|
||||||
self.logger.warning("The /voice command was not used in reply to a voice message.")
|
self.logger.warning("The /voice command was not used in reply to a voice message.")
|
||||||
@ -119,14 +149,16 @@ class TelegramUserBot:
|
|||||||
self.logger.info(f"Voice message downloaded to {file_path}.")
|
self.logger.info(f"Voice message downloaded to {file_path}.")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Attempt to convert voice to text
|
# Start typing animation
|
||||||
text: str = speech_recognition.convert_voice_to_text(file_path) # type: ignore
|
await client.send_chat_action(message.chat.id, ChatAction.TYPING)
|
||||||
|
|
||||||
|
# Attempt to convert voice to text with the selected language
|
||||||
|
text: str = speech_recognition.convert_voice_to_text(file_path, language=language) # type: ignore
|
||||||
self.logger.info("Voice message successfully converted to text.")
|
self.logger.info("Voice message successfully converted to text.")
|
||||||
|
|
||||||
# Format the text for sending
|
# Format the text for sending
|
||||||
formatted_text: str = (
|
formatted_text: str = (
|
||||||
"<b>Conversion Result:</b>"
|
f"<pre language=\"Conversion Result ({language})\">"
|
||||||
"<pre>"
|
|
||||||
f"{text}"
|
f"{text}"
|
||||||
"</pre>"
|
"</pre>"
|
||||||
)
|
)
|
||||||
@ -142,6 +174,9 @@ class TelegramUserBot:
|
|||||||
except Exception:
|
except Exception:
|
||||||
self.logger.error("An unexpected error occurred.", exc_info=True)
|
self.logger.error("An unexpected error occurred.", exc_info=True)
|
||||||
await processing_message.edit_text("An error occurred while processing the voice message. Please try again later.")
|
await processing_message.edit_text("An error occurred while processing the voice message. Please try again later.")
|
||||||
|
finally:
|
||||||
|
# Stop indicating typing action
|
||||||
|
await client.send_chat_action(message.chat.id, ChatAction.CANCEL)
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user