-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram_bot_example.py
More file actions
111 lines (84 loc) · 3.29 KB
/
Copy pathtelegram_bot_example.py
File metadata and controls
111 lines (84 loc) · 3.29 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
"""
Приклад 2: Telegram бот з Perplexity API
Вимоги:
pip install aiogram openai python-dotenv
Налаштування (.env):
PERPLEXITY_API_KEY=pplx-xxxxxxxxxx
TELEGRAM_BOT_TOKEN=your_bot_token
"""
import os
import asyncio
from aiogram import Bot, Dispatcher, types
from aiogram.filters import CommandStart, Command
from openai import AsyncOpenAI
from dotenv import load_dotenv
load_dotenv()
bot = Bot(token=os.environ["TELEGRAM_BOT_TOKEN"])
dp = Dispatcher()
# Асинхронний клієнт для aiogram
perplexity = AsyncOpenAI(
api_key=os.environ["PERPLEXITY_API_KEY"],
base_url="https://api.perplexity.ai"
)
# Зберігання контексту розмов
conversations: dict[int, list] = {}
@dp.message(CommandStart())
async def start_handler(message: types.Message):
await message.answer(
"👋 Привіт! Я AI-асистент на базі Perplexity.\n"
"Задавай будь-які питання — я шукаю відповіді в реальному часі!"
)
@dp.message(Command("clear"))
async def clear_handler(message: types.Message):
conversations.pop(message.from_user.id, None)
await message.answer("🗑 Контекст розмови очищено")
@dp.message()
async def message_handler(message: types.Message):
user_id = message.from_user.id
user_text = message.text
# Ініціалізація контексту
if user_id not in conversations:
conversations[user_id] = [{
"role": "system",
"content": "Ти корисний AI-асистент. Відповідай коротко і по суті. Мова: українська."
}]
conversations[user_id].append({
"role": "user",
"content": user_text
})
# Показуємо typing індикатор
await bot.send_chat_action(message.chat.id, "typing")
try:
response = await perplexity.chat.completions.create(
model="sonar",
messages=conversations[user_id],
return_citations=True
)
answer = response.choices[0].message.content
citations = getattr(response, "citations", [])
# Додаємо відповідь до контексту
conversations[user_id].append({
"role": "assistant",
"content": answer
})
# Обмеження контексту (останні 10 повідомлень + system)
if len(conversations[user_id]) > 21:
conversations[user_id] = (
conversations[user_id][:1] + # system
conversations[user_id][-20:] # останні 20
)
# Формуємо відповідь
reply = answer
if citations:
reply += "\n\n📚 Джерела:\n"
for i, url in enumerate(citations[:3], 1): # максимум 3 джерела
reply += f"{i}. {url}\n"
await message.reply(reply)
except Exception as e:
await message.reply(f"❌ Помилка: {str(e)}")
async def main():
print("🤖 Бот запущено")
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())