1
0
mirror of https://github.com/ijaric/voice_assistant.git synced 2025-05-24 14:33:26 +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,9 +1,11 @@
BOT_CONTAINER_NAME=bot_container_name
BOT_IMAGE_NAME=botimage_name
BOT_NAME=mybotname
# required parameters
BOT_TOKEN=123456:Your-TokEn_ExaMple
BOT_ADMINS=123456,654321
API_PROTOCOL=http
API_URL=127.0.0.1
API_URL=api
API_PORT=8000

View File

@ -1,8 +1,15 @@
FROM python:3.9-buster
FROM python:3.11
ENV BOT_NAME=$BOT_NAME
WORKDIR /usr/src/app/"${BOT_NAME:-tg_bot}"
COPY requirements.txt /usr/src/app/"${BOT_NAME:-tg_bot}"
RUN pip install -r /usr/src/app/"${BOT_NAME:-tg_bot}"/requirements.txt
COPY pyproject.toml /usr/src/app/"${BOT_NAME:-tg_bot}"/pyproject.toml
COPY poetry.lock /usr/src/app/"${BOT_NAME:-tg_bot}"//poetry.lock
COPY poetry.toml /usr/src/app/"${BOT_NAME:-tg_bot}"/poetry.toml
WORKDIR /usr/src/app/"${BOT_NAME:-tg_bot}"
RUN pip install poetry \
&& poetry install --no-dev
COPY . /usr/src/app/"${BOT_NAME:-tg_bot}"

View File

@ -10,7 +10,7 @@ services:
working_dir: "/usr/src/app/${BOT_NAME:-tg_bot}"
volumes:
- .:/usr/src/app/${BOT_NAME:-tg_bot}
command: python3 -m bot
command: poetry run python bot.py
restart: always
env_file:
- ".env"

View File

@ -907,6 +907,21 @@ files = [
[package.dependencies]
typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0"
[[package]]
name = "pydantic-settings"
version = "2.0.3"
description = "Settings management using Pydantic"
optional = false
python-versions = ">=3.7"
files = [
{file = "pydantic_settings-2.0.3-py3-none-any.whl", hash = "sha256:ddd907b066622bd67603b75e2ff791875540dc485b7307c4fffc015719da8625"},
{file = "pydantic_settings-2.0.3.tar.gz", hash = "sha256:962dc3672495aad6ae96a4390fac7e593591e144625e5112d359f8f67fb75945"},
]
[package.dependencies]
pydantic = ">=2.0.1"
python-dotenv = ">=0.21.0"
[[package]]
name = "pylint"
version = "2.17.7"
@ -1346,4 +1361,4 @@ multidict = ">=4.0"
[metadata]
lock-version = "2.0"
python-versions = "^3.11"
content-hash = "6a4d557aec6dcef7c790b3838d67c294ac771255d2967b05d8b6745fd81419cd"
content-hash = "afc5943d3cbf03dd9b7103acb8a74e8750b22fc3b98c50efa21c3ea5f2739483"

View File

@ -22,6 +22,7 @@ version = "0.1.0"
[tool.poetry.dependencies]
aiogram = "~2.18"
environs = "~9.0"
pydantic-settings = "^2.0.3"
pytest-asyncio = "^0.21.1"
python = "^3.11"

View File

@ -1,3 +0,0 @@
aiogram~=2.18
aioredis<2.0
environs~=9.0

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: