-
Notifications
You must be signed in to change notification settings - Fork 1
/
le.py
73 lines (52 loc) · 1.99 KB
/
le.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
"""Load and run all available handlers."""
import logging
from argparse import ArgumentParser
from typing import ( # noqa
Generator,
List,
)
from pkg_resources import ( # noqa
EntryPoint,
iter_entry_points,
)
from telegram.ext import Updater
def create_argument_parser() -> ArgumentParser:
"""Return a new argument parser."""
entry_point_iterator = iter_entry_points(
'le.parsers') # type: Generator[EntryPoint, None, None]
other_parsers = [
ep.load()
for ep in entry_point_iterator
] # type: List[ArgumentParser]
parser = ArgumentParser(
parents=other_parsers, fromfile_prefix_chars='@')
parser.add_argument('telegram_token')
parser.add_argument('-d', '--debug', action='store_true')
return parser
def run():
"""Run all handlers in the group ``le.handlers``."""
args = create_argument_parser().parse_args()
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG if args.debug else logging.INFO
)
logging.getLogger('telegram.ext').setLevel(logging.INFO)
logging.getLogger('telegram.bot').setLevel(logging.INFO)
logging.getLogger('telegram.vendor').setLevel(logging.INFO)
updater = Updater(token=args.telegram_token)
entry_points_iterator = iter_entry_points('le.handlers')
handlers = ((ep.name, ep.load()) for ep in entry_points_iterator)
for name, handler in handlers:
updater.dispatcher.add_handler(handler)
logging.getLogger().debug(
'Loaded {}: {}'.format(name, handler)
)
entry_points_iterator = iter_entry_points('le.handlers.chat')
chat_handlers = ((ep.name, ep.load()) for ep in entry_points_iterator)
for group, (name, handler) in enumerate(chat_handlers, start=1):
updater.dispatcher.add_handler(handler, group)
logging.getLogger().debug(
'Loaded {}: {}'.format(name, handler)
)
updater.start_polling()
updater.idle()