Files
kandrusyak_bot/assistant_bot/application.py

119 lines
3.7 KiB
Python

import logging
from telegram import Update
from telegram.ext import (
Application,
CommandHandler,
InlineQueryHandler,
MessageHandler,
TypeHandler,
filters,
)
from .ai import AIClient
from .auth import authentication_guard
from .config import (
get_assistant_mode,
get_assistant_password,
get_bot_token,
get_db_path,
get_local_timezone,
get_ollama_base_url,
get_voice_max_duration_seconds,
get_whisper_compute_type,
get_whisper_device,
get_whisper_language,
get_whisper_model,
get_yandex_cloud_folder,
get_yandex_stt_language,
get_yandex_stt_model,
)
from .handlers import (
ask_command,
help_command,
history_command,
inline_query,
model_command,
models_command,
new_conversation_command,
private_text,
private_voice,
start,
)
from .jobs import post_init, post_shutdown
from .ollama import OllamaClient
from .storage import AssistantStorage
from .speech import SpeechRecognizer, SpeechTranscriber
from .yandex_ai import YandexAIClient, YandexSpeechRecognizer
def configure_logging() -> None:
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
logging.getLogger("httpx").setLevel(logging.WARNING)
def create_application() -> Application:
assistant_password = get_assistant_password()
storage = AssistantStorage(get_db_path())
mode = get_assistant_mode()
application = (
Application.builder()
.token(get_bot_token())
.post_init(post_init)
.post_shutdown(post_shutdown)
.build()
)
application.bot_data["storage"] = storage
application.bot_data["assistant_password"] = assistant_password
ai_client: AIClient
speech_recognizer: SpeechTranscriber
if mode == "local":
ai_client = OllamaClient(get_ollama_base_url())
speech_recognizer = SpeechRecognizer(
model_name=get_whisper_model(),
device=get_whisper_device(),
compute_type=get_whisper_compute_type(),
language=get_whisper_language(),
)
else:
folder_id = get_yandex_cloud_folder()
ai_client = YandexAIClient(folder_id=folder_id)
speech_recognizer = YandexSpeechRecognizer(
folder_id=folder_id,
language=get_yandex_stt_language(),
model=get_yandex_stt_model(),
)
application.bot_data["ai_client"] = ai_client
application.bot_data["timezone"] = get_local_timezone()
application.bot_data["speech_recognizer"] = speech_recognizer
application.bot_data["voice_max_duration"] = get_voice_max_duration_seconds()
application.add_handler(TypeHandler(Update, authentication_guard), group=-1)
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("help", help_command))
application.add_handler(CommandHandler("ask", ask_command))
application.add_handler(CommandHandler("models", models_command))
application.add_handler(CommandHandler("model", model_command))
application.add_handler(CommandHandler("new", new_conversation_command))
application.add_handler(CommandHandler("history", history_command))
application.add_handler(InlineQueryHandler(inline_query))
application.add_handler(
MessageHandler(filters.VOICE & filters.ChatType.PRIVATE, private_voice)
)
application.add_handler(
MessageHandler(
filters.TEXT & filters.ChatType.PRIVATE & ~filters.COMMAND,
private_text,
)
)
return application
def run() -> None:
configure_logging()
create_application().run_polling(allowed_updates=Update.ALL_TYPES)