-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypernet.py
executable file
·97 lines (77 loc) · 3.58 KB
/
hypernet.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
import argparse
import logging
import time
from core.bot import Bot
from core.message import Message
from core.plugin import Plugin
def load_connectors():
import connectors
from core.connector import Connector
return Plugin.discover(connectors, Connector)
class Framework(object):
def __init__(self):
self.__connectors = load_connectors()
self.__bots = dict()
self.__bots['innovate_bot_73'] = Bot('innovate_bot_73')
def main(self):
logging.info('The framework is starting')
try:
for connector in self.__connectors.values():
connector.start()
while True:
time.sleep(1)
# collect incoming messages
incoming_messages = list()
for connector in self.__connectors.values():
while True:
ret = connector.receive_message()
if ret is None: break
incoming_messages.append(ret)
# dispatch incoming messages
for (message, channel) in incoming_messages:
logging.info('incoming message "{}" {}'.format(message, channel))
dispatched = False
for bot in self.__bots.values():
if bot.get_name() in message.find_elements(message.__class__.Mention) \
or channel.get_conversation() is None \
or bot.get_name() == channel.get_receiver():
dispatched = True
logging.debug('dispatched to bot \033[32m{}\033[0m'.format(bot.get_name()))
bot.deliver_incoming_message(message, channel)
if not dispatched:
logging.debug('no dispatching (no matching bot)')
# trigger services for processing
for bot in self.__bots.values():
bot.on_schedule()
# collect outgoing messages
outgoing_messages = list()
for bot in self.__bots.values():
outgoing_messages += bot.fetch_outgoing_messages()
# dispatch outgoing messages
for (message, channel) in outgoing_messages:
logging.info('outgoing message "{}" {}'.format(message, channel))
dispatched = False
for connector in self.__connectors.values():
if channel.get_network() == connector.get_name():
dispatched = True
logging.debug('dispatched to network \033[32m{}\033[0m'.format(connector.get_name()))
connector.send_message(message, channel)
if not dispatched:
logging.debug('no dispatching (no matching network)')
except KeyboardInterrupt:
pass
finally:
for connector in self.__connectors.values():
connector.stop()
logging.info('The framework has stopped')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--log", default="/dev/stdout", help="output file for logs")
arguments = parser.parse_args()
logging.basicConfig(filename=arguments.log,
filemode='w',
level=logging.DEBUG,
format='[%(levelname)s]\t%(asctime)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
framework = Framework()
framework.main()