mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2025-09-11 22:30:41 +03:00
add keyboard (#13)
This commit is contained in:
parent
eb36036c04
commit
275470f174
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
install service
|
install service
|
||||||
|
|
||||||
sudo cp scripts/chat-gpt.service /etc/systemd/system
|
sudo cp scripts/gpt_chat_bot.service /etc/systemd/system
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd ~/PycharmProjects/chat_gpt_bot
|
cd ~/PycharmProjects/chat_gpt_bot
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from enum import StrEnum
|
from enum import IntEnum, StrEnum, auto
|
||||||
|
|
||||||
AUDIO_SEGMENT_DURATION = 120 * 1000
|
AUDIO_SEGMENT_DURATION = 120 * 1000
|
||||||
|
|
||||||
@ -6,6 +6,18 @@ API_PREFIX = "/api"
|
|||||||
CHAT_GPT_BASE_URL = "http://chat_service:8858/backend-api/v2/conversation"
|
CHAT_GPT_BASE_URL = "http://chat_service:8858/backend-api/v2/conversation"
|
||||||
|
|
||||||
|
|
||||||
|
class BotStagesEnum(IntEnum):
|
||||||
|
about_me = auto()
|
||||||
|
website = auto()
|
||||||
|
help = auto()
|
||||||
|
about_bot = auto()
|
||||||
|
|
||||||
|
|
||||||
|
class BotEntryPoints(IntEnum):
|
||||||
|
start_routes = auto()
|
||||||
|
end = auto()
|
||||||
|
|
||||||
|
|
||||||
class LogLevelEnum(StrEnum):
|
class LogLevelEnum(StrEnum):
|
||||||
CRITICAL = "critical"
|
CRITICAL = "critical"
|
||||||
ERROR = "error"
|
ERROR = "error"
|
||||||
|
@ -1,27 +1,66 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import random
|
import random
|
||||||
import tempfile
|
import tempfile
|
||||||
|
from urllib.parse import urljoin
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
from constants import CHAT_GPT_BASE_URL
|
from constants import CHAT_GPT_BASE_URL, BotEntryPoints
|
||||||
|
from core.keyboards import main_keyboard
|
||||||
from core.utils import SpeechToTextService
|
from core.utils import SpeechToTextService
|
||||||
from httpx import AsyncClient, AsyncHTTPTransport
|
from httpx import AsyncClient, AsyncHTTPTransport
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from settings.config import settings
|
from settings.config import settings
|
||||||
from telegram import Update
|
from telegram import InlineKeyboardMarkup, Update
|
||||||
from telegram.ext import ContextTypes
|
from telegram.ext import ContextTypes
|
||||||
|
|
||||||
|
|
||||||
|
async def main_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
||||||
|
"""Send message on `/start`."""
|
||||||
|
if not update.message:
|
||||||
|
return BotEntryPoints.end
|
||||||
|
reply_markup = InlineKeyboardMarkup(main_keyboard)
|
||||||
|
await update.message.reply_text("Выберете команду:", reply_markup=reply_markup)
|
||||||
|
return BotEntryPoints.start_routes
|
||||||
|
|
||||||
|
|
||||||
|
async def about_me(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
|
if not update.effective_message:
|
||||||
|
return None
|
||||||
|
await update.effective_message.reply_text(
|
||||||
|
'Автор бота: *Дмитрий Афанасьев*\n\nTg nickname: *Balshtg*', parse_mode='MarkdownV2'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def about_bot(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
|
if not update.effective_message:
|
||||||
|
return None
|
||||||
|
await update.effective_message.reply_text(
|
||||||
|
"Бот использует бесплатную модель Chat-GPT3.5 для ответов на вопросы. "
|
||||||
|
"Принимает запросы на разных языках. \n\n Бот так же умеет переводить голосовые сообщения в текст"
|
||||||
|
"просто пришлите голосовуху и получите поток сознания без запятых в виде текста",
|
||||||
|
parse_mode='MarkdownV2',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def website(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
|
if not update.effective_message:
|
||||||
|
return None
|
||||||
|
website = urljoin(settings.DOMAIN, f"{settings.URL_PREFIX}/chat")
|
||||||
|
await update.effective_message.reply_text(f"Веб версия: {website}")
|
||||||
|
|
||||||
|
|
||||||
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
"""Send a message when the command /help is issued."""
|
"""Send a message when the command /help is issued."""
|
||||||
|
|
||||||
if not update.message:
|
if not update.effective_message:
|
||||||
return None
|
return None
|
||||||
await update.message.reply_text(
|
reply_markup = InlineKeyboardMarkup(main_keyboard)
|
||||||
|
await update.effective_message.reply_text(
|
||||||
"Help!",
|
"Help!",
|
||||||
disable_notification=True,
|
disable_notification=True,
|
||||||
api_kwargs={"text": "Hello World"},
|
api_kwargs={"text": "Список основных команд:"},
|
||||||
|
reply_markup=reply_markup,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,8 +1,22 @@
|
|||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from core.commands import ask_question, help_command, voice_recognize
|
from constants import BotEntryPoints, BotStagesEnum
|
||||||
from telegram.ext import CommandHandler, MessageHandler, filters
|
from core.commands import (
|
||||||
|
about_me,
|
||||||
|
ask_question,
|
||||||
|
help_command,
|
||||||
|
main_command,
|
||||||
|
voice_recognize,
|
||||||
|
website,
|
||||||
|
)
|
||||||
|
from telegram.ext import (
|
||||||
|
CallbackQueryHandler,
|
||||||
|
CommandHandler,
|
||||||
|
ConversationHandler,
|
||||||
|
MessageHandler,
|
||||||
|
filters,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -18,3 +32,19 @@ bot_event_handlers = BotEventHandlers()
|
|||||||
bot_event_handlers.add_handler(CommandHandler("help", help_command))
|
bot_event_handlers.add_handler(CommandHandler("help", help_command))
|
||||||
bot_event_handlers.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, ask_question))
|
bot_event_handlers.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, ask_question))
|
||||||
bot_event_handlers.add_handler(MessageHandler(filters.VOICE | filters.AUDIO, voice_recognize))
|
bot_event_handlers.add_handler(MessageHandler(filters.VOICE | filters.AUDIO, voice_recognize))
|
||||||
|
bot_event_handlers.add_handler(
|
||||||
|
ConversationHandler(
|
||||||
|
entry_points=[CommandHandler("start", main_command)],
|
||||||
|
states={
|
||||||
|
BotEntryPoints.start_routes: [
|
||||||
|
CallbackQueryHandler(about_me, pattern="^" + str(BotStagesEnum.about_me) + "$"),
|
||||||
|
CallbackQueryHandler(website, pattern="^" + str(BotStagesEnum.website) + "$"),
|
||||||
|
CallbackQueryHandler(help_command, pattern="^" + str(BotStagesEnum.help) + "$"),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
fallbacks=[CommandHandler("start", main_command)],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
bot_event_handlers.add_handler(CallbackQueryHandler(about_me, pattern="^" + str(BotStagesEnum.about_me) + "$"))
|
||||||
|
bot_event_handlers.add_handler(CallbackQueryHandler(website, pattern="^" + str(BotStagesEnum.website) + "$"))
|
||||||
|
bot_event_handlers.add_handler(CallbackQueryHandler(help_command, pattern="^" + str(BotStagesEnum.help) + "$"))
|
||||||
|
13
bot_microservice/core/keyboards.py
Normal file
13
bot_microservice/core/keyboards.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
from constants import BotStagesEnum
|
||||||
|
from telegram import InlineKeyboardButton
|
||||||
|
|
||||||
|
main_keyboard = [
|
||||||
|
[
|
||||||
|
InlineKeyboardButton("Обо мне", callback_data=str(BotStagesEnum.about_me)),
|
||||||
|
InlineKeyboardButton("Веб версия", callback_data=str(BotStagesEnum.website)),
|
||||||
|
],
|
||||||
|
[
|
||||||
|
InlineKeyboardButton("Помощь", callback_data=str(BotStagesEnum.help)),
|
||||||
|
InlineKeyboardButton("О боте", callback_data=str(BotStagesEnum.help)),
|
||||||
|
],
|
||||||
|
]
|
Loading…
x
Reference in New Issue
Block a user