-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSimpleTgBot.py
More file actions
56 lines (48 loc) · 1.7 KB
/
SimpleTgBot.py
File metadata and controls
56 lines (48 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import asyncio
import logging
from aiogram import Bot, Dispatcher, types
from aiogram import Router, F
from aiogram.filters import Command
from aiogram.types import Message
from grok3api.client import GrokClient
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger("aiogram")
BOT_TOKEN = "BOT_TOKEN"
bot = Bot(token=BOT_TOKEN)
router = Router()
dp = Dispatcher()
@router.message(~F.text.startswith("/"))
async def handle_message(message: Message):
"""Обрабатывает текст."""
if not message.text:
await message.answer("Пустое сообщение.")
return
try:
response = await GROK_CLIENT.async_ask(message=message.text, history_id=str(message.chat.id))
text_response = response.modelResponse.message if response.modelResponse else ""
await message.answer(text_response)
except Exception as e:
print(f"Ошибка: {e}")
await message.answer("Ошибка.")
@router.message(Command("clear"))
async def handle_clear_command(message: Message):
"""Очищает историю."""
try:
GROK_CLIENT.history.del_history_by_id(str(message.chat.id))
await message.answer("История очищена.")
except Exception as e:
print(f"Ошибка: {e}")
await message.answer("Ошибка очистки.")
async def main():
"""Запускает бота."""
dp.include_router(router)
await dp.start_polling(bot)
if __name__ == "__main__":
GROK_CLIENT = GrokClient(
history_msg_count=10,
main_system_prompt="Отвечай только матом"
)
asyncio.run(main())