import time
import logging
import traceback
import os
from os.path import isfile, join

from slack_clients import SlackClients
from messenger import Messenger
from event_handler import RtmEventHandler
from text_corpus import gen_text_corpus

logger = logging.getLogger(__name__)


def spawn_bot():
    return SlackBot()


class SlackBot(object):
    def __init__(self, token=None):
        """Creates Slacker Web and RTM clients with API Bot User token.

        Args:
            token (str): Slack API Bot User token (for development token set in env)
        """
        self.last_ping = 0
        self.keep_running = True
        if token is not None:
            self.clients = SlackClients(token)

        training_files = []
        for f in os.listdir('resources/training'):
             if isfile(join('resources/training', f)):
                 training_files.append(join('resources/training', f))

        self.trump_corpus = gen_text_corpus(training_files)
        logger.debug('first trump sent: {}'.format(self.trump_corpus.seq_sent[0]))


    def start(self, resource):
        """Creates Slack Web and RTM clients for the given Resource
        using the provided API tokens and configuration, then connects websocket
        and listens for RTM events.

        Args:
            resource (dict of Resource JSON): See message payloads - https://beepboophq.com/docs/article/resourcer-api
        """
        logger.debug('Starting bot for resource: {}'.format(resource))
        if 'resource' in resource and 'SlackBotAccessToken' in resource['resource']:
            res_access_token = resource['resource']['SlackBotAccessToken']
            self.clients = SlackClients(res_access_token)

        if self.clients.rtm.rtm_connect():
            logging.info(u'Connected {} to {} team at https://{}.slack.com'.format(
                self.clients.rtm.server.username,
                self.clients.rtm.server.login_data['team']['name'],
                self.clients.rtm.server.domain))

            msg_writer = Messenger(self.clients)
            event_handler = RtmEventHandler(self.clients, msg_writer)

            while self.keep_running:
                for event in self.clients.rtm.rtm_read():
                    try:
                        logging.info('Before Before Before')
                        event_handler.handle(event)
                        logging.info('After After After')                        

                    except:
                        err_msg = traceback.format_exc()
                        logging.error('Unexpected error: {}'.format(err_msg))
                        # ignore: msg_writer.write_error(event['channel'], err_msg)
                        continue

                self._auto_ping()
                time.sleep(.5)

        else:
            logger.error('Failed to connect to RTM client with token: {}'.format(self.clients.token))

    def _auto_ping(self):
        # hard code the interval to 3 seconds
        now = int(time.time())
        if now > self.last_ping + 3:
            self.clients.rtm.server.ping()
            self.last_ping = now

    def stop(self, resource):
        """Stop any polling loops on clients, clean up any resources,
        close connections if possible.

        Args:
            resource (dict of Resource JSON): See message payloads - https://beepboophq.com/docs/article/resourcer-api
        """
        self.keep_running = False

@@ -2250,32 +2250,33 @@
                 
+#
 logging.info('Be
@@ -2364,32 +2364,33 @@
                 
+#
 logging.info('Af

Update slack_bot.py