from slackclient import SlackClient
from time import sleep
import eventpool
from event import Event

class Bot:
    def __init__(self, api_token, default_channel='random'):
        self.api = SlackClient(token=api_token)
        self.cur_channel = default_channel
        # 今後デフォルトで設定するかもしれないところ API じゃできないっぽい
        self.owner = ''
        self.user_dm = ''
        self.handlers = []
        self.pool = eventpool.EventPool()
    def add_eventhandler(self, handler):
        self.handlers.append(handler)
        handler.start()
    def mainloop(self):
        try:
            if self.api.rtm_connect():
                while True:
                    data_list = self.api.rtm_read()
                    for data in data_list:
                        self.on_event(Event(self, data))
#                   self.pool.check_queue()
                    sleep(1)
        except KeyboardInterrupt:
            for handler in self.handlers:
                handler.event_queue.join()
    def on_event(self, event):
        if 'bot_id' in event.data:
            return
        for handler in self.handlers:
            handler.put_event(event)
#            if handler.accept(event):
#                record = eventpool.EventPoolRecord(handler, event)
#                self.pool.register(record)
    def post_message(self, message, channel=None, dest_user=None):
        """Post a Message
        Description:
            This method posts a message to channel (if channel is None, self.cur_channel).
        Arguments:
            message (str): message to post
        """
        print("post message")
        if dest_user is None:
            text = message
        else:
            text = '@{0} {1}'.format(dest_user, message)
        if channel is None:
            channel = self.cur_channel
        result = self.api.api_call('chat.postMessage', text=text, link_names=True, channel=channel, as_user=True)
        return result
    def add_reaction(self, emoji, channel, timestamp):
        result = self.api.api_call('reactions.add', name=emoji, channel=channel, timestamp=timestamp)
        return result
    def remove_reaction(self, emoji, channel, timestamp):
        result = self.api.api_call('reactions.remove', name=emoji, channel=channel, timestamp=timestamp)
        return result

@@ -92,16 +92,73 @@
 rt Event
+%0Afrom websocket import WebSocketConnectionClosedException
 %0A%0Aclass 
@@ -606,16 +606,40 @@
 (self):%0A
+        while True:%0A    
         
@@ -643,16 +643,20 @@
    try:%0A
+    
         
@@ -686,16 +686,60 @@
 nect():%0A
+                    print('connected.')%0A    
         
@@ -778,16 +778,20 @@
         
+    
 data_lis
@@ -830,24 +830,28 @@
             
+    
 for data in 
@@ -861,16 +861,20 @@
 a_list:%0A
+    
         
@@ -934,32 +934,37 @@
                 
+     
 self.pool.check_
@@ -991,16 +991,20 @@
         
+    
 sleep(1)
@@ -1016,38 +1016,320 @@
     
-except KeyboardInterrupt:%0A    
+        else:%0A                    print('connection failed.')%0A                    sleep(10)%0A            except WebSocketConnectionClosedException:%0A                print('reconnecting...')%0A                continue%0A            except KeyboardInterrupt:%0A                print('shutting down')%0A                break%0A
     
@@ -1354,36 +1354,32 @@
  self.handlers:%0A
-    
             hand

Add handling for WebSocketConnectionClosedException