from threading import Thread
from time import sleep
from queue import Queue
import re

class EventHandler:
    def __init__(self):
        self.inner_thread = None
        self.event_queue = Queue()
    def start(self):
        assert self.inner_thread is None
        thread = Thread(target=self.loop, daemon=True)
        thread.start()
        self.inner_thread = thread
    def loop(self):
        while True:
            event = self.event_queue.get()
            self.on_event(event)
            self.event_queue.task_done()
    def put_event(self, event):
        if self.accept(event):
            self.event_queue.put(event)
    def accept(self, event):
        raise NotImplementedError()
    def on_event(self, event):
        raise NotImplementedError()

class MessageHandler(EventHandler):
    def __init__(self):
        EventHandler.__init__(self)
    def accept(self, event):
        return event.data['type'] == 'message' and 'text' in event.data

class SimpleMessageHandler(MessageHandler):
    def __init__(self, regex_str, callback):
        MessageHandler.__init__(self)
        self.matcher = re.compile(regex_str)
        self.callback = callback
        self.last_post = None
    def accept(self, event):
        return MessageHandler.accept(self, event) and self.matcher.search(event.data['text'])
    def on_event(self, event):
        message = self.callback(event, event.data['text'])
        if message:
            self.last_post = event.post_message(message)

@@ -852,24 +852,54 @@
 nit__(self)%0A
+        self.last_post = None%0A
     def acce
@@ -907,32 +907,32 @@
 t(self, event):%0A
-
         return e
@@ -986,16 +986,310 @@
 ent.data
+%0A    def on_event(self, event):%0A        text = event.data%5B'text'%5D%0A        if not 'subtype' in event.data:%0A            message = self.on_chat(event, text)%0A            if message:%0A                self.last_post = event.post_message(message)%0A    def on_chat(self, event, text):%0A        return None
 %0A%0Aclass 
@@ -1491,38 +1491,8 @@
 ack%0A
-        self.last_post = None%0A
     
@@ -1613,36 +1613,35 @@
 t'%5D)%0A    def on_
-even
+cha
 t(self, event):%0A
@@ -1629,32 +1629,38 @@
 chat(self, event
+, text
 ):%0A        messa
@@ -1650,33 +1650,30 @@
 t):%0A        
-message =
+return
  self.callba
@@ -1706,81 +1706,4 @@
 '%5D)%0A
-        if message:%0A            self.last_post = event.post_message(message)%0A

Add on_chat method to MessageHandler