-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
160 lines (120 loc) · 5.46 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CallbackQueryHandler
import logging
from emoji import emojize
from collections import deque
import shelve
from utils import *
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
GREETING_MSG = 'Привет, {name}! Я {botname}!:waving_hand:\nО чём хочешь узнать?'
NOT_FOUND_MSG = 'По запросу ничего не найдено! :frowning_face:'
ERROR_MSG = 'Что-то пошло не так... :thinking_face:'
MULTIPLE_MEANINGS_MSG = 'Много различных значений, уточните запрос. :winking_face:'
HISTORY_HAS_BEEN_CLEANED_MSG = 'История запросов очищена.'
HISTORY_IS_EMPTY_MSG = 'История запросов чиста.'
HISTORY_LIST_MSG = 'История запросов:'
TOKEN = None
HISTORY_MAX_LEN = 10
DB_NAME = 'history.db'
wiki = WikipediaDataSet('articles_meta.csv', 'dataset.npy', 'cc.ru.300.bin')
logger.info('Wikipedia dataset has been constructed')
wiki.build_LSH_index()
logger.info('LSH Index has been built')
def start(bot, update):
msg = GREETING_MSG.format(name=update.message.from_user.first_name, botname=bot.username)
update.message.reply_text(emojize(msg))
def history(bot, update):
with shelve.open(DB_NAME) as db:
hist = db.get(str(update.message.chat_id))
if hist:
rows = []
for i, (title, url) in enumerate(hist):
rows.append('{}. {} [{}]'.format(i+1, title, url))
update.message.reply_text('{}\n{}'.format(HISTORY_LIST_MSG, '\n'.join(rows)),
disable_web_page_preview=True)
else:
update.message.reply_text(HISTORY_IS_EMPTY_MSG)
def add_to_history(chat_id, title, url):
key = str(chat_id)
with shelve.open(DB_NAME) as db:
hist = db.get(key)
hist = hist or deque(maxlen=HISTORY_MAX_LEN)
hist.append((title, url))
with shelve.open(DB_NAME) as db:
db[key] = hist
def clear_history(bot, update):
with shelve.open(DB_NAME) as db:
db[str(update.message.chat_id)] = deque(maxlen=HISTORY_MAX_LEN)
update.message.reply_text(emojize(HISTORY_HAS_BEEN_CLEANED_MSG))
def button_pressed(bot, update):
query = update.callback_query
query.answer()
bot.send_chat_action(chat_id=query.message.chat_id,
action=telegram.ChatAction.TYPING)
url = WIKI_ARTICLE_URL.format(pageid=query.data)
title = get_title_by_url(url)
summary = get_wiki_article_summary(query.data)
summary = summary if summary else ''
title = title if title else '?'
recommended_articles = wiki.find_k_nearest_neighbors(title)
keyboard = []
for article in recommended_articles:
keyboard.append([
InlineKeyboardButton(article.title, callback_data=article.pageid)
])
reply_markup = InlineKeyboardMarkup(keyboard)
add_to_history(str(query.message.chat_id), title, url)
bot.edit_message_text('{}\n{}'.format(url, summary),
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=reply_markup,
disable_web_page_preview=True)
def query_article(bot, update):
bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING)
query = update.message.text.strip().lower()
try:
found = search_wiki_article(query)
if found:
title, pageid = found
url = WIKI_ARTICLE_URL.format(pageid=pageid)
summary = get_wiki_article_summary(pageid)
if summary:
recommended_articles = wiki.find_k_nearest_neighbors(title)
keyboard = []
for article in recommended_articles:
keyboard.append([
InlineKeyboardButton(article.title, callback_data=article.pageid)
])
reply_markup = InlineKeyboardMarkup(keyboard)
add_to_history(str(update.message.chat_id), title, url)
update.message.reply_text('{}\n{}'.format(url, summary),
reply_markup=reply_markup,
disable_web_page_preview=True)
else:
update.message.reply_text(emojize(MULTIPLE_MEANINGS_MSG))
else:
update.message.reply_text(emojize(NOT_FOUND_MSG))
except Exception as AnyException:
update.message.reply_text(emojize(ERROR_MSG))
logger.error(AnyException)
def error(bot, update, error):
logger.warning('Update "%s" caused error "%s"', update, error)
def main():
updater = Updater(TOKEN, request_kwargs={'read_timeout': 30,
'connect_timeout': 30})
dp = updater.dispatcher
dp.add_handler(CallbackQueryHandler(button_pressed))
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("hist", history))
dp.add_handler(CommandHandler("clear_hist", clear_history))
dp.add_handler(MessageHandler(Filters.text, query_article))
dp.add_error_handler(error)
updater.start_polling(clean=True)
logger.info('Bot Started')
updater.idle()
if __name__ == '__main__':
main()