1
0
mirror of https://github.com/ijaric/voice_assistant.git synced 2025-12-18 15:46:17 +00:00

feat: [#49] minimally functioning bot

This commit is contained in:
2023-10-14 01:12:02 +03:00
parent a9bb229801
commit 70696ae067
9 changed files with 56 additions and 18 deletions

View File

@@ -1,4 +1,5 @@
import io
import typing
import aiogram
import aiohttp
@@ -7,26 +8,42 @@ import tgbot.settings as tgbot_settings
async def voice_response(message_voice: aiogram.types.Message):
config: tgbot_settings.Settings = message_voice.bot.get("config")
config = typing.cast(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)
voice_answer: bytes = await resp.read()
answer_io = io.BytesIO(voice_answer)
answer_io.name = "answer_io.ogg"
await message_voice.bot.send_chat_action(
message_voice.from_user.id, action=aiogram.types.ChatActions.RECORD_AUDIO
)
try:
await message_voice.answer_voice(voice=answer_io)
except aiogram.exceptions.BadRequest:
await message_voice.answer(
"We were unable to send you a voice message. Please check your privacy settings."
)
else:
await message_voice.answer("Not recognized text")
await session.close()
return
def register_voice_response(dp: aiogram.Dispatcher):

View File

@@ -1,9 +1,8 @@
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)
api: app_split_settings.ApiSettings = app_split_settings.ApiSettings()
tgbot: app_split_settings.TgBotSettings = app_split_settings.TgBotSettings()

View File

@@ -11,9 +11,9 @@ class ApiSettings(pydantic_settings.BaseSettings):
extra="ignore",
)
url: str
port: int
protocol: str
url: str = "127.0.0.1"
port: int = 8000
protocol: str = "http"
@property
def api_url(self) -> str: