-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
86 lines (73 loc) · 3.23 KB
/
Copy pathbot.py
File metadata and controls
86 lines (73 loc) · 3.23 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
from telegram import InlineQueryResultArticle, InputTextMessageContent, Update
from telegram.ext import ApplicationBuilder, InlineQueryHandler, ContextTypes, CommandHandler
from parser import getDefs
from mytoken import BOT_TOKEN
import uuid
# f"<a href=\"https://hy.wiktionary.org/wiki/{query}\">(wiki)</a>"
async def inline_query_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
original_query = update.inline_query.query
query = original_query.lower()
if not query:
return
# print(f"{update.inline_query.from_user.first_name} ({update.inline_query.from_user.username}): {query}")
defs = getDefs(query)
if not any(defs.values()):
defs = getDefs(query.capitalize())
if not any(defs.values()):
defs = getDefs(original_query)
results = []
n = 0
for k, v in defs.items():
for i in v:
n+=1
fordesc = i.replace('<i>', '')
fordesc = fordesc.replace('</i>', '')
results.append(
InlineQueryResultArticle(
id=str(uuid.uuid4()),
title=k,
description=str(n) + '. ' + fordesc,
input_message_content=InputTextMessageContent(
message_text = (
f"Բառ՝\n"
f"<blockquote><b>{query}</b></blockquote>\n"
f"Բացատրություն՝\n"
f"<blockquote><u>{k + '\n'}</u><b>{str(n) + '. ' + i}</b></blockquote>"
),
parse_mode = "HTML",
disable_web_page_preview=True
)
)
)
if not results:
results.append(
InlineQueryResultArticle(
id=str(uuid.uuid4()),
title=query,
description="Այսպիսի բառ չգտանք :(",
input_message_content=InputTextMessageContent(
message_text=(
f"Բառ՝\n"
f"<blockquote><b>{query}</b></blockquote>\n"
f"Բացատրություն՝\n"
f"<blockquote><b>Այսպիսի բառ չգտանք :(</b></blockquote>"
),
parse_mode="HTML",
disable_web_page_preview=True
)
)
)
await update.inline_query.answer(results[:15], cache_time=0)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("""
Հայերեն բառարան, որը աշխատում է ցանկացած Տելեգրամյան չատում (inline)!
Ուղղակի գրիր որտեղ ուզում ես՝
@barayinbot [քո բառը]
(by @millkeny)
""")
print(f"\n{update.message.from_user.first_name} (@{update.message.from_user.username}): /start\n")
app = ApplicationBuilder().token(BOT_TOKEN).build()
print("Bot is working!\n")
app.add_handler(CommandHandler("start", start))
app.add_handler(InlineQueryHandler(inline_query_handler))
app.run_polling()