-
Notifications
You must be signed in to change notification settings - Fork 0
/
yappi.py
301 lines (243 loc) · 8.45 KB
/
yappi.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"""
A main module with Yappi translator bot powered by Telegram Bot API.
"""
import logging
import urllib
from functools import partial, wraps
from inspect import signature
from telegram import InlineKeyboardButton as Button
from telegram import Emoji, InlineKeyboardMarkup, ParseMode
from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackQueryHandler, Filters
import yadict
import config
from models import db, CallbackEntity, Request, User, Chat, FirstRequest, Message
from templates import MessageTemplate, Translate
def logging_setup():
if config.Config.DEBUG:
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG
)
else:
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG,
filename='yappi.log'
)
FORWARD_URL = 'https://telegram.me/{fwd_bot}?start={fwd_url}'
class AnswerOption(object):
TRANSLATE = '1'
SKIP = '2'
def save_callback_data(data):
return CallbackEntity.create(data=data)
def encode_callback_data(answer_option, index):
return '{}@{}'.format(answer_option, index)
def decode_callback_data(callback_data):
index = int(callback_data.split('@')[1])
return CallbackEntity.get_callback(index)
def decode_answer_option(callback_data):
return callback_data.split('@')[0]
def userify(func):
@wraps(func)
@db.atomic()
def wrapper(*args, **kwargs):
update = args[1]
request = update.callback_query or update.message
tid = request.from_user.id
name = request.from_user.first_name
user, _ = User.get_or_create(tid=tid, name=name)
kwargs['user'] = user
return func(*args, **kwargs)
return wrapper
def chatify(func):
@wraps(func)
@db.atomic()
def wrapper(*args, **kwargs):
update = args[1]
request = update.callback_query or update
chat_id = request.message.chat_id
chat, _ = Chat.get_or_create(chat_id=chat_id)
kwargs['chat'] = chat
return func(*args, **kwargs)
return wrapper
def messagify(func):
@wraps(func)
@db.atomic()
def wrapper(*args, **kwargs):
update = args[1]
request = update.callback_query or update
message_id = request.message.message_id
time = request.message.to_dict()['date']
message = Message.create(
chat=kwargs['chat'],
user=kwargs['user'],
message_id=message_id,
time=time
)
kwargs['message'] = message
return func(*args, **kwargs)
return wrapper
def forward(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
success = kwargs['success']
except KeyError:
success = signature(func).parameters.get('success').default
markup = None
if success:
try:
request = kwargs['request']
except KeyError:
raise ValueError('Successful request, but data is missing!')
url = FORWARD_URL.format(
fwd_bot=config.Config.SR_BOT,
fwd_url=urllib.parse.quote(request)
)
keyboard = [[Button(MessageTemplate.BOOKMARK, url=url)]]
markup = InlineKeyboardMarkup(keyboard)
kwargs['markup'] = markup
return func(*args, **kwargs)
return wrapper
@forward
def edit_message(bot, update, text, success=None, request=None, **kwargs):
return bot.edit_message_text(
chat_id=update.callback_query.message.chat_id,
text=text,
message_id=update.callback_query.message.message_id,
reply_markup=kwargs['markup'],
parse_mode=ParseMode.MARKDOWN
)
@forward
def send_message(bot, update, text, success=None, request=None, **kwargs):
return bot.send_message(
chat_id=update.message.chat_id,
text=text,
reply_markup=kwargs['markup'],
parse_mode=ParseMode.MARKDOWN
)
@db.atomic()
def translate(content, user, chat, message, bot, reply):
def reply_and_save(request):
response = Translate.HEAD.format(caption=content, answer=answer)
reply_message = reply(response, success=True, request=content)
message.request = request
message.save()
FirstRequest.create(
request=request,
chat=chat,
user=user,
message=message,
reply_to=reply_message.message_id
)
content, warning = yadict.normalize(content)
if warning:
reply(content)
return
fr_query, request = FirstRequest.get_first_request_and_request(
chat=chat, user=user, content=content)
if fr_query:
reply(MessageTemplate.ALREADY_REQUESTED, success=True, request=content)
bot.send_message(
chat.chat_id,
Emoji.WHITE_UP_POINTING_INDEX,
reply_to_message_id=fr_query.reply_to
)
else:
if request:
answer = yadict.load_content_from_db(request)
reply_and_save(request)
else:
answer, created_request = yadict.load_content_from_api(content)
if not answer:
reply(MessageTemplate.CANT_FIND.format(content))
else:
reply_and_save(created_request)
@chatify
@userify
@messagify
def translate_command(bot, update, args, **kwargs):
user = kwargs['user']
chat = kwargs['chat']
message = kwargs['message']
reply = partial(send_message, bot, update)
translate(args, user, chat, message, bot, reply)
@chatify
@userify
@messagify
def handle_text(bot, update, user_data, **kwargs):
user_data['user'] = kwargs['user']
user_data['chat'] = kwargs['chat']
user_data['message'] = kwargs['message']
user_message = update.message.text
data_id = save_callback_data(user_message)
encode_translate = encode_callback_data(AnswerOption.TRANSLATE, data_id)
encode_skip = encode_callback_data(AnswerOption.SKIP, data_id)
keyboard = [[
Button('tr', callback_data=encode_translate),
Button('skip', callback_data=encode_skip)
]]
markup = InlineKeyboardMarkup(keyboard)
bot.send_message(
chat_id=update.message.chat_id,
text=MessageTemplate.TRANSLATE,
reply_markup=markup,
parse_mode=ParseMode.MARKDOWN)
def handle_message_dialog(bot, update, answer, user_data):
user = user_data['user']
chat = user_data['chat']
message = user_data['message']
reply = partial(edit_message, bot, update)
callback_data = update.callback_query.data
content = decode_callback_data(callback_data)
if not content:
reply(MessageTemplate.CALLBACK_DATA_MISSING)
# translate request
elif answer == AnswerOption.TRANSLATE:
translate(content, user, chat, message, bot, reply)
elif answer == AnswerOption.SKIP:
reply(MessageTemplate.SKIP)
def callback_handler(bot, update, user_data, chat_data):
callback_query_message_id = update.callback_query.message.message_id
current_message = chat_data.get('current_message')
if current_message == callback_query_message_id:
return
chat_data['current_message'] = callback_query_message_id
answer = decode_answer_option(update.callback_query.data)
translate_answers = (
AnswerOption.TRANSLATE,
AnswerOption.SKIP
)
if answer in translate_answers:
handle_message_dialog(bot, update, answer, user_data)
def stats(bot, update):
stats_message = 'Statistic:\n'
stats_message += ''.join(
MessageTemplate.USER_STATS_LINE.format(el[0], el[1])
for el in FirstRequest.statistics()
)
stats_message += MessageTemplate.TOTAL_ENTITIES.format(len(Request.select()))
bot.sendMessage(
update.message.chat_id,
stats_message,
parse_mode=ParseMode.MARKDOWN
)
def main():
logging_setup()
updater = Updater(config.Config.BTOKEN)
updater.dispatcher.add_handler(
CallbackQueryHandler(
callback_handler,
pass_user_data=True,
pass_chat_data=True
)
)
updater.dispatcher.add_handler(
CommandHandler('tr', translate_command, pass_args=True))
updater.dispatcher.add_handler(CommandHandler('stats', stats))
updater.dispatcher.add_handler(
MessageHandler(Filters.text, handle_text, pass_user_data=True))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()