-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
422 lines (388 loc) · 14.1 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import datetime
import html
import logging
import random
import traceback
from collections import defaultdict
from telegram import (
MessageEntity,
ParseMode,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
Update,
)
from telegram.ext import (
CallbackContext,
CommandHandler,
ConversationHandler,
Defaults,
Filters,
MessageHandler,
PicklePersistence,
Updater,
)
from telegram.ext.dispatcher import run_async
from fpwrapper import FeedCollection, FeedCollectionError
from localconfig import strings, envs
# declare symbols for conversation states
MAIN, ADD_URL, ADD_MODE, REMOVE_URL, EDIT_URL, EDIT_REPR = map(chr, range(6))
class EscapedDict(defaultdict):
def __getitem__(self, key):
value = defaultdict.__getitem__(self, key)
if isinstance(value, str):
return html.escape(value)
elif isinstance(value, dict):
return EscapedDict(self.default_factory, value)
else:
return value
class SimpleReplies(object):
"""Wrapper providing simple functions which reply with no side effects"""
def __getitem__(self, key):
@run_async
def r(upd: Update, ctx: CallbackContext, mapping: dict = {}, **kwargs):
mapping["_escaped"] = EscapedDict(str, mapping)
upd.message.reply_text(
text=strings[key].format_map(mapping),
**kwargs
)
return r
reply = SimpleReplies()
# simple command callbacks
def start(upd: Update, ctx: CallbackContext):
"""Initializes user settings and moves main_conv into MAIN"""
ctx.chat_data["feeds"] = {
"asap": FeedCollection([]),
"digest": FeedCollection([]),
}
ctx.chat_data["reprs"] = {}
ctx.chat_data["digesttime"] = datetime.time(0, 0, 0)
# enroll user in job_queue
ctx.job_queue.run_repeating(
callback=asap_update,
interval=envs["asap_freq"],
first=random.randrange(0, envs["asap_freq"]), # random staggering
context=upd.effective_chat.id,
)
ctx.job_queue.run_daily(
callback=digest_update,
time=ctx.chat_data["digesttime"],
context=upd.effective_chat.id,
)
reply["welcome"](upd, ctx)
return MAIN
def show_feeds(upd: Update, ctx: CallbackContext):
"""Pretty prints user feeds"""
feeds = {
mode : "\n".join((
f"{'+' if feed_url in ctx.chat_data['reprs'] else '-'} <a href='{feed_url}'>{html.escape(feed.metadata['title'])}</a>"
for feed_url, feed
in ctx.chat_data["feeds"][mode].feeds.items()
))
for mode in ("asap", "digest")
}
if not feeds["asap"] and not feeds["digest"]:
reply["nofeeds"](upd, ctx)
else:
reply["showfeeds"](upd, ctx, mapping=feeds)
# add flow callbacks
def add_command(upd: Update, ctx: CallbackContext):
"""Processes args of /add and hands over to add_feed"""
# look for urls
if upd.message.parse_entities(types=[MessageEntity.URL]).values():
ctx.chat_data["add_url"] = list(upd.message.parse_entities(types=[MessageEntity.URL]).values())
# look for mode in last argument
if len(ctx.args) >= 1 and ctx.args[-1].lower() in ("digest", "asap"):
ctx.chat_data["add_mode"] = ctx.args[-1].lower()
return add_feed(upd, ctx)
def add_url_step(upd: Update, ctx: CallbackContext):
"""Processes feed URL and hands over to add_feed"""
if upd.message.parse_entities(types=[MessageEntity.URL]).values():
ctx.chat_data["add_url"] = list(upd.message.parse_entities(types=[MessageEntity.URL]).values())
return add_feed(upd, ctx)
else:
return reply["add_urlwhat"](upd, ctx)
def add_mode_step(upd: Update, ctx: CallbackContext):
"""Processes feed mode and hands over to add_feed"""
ctx.chat_data["add_mode"] = upd.message.text.lower()
return add_feed(upd, ctx)
def add_cancel_conversation(upd: Update, ctx: CallbackContext):
"""Resets keyboard and moves main_conv back to MAIN"""
reply["add_cancel"](upd, ctx,
reply_markup=ReplyKeyboardRemove(selective=True)
)
return add_cleanup(upd, ctx)
def add_feed(upd: Update, ctx: CallbackContext):
"""Checks ctx for args and adds feed/directs to correct state"""
if "add_url" not in ctx.chat_data:
# direct user to send url
reply["add_requesturl"](upd, ctx)
return ADD_URL
elif "add_mode" not in ctx.chat_data:
# direct user to input mode
reply["add_requestmode"](upd, ctx,
reply_markup=ReplyKeyboardMarkup(
[["ASAP", "Digest"]],
resize_keyboard=True,
selective=True
)
)
return ADD_MODE
else:
# proceed to add feeds
success = []
duplicates = []
for url in ctx.chat_data["add_url"]:
try:
ctx.chat_data["feeds"][ctx.chat_data["add_mode"]].add_feed(url)
except FeedCollectionError:
duplicates.append(url)
else:
success.append(url)
if success:
reply["add_success"](upd, ctx,
mapping={"urls":", ".join(success)},
reply_markup=ReplyKeyboardRemove(selective=True),
)
if duplicates:
reply["add_dupurl"](upd, ctx,
mapping={"urls":", ".join(duplicates)},
reply_markup=ReplyKeyboardRemove(selective=True),
)
return add_cleanup(upd, ctx)
def add_cleanup(upd: Update, ctx: CallbackContext):
"""Clears out add context before returning to MAIN"""
try:
del ctx.chat_data["add_url"]
del ctx.chat_data["add_mode"]
except KeyError:
pass
return MAIN
# remove flow callbacks
def remove_command(upd: Update, ctx: CallbackContext):
"""Grabs URLs from entities and removes feeds, or bumps to REMOVE_URL if not found"""
urls = upd.message.parse_entities(types=[MessageEntity.URL]).values()
if len(urls) < 1:
reply["remove_requesturl"](upd, ctx,
reply_markup=ReplyKeyboardMarkup(
[
[url] for url in {
feed
for mode, fc in ctx.chat_data["feeds"].items()
for feed in fc.feeds
}
],
resize_keyboard=True,
selective=True,
)
)
return REMOVE_URL
# remove feeds from either FeedCollection
for url in urls:
_exc_counter = 0
try:
ctx.chat_data["feeds"]["asap"].remove_feed(url)
except FeedCollectionError:
_exc_counter += 1
try:
ctx.chat_data["feeds"]["digest"].remove_feed(url)
except FeedCollectionError:
_exc_counter += 1
if _exc_counter >= 2:
reply["remove_feednotfound"](upd, ctx,
mapping={"url": url},
reply_markup=ReplyKeyboardRemove(selective=True),
)
else:
reply["remove_success"](upd, ctx,
mapping={"url": url},
reply_markup=ReplyKeyboardRemove(selective=True),
)
return MAIN
def remove_cancel_conversation(upd: Update, ctx: CallbackContext):
reply["remove_cancel"](upd, ctx,
reply_markup=ReplyKeyboardRemove(selective=True),
)
return MAIN
# job_queue callbacks
def format_feeds(ctx: CallbackContext, fc: FeedCollection, reprs: dict, defaultrepr: str):
"""Gets new entries from a FeedCollection and formats them according to reprs/defaultrepr"""
entries = fc.get_new_entries()
formatted = {}
for url in entries:
try:
formatted[url] = [
(
reprs[url] if url in reprs
else defaultrepr
).format(
entry=entry,
feed=fc.feeds[url].metadata,
_escaped=EscapedDict(str, {
"entry": entry,
"feed": fc.feeds[url].metadata,
})
) for entry in entries[url]
]
except TypeError:
# return a simple string representation
# this shoud only occur if an Exception was raised
# from parsing this feed, resulting in a exc_info tuple
formatted[url] = [strings["fperror"].format(
url=url,
_escaped=EscapedDict(str, {"url": url}),
)]
report(ctx, strings["fperrorreport"],
url=url,
trace="".join(traceback.format_exception(*entries[url])),
)
except KeyError:
formatted[url] = [strings["reprerror"].format(
url=url,
_escaped=EscapedDict(str, {"url": url}),
)]
# remove feed from result if it is empty
if not formatted[url]:
del formatted[url]
return formatted
@run_async
def asap_update(ctx: CallbackContext):
chat_id = ctx.job.context
fc = ctx.dispatcher.chat_data[chat_id]["feeds"]["asap"]
reprs = ctx.dispatcher.chat_data[chat_id]["reprs"]
formatted = format_feeds(ctx, fc, reprs, strings["asapdefaultrepr"])
for url in formatted:
for entry in reversed(formatted[url]):
ctx.bot.send_message(
chat_id=chat_id,
text=entry,
disable_web_page_preview=False,
)
@run_async
def digest_update(ctx: CallbackContext):
chat_id = ctx.job.context
fc = ctx.dispatcher.chat_data[chat_id]["feeds"]["digest"]
reprs = ctx.dispatcher.chat_data[chat_id]["reprs"]
formatted = format_feeds(ctx, fc, reprs, strings["digestdefaultrepr"])
for url in formatted:
msgheader = strings["digestheader"].format(
feed=fc.feeds[url].metadata,
_escaped=EscapedDict(str, {"feed": fc.feeds[url].metadata}),
)
msgbody = "".join(reversed(formatted[url]))
ctx.bot.send_message(
chat_id=chat_id,
text="".join([msgheader, msgbody]),
)
# error handlers
def bot_error(upd: Update, ctx: CallbackContext):
# notify user
if upd.effective_message:
upd.effective_message.reply_text(strings["error"])
# report to devs
report(ctx, strings["errorreport"],
chat = (
upd.effective_user.mention_markdown() if upd.effective_user
else f"@{upd.effective_chat.username}" if upd.effective_chat
else "???"
),
trace=traceback.format_exc(),
)
# Re-raise the exception for the sake of the logger.
raise
def report(ctx: CallbackContext, template: str, **kwargs):
for dev_id in envs["devs"]:
# Markdown mode must be used as the Telegram API attempts to
# parse html <tag>-like terms even within <pre> tags.
ctx.bot.send_message(
chat_id=dev_id,
parse_mode=ParseMode.MARKDOWN,
text=template.format(**kwargs),
)
@run_async
def announce(upd: Update, ctx: CallbackContext):
if str(upd.effective_chat.id) in envs["devs"]:
message = " ".join(ctx.args)
for chat_id in ctx.dispatcher.chat_data:
ctx.bot.send_message(
chat_id=chat_id,
text=message,
)
else:
reply["unknowninput"](upd, ctx)
def main():
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO
)
updater = Updater(
token=envs["api_token"],
use_context=True,
defaults=Defaults(
parse_mode=ParseMode.HTML,
disable_web_page_preview=True,
),
persistence=PicklePersistence(filename=f"{envs['pkl_location']}/bot.pkl")
)
dispatcher = updater.dispatcher
dispatcher.add_handler(ConversationHandler(
entry_points=[
CommandHandler("start", start),
],
states={
MAIN: [
CommandHandler("settings", show_feeds),
CommandHandler("help", reply["help"]),
CommandHandler("start", reply["alreadyinitialized"]),
CommandHandler("add", add_command),
CommandHandler("remove", remove_command),
# CommandHandler("edit", edit_command),
],
ADD_URL: [
MessageHandler(Filters.entity(MessageEntity.URL), add_url_step),
CommandHandler("cancel", add_cancel_conversation),
MessageHandler(Filters.all, reply["add_urlwhat"]),
],
ADD_MODE: [
MessageHandler(Filters.regex(r"^(?i:digest)|(?i:asap)$"), add_mode_step),
CommandHandler("cancel", add_cancel_conversation),
MessageHandler(Filters.all, reply["add_modewhat"]),
],
REMOVE_URL: [
MessageHandler(Filters.entity(MessageEntity.URL), remove_command),
CommandHandler("cancel", remove_cancel_conversation),
MessageHandler(Filters.all, reply["remove_what"]),
],
# EDIT_URL: [],
# EDIT_REPR: [],
},
fallbacks=[
MessageHandler(Filters.all, reply["unknowninput"]),
],
persistent=True,
name="main_conv"
))
dispatcher.add_handler(CommandHandler("announce", announce))
dispatcher.add_handler(MessageHandler(Filters.all, reply["uninitialized"]))
dispatcher.add_error_handler(bot_error)
job_queue = dispatcher.job_queue
# enqueue update jobs for persisted users
for chat_id in dispatcher.chat_data:
# check if chat_data is actually populated by data from /start
if dispatcher.chat_data[chat_id]:
job_queue.run_repeating(
callback=asap_update,
interval=envs["asap_freq"],
first=random.randrange(0, envs["asap_freq"]), # random staggering
context=chat_id,
)
job_queue.run_daily(
callback=digest_update,
time=dispatcher.chat_data[chat_id]["digesttime"],
context=chat_id,
)
updater.start_polling(
allowed_updates=["message"],
)
updater.idle()
if __name__ == "__main__":
main()