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))

            event_handler = RtmEventHandler(self.clients)

            while self.keep_running:
                for event in self.clients.rtm.rtm_read():
                    try:
                        event_handler.handle(event)
                    except:
                        err_msg = traceback.format_exc()
                        logging.error('Unexpected error: {}'.format(err_msg))
                        continue

                self._auto_ping()
                time.sleep(.1)

        else:
            logger.error('Failed to connect to RTM client with token: {}'.format(self.clients.token))
Example #2
0
    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)
 def setUp(self):
     self.clients = SlackClients('na')
     self.logger = logging.getLogger(__name__)
     self.actions = {
         'say': say,
         'error': error,
         'merge': merge,
     }
     self.wit_client = Wit("na", self.actions, self.logger)
     self.msg_writer = Messenger(self.clients)
     pass
 def setUp(self):
     # Set up environment to allow for misc testing
     self.clients = SlackClients('na')
     self.logger = logging.getLogger(__name__)
     self.actions = {
         'say': say,
         'error': error,
         'merge': merge,
     }
     self.wit_client = Wit("na", self.actions, self.logger)
     self.msg_writer = Messenger(self.clients)
     self.msg_writer.send_message = MagicMock(return_value=None)
     pass
Example #5
0
    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]))
Example #6
0
    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)
        """
        '''
        def set_glob_convostarted():
            global self.conversation_started    # Needed to modify global copy of globvar
        def set_convo_step():
            global self.convo_step    # Needed to modify global copy of globvar

        self.set_glob_convostarted();
        self.set_convo_step();
        '''
        self.last_ping = 0
        self.keep_running = True
        if token is not None:
            self.clients = SlackClients(token)