-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
137 lines (110 loc) · 3.6 KB
/
bot.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import csv
import logging
import os
import random
import uuid
from fuzzywuzzy import fuzz
from telegram import (
InlineQueryResultArticle,
InlineQueryResultCachedSticker,
InputTextMessageContent,
Update,
)
from telegram.ext import (
CallbackContext,
ChosenInlineResultHandler,
Filters,
InlineQueryHandler,
MessageHandler,
Updater,
)
MINUTE = 60
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
def load_db() -> dict[tuple[str, str], tuple[str]]:
with open("stickers.csv", "r") as f:
reader = csv.DictReader(f, ["id", "sticker_file_id"], restkey="search_tags")
first_line = next(reader)
if first_line.get("id") != "id":
f.seek(0)
return {
(entry["id"], entry["sticker_file_id"]): tuple(entry["search_tags"])
for entry in reader
}
db = load_db()
inverted_db = {val: k for k, values in db.items() for val in values}
inverted_db_keys = inverted_db.keys()
def ranked_matches(query: str) -> set[tuple[str, str]]:
match_ratio = 80
return {
inverted_db[key]
for key, ratio in sorted(
((k, fuzz.partial_ratio(k, query)) for k in inverted_db_keys),
key=lambda _: _[1],
reverse=True,
)
if ratio >= match_ratio
}
def inline_handler(update: Update, _):
query = update.inline_query.query
if query == "":
matches = {inverted_db[key] for key in inverted_db_keys}
else:
matches = ranked_matches(query)
if matches:
update.inline_query.answer(
[
InlineQueryResultCachedSticker(id=match[0], sticker_file_id=match[1])
for match in matches
],
is_personal=False,
auto_pagination=True,
cache_time=60 * MINUTE,
)
else:
update.inline_query.answer(
[
InlineQueryResultArticle(
id=str(uuid.uuid4()),
title="Контора пидорасов",
input_message_content=InputTextMessageContent(
message_text=f"{query} - контора пидорасов"
),
)
],
is_personal=False,
auto_pagination=True,
)
def echo_sticker(update: Update, _):
update.message.reply_text(
f"`{update.message.sticker.file_id}`", parse_mode="markdown"
)
def echo_usage(update: Update, context: CallbackContext):
update.message.reply_text(
f"Попробуй что-н поискать, например: `{context.bot.bot.name} {random.choice(list(inverted_db_keys))}`",
parse_mode="markdown",
)
def echo(update: Update, _):
logging.info(
"[STICKER CHOSEN] query='%s' result='%s'",
update.chosen_inline_result.query,
update.chosen_inline_result.result_id,
)
TG_TOKEN = os.getenv("TG_TOKEN")
updater = Updater(TG_TOKEN)
updater.dispatcher.add_handler(InlineQueryHandler(inline_handler))
updater.dispatcher.add_handler(MessageHandler(Filters.sticker, echo_sticker))
updater.dispatcher.add_handler(ChosenInlineResultHandler(echo))
updater.dispatcher.add_handler(MessageHandler(Filters.all, echo_usage))
if __name__ == "__main__":
# updater.start_polling()
PORT = int(os.getenv("PORT", 8080))
APP_NAME = os.getenv("HEROKU_APP_NAME")
updater.start_webhook(
listen="0.0.0.0",
port=PORT,
url_path=TG_TOKEN,
webhook_url=f"https://{APP_NAME}.herokuapp.com/{TG_TOKEN}",
)
updater.idle()