1
0
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:
2023-10-13 23:05:25 +03:00
parent bc88ceffec
commit 8486d2e0bf
10 changed files with 247 additions and 0 deletions

View 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)

View File

@@ -0,0 +1,4 @@
import pathlib
BASE_PATH = pathlib.Path(__file__).parent.parent.parent.parent.resolve()
ENV_PATH = BASE_PATH / ".env"

View 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)

View File

@@ -0,0 +1,7 @@
from .api import *
from .tgbot import *
__all__ = [
"ApiSettings",
"TgBotSettings",
]

View 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}"

View 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(",")))

View File

@@ -0,0 +1,4 @@
import pathlib
BASE_PATH = pathlib.Path(__file__).parent.parent.parent.resolve()
ENV_PATH = BASE_PATH / ".env"