mirror of
https://github.com/ijaric/voice_assistant.git
synced 2025-12-18 15:46:17 +00:00
feat: [#49] tg_bot
This commit is contained in:
33
src/bot_aiogram/tgbot/handlers/voice.py
Normal file
33
src/bot_aiogram/tgbot/handlers/voice.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import io
|
||||
|
||||
import aiogram
|
||||
import aiohttp
|
||||
|
||||
import tgbot.settings as tgbot_settings
|
||||
|
||||
|
||||
async def voice_response(message_voice: aiogram.types.Message):
|
||||
config: tgbot_settings.Settings = message_voice.bot.get("config")
|
||||
voice_file_id: str = message_voice.voice.file_id
|
||||
file_info = await message_voice.bot.get_file(voice_file_id)
|
||||
file_path: str = file_info.file_path
|
||||
voice_data: io.BytesIO = io.BytesIO()
|
||||
voice_data.name = "voice.ogg"
|
||||
voice_data.seek(0)
|
||||
await message_voice.bot.download_file(file_path, destination=voice_data)
|
||||
await message_voice.bot.send_chat_action(message_voice.from_user.id, "typing")
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(
|
||||
f"{config.api.api_url}/api/v1/voice/",
|
||||
data={"voice": voice_data},
|
||||
) as resp:
|
||||
if resp.status == 200:
|
||||
voice_answer = await resp.read()
|
||||
await message_voice.answer_voice(voice_answer)
|
||||
else:
|
||||
await message_voice.answer("Not recognized text")
|
||||
await session.close()
|
||||
|
||||
|
||||
def register_voice_response(dp: aiogram.Dispatcher):
|
||||
dp.register_message_handler(voice_response, content_types=aiogram.types.ContentType.VOICE)
|
||||
4
src/bot_aiogram/tgbot/misc/utils.py
Normal file
4
src/bot_aiogram/tgbot/misc/utils.py
Normal file
@@ -0,0 +1,4 @@
|
||||
import pathlib
|
||||
|
||||
BASE_PATH = pathlib.Path(__file__).parent.parent.parent.parent.resolve()
|
||||
ENV_PATH = BASE_PATH / ".env"
|
||||
9
src/bot_aiogram/tgbot/settings.py
Normal file
9
src/bot_aiogram/tgbot/settings.py
Normal file
@@ -0,0 +1,9 @@
|
||||
import pydantic
|
||||
import pydantic_settings
|
||||
|
||||
import tgbot.split_settings as app_split_settings
|
||||
|
||||
|
||||
class Settings(pydantic_settings.BaseSettings):
|
||||
api: app_split_settings.ApiSettings = pydantic.Field(default_factory=app_split_settings.ApiSettings)
|
||||
tgbot: app_split_settings.TgBotSettings = pydantic.Field(default_factory=app_split_settings.TgBotSettings)
|
||||
7
src/bot_aiogram/tgbot/split_settings/__init__.py
Normal file
7
src/bot_aiogram/tgbot/split_settings/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from .api import *
|
||||
from .tgbot import *
|
||||
|
||||
__all__ = [
|
||||
"ApiSettings",
|
||||
"TgBotSettings",
|
||||
]
|
||||
20
src/bot_aiogram/tgbot/split_settings/api.py
Normal file
20
src/bot_aiogram/tgbot/split_settings/api.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import pydantic_settings
|
||||
|
||||
import tgbot.split_settings.utils as split_settings_utils
|
||||
|
||||
|
||||
class ApiSettings(pydantic_settings.BaseSettings):
|
||||
model_config = pydantic_settings.SettingsConfigDict(
|
||||
env_file=split_settings_utils.ENV_PATH,
|
||||
env_prefix="API_",
|
||||
env_file_encoding="utf-8",
|
||||
extra="ignore",
|
||||
)
|
||||
|
||||
url: str
|
||||
port: int
|
||||
protocol: str
|
||||
|
||||
@property
|
||||
def api_url(self) -> str:
|
||||
return f"{self.protocol}://{self.url}:{self.port}"
|
||||
22
src/bot_aiogram/tgbot/split_settings/tgbot.py
Normal file
22
src/bot_aiogram/tgbot/split_settings/tgbot.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import pydantic
|
||||
import pydantic_settings
|
||||
|
||||
import tgbot.split_settings.utils as split_settings_utils
|
||||
|
||||
|
||||
class TgBotSettings(pydantic_settings.BaseSettings):
|
||||
model_config = pydantic_settings.SettingsConfigDict(
|
||||
env_file=split_settings_utils.ENV_PATH,
|
||||
env_prefix="BOT_",
|
||||
env_file_encoding="utf-8",
|
||||
extra="ignore",
|
||||
)
|
||||
|
||||
token: pydantic.SecretStr = pydantic.Field(
|
||||
default=..., validation_alias=pydantic.AliasChoices("token", "bot_token")
|
||||
)
|
||||
admins: str = pydantic.Field(default="")
|
||||
|
||||
@pydantic.field_validator("admins")
|
||||
def validate_bot_admins(cls, v: str) -> list[int]:
|
||||
return list(map(int, v.split(",")))
|
||||
4
src/bot_aiogram/tgbot/split_settings/utils.py
Normal file
4
src/bot_aiogram/tgbot/split_settings/utils.py
Normal file
@@ -0,0 +1,4 @@
|
||||
import pathlib
|
||||
|
||||
BASE_PATH = pathlib.Path(__file__).parent.parent.parent.resolve()
|
||||
ENV_PATH = BASE_PATH / ".env"
|
||||
Reference in New Issue
Block a user