-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (49 loc) · 1.79 KB
/
Copy pathmain.py
File metadata and controls
68 lines (49 loc) · 1.79 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
import asyncio
import logging
import yaml
from aiogram import Bot, Dispatcher, types, F
from aiogram.types import InlineQueryResultArticle, InputTextMessageContent
from aiogram.filters import CommandStart
from config import BOT_TOKEN
logging.basicConfig(level=logging.INFO)
def load_snippets():
with open("snippets.yaml", "r", encoding="utf-8") as f:
return yaml.safe_load(f)
snippets = load_snippets()
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher()
@dp.message(CommandStart())
async def cmd_start(message: types.Message):
await message.answer(
"Hello! I am an inline bot. Type @CollapseLoader_bot to see my snippets."
)
@dp.inline_query()
async def inline_query_handler(query: types.InlineQuery):
query_text = query.query.lower().strip()
results = []
for key, data in snippets.items():
title = data.get("title", key)
content = data.get("content", "")
if (
query_text in key.lower()
or query_text in title.lower()
or query_text in content.lower()
):
description = content.split("\n")[0] if content else "No content"
if len(description) > 50:
description = description[:47] + "..."
results.append(
InlineQueryResultArticle(
id=key,
title=title,
description=description,
input_message_content=InputTextMessageContent(
message_text=content, parse_mode="Markdown"
),
)
)
await query.answer(results[:50], cache_time=1)
async def main():
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())