-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
41 lines (32 loc) · 1.24 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
from msgcenter.irc_backend import IrcBackend
from msgcenter.whatsapp_backend import WhatsAppBackend
from msgcenter.message import MessageSink
from msgcenter.dispatcher import Dispatcher
import json
import six
import sys
DEFAULT_CONFIG_FILENAME = "config.json"
def apply_config(filename, dispatcher):
with open(filename) as file:
config = json.loads(file.read())
for name, conf in six.iteritems(config["backend"]):
if conf["type"] == "irc":
dispatcher.register(name, IrcBackend(name, conf["server"], conf["port"], conf["nick"]))
elif conf["type"] == "whatsapp":
dispatcher.register(name, WhatsAppBackend(name, conf["phone"], conf["password"]))
for name, sink_defs in six.iteritems(config["group"]):
sinks = []
for sink in sink_defs:
new_sink = MessageSink(sink["backend"], sink["channel"])
sinks.append(new_sink)
dispatcher.add_group(name, sinks)
def main():
d = Dispatcher()
if len(sys.argv) == 2:
config_filename = sys.argv[1]
else:
config_filename = DEFAULT_CONFIG_FILENAME
apply_config(config_filename, d)
d.start()
if __name__ == "__main__":
main()