Ejemplo n.º 1
0
    def _react(self, message, emoji):
        channel = jsearch('metadata.source_channel', message)
        ts = jsearch('metadata.ts', message)

        call_slack_api(self.botThread.slack_client,
                       'reactions.add',
                       False,
                       'ok',
                       channel=channel,
                       name=emoji,
                       timestamp=ts)
Ejemplo n.º 2
0
 def _fetch_slack_emojis(self):
     custom_emojis = utils.call_slack_api(
         self.client,
         'emoji.list',
         False,
         'emoji'
         ) or {}
     return [*custom_emojis.keys(), *self.default_emojis]
Ejemplo n.º 3
0
    def _announce_secret_word(self, word, message):
        paid = self._payout(word, message)

        if not paid:
            return False

        blocks = [
            {
                'type': 'header',
                'text': {
                    'type': 'plain_text',
                    'text': 'YOU SAID THE SECRET WORD'
                }
            },
            {
                'type': 'image',
                'image_url': ('https://www.themarysue.com/wp-content/uploads'
                              '/2015/10/secret-word-ph.gif'),
                'alt_text': ''
            },
            {
                'type': 'section',
                'fields': [{
                    'type': 'mrkdwn',
                    'text': (f'The secret word was `{word}`. \n '
                             f'You earned {paid} {self.name}')
                }]
            }
        ]
        utils.call_slack_api(
            self.botThread.slack_client,
            'chat.postMessage',
            False,
            None,
            channel=utils.jsearch('metadata.source_channel || ""', message),
            as_user=True,
            thread_ts=utils.jsearch(
                'metadata.thread_ts || metadata.ts || ""', message),
            reply_broadcast=True,
            blocks=json.dumps(blocks)
        )
Ejemplo n.º 4
0
    def _get_participants(self, oldest):
        participants = []
        for channel in self.participant_channels:
            participants += utils.call_slack_api(
                self.botThread.slack_client,
                'conversations.history',
                True,
                'messages[?!contains(keys(@), `"bot_id"`)].user',
                channel=channel,
                oldest=f'{oldest}.000000'
            )

        return [
            p for p in participants
            if p not in self.pool_excludes + ['USSLACKBOT']
        ]
Ejemplo n.º 5
0
    def _init_board(self, name, cfg):
        get_history = False
        f_type = cfg.get('file_type', 'json')
        f_name = f'{name.lower()}.{f_type}'
        cfg['path'] = os.path.join(DATA_DIR, f_name)
        data = load_file(cfg['path'], f_type=f_type, default=[])
        channels = cfg.pop('channels', ['general'])
        channels = [self.botThread.get_channel_id_by_name(c) for c in channels]

        if not os.path.isfile(cfg['path']):
            get_history = True

        for channel in channels:
            if channel not in self.channels:
                self.channels[channel] = set()

            self.channels[channel].add(name)

        self.boards[name] = {'data': data, 'config': cfg}

        if get_history is True:
            for channel in channels:
                messages = call_slack_api(self.botThread.slack_client,
                                          'conversations.history',
                                          True,
                                          'messages',
                                          channel=channel)
                transform = ('[].{text: text, metadata: {source_channel: '
                             f'`{channel}`'
                             ', source_user: user, ts: ts}}')
                messages = jsearch(transform, messages)

                for message in messages:
                    self._process_board(message, name)

                LOGGER.debug('Channel history processed')
Ejemplo n.º 6
0
    def _generate_secret_word(self, periods=None):
        if not periods or periods < 1:
            periods = 1

        fillups = self.db.pool_history.query(limit=periods + 1, sort='id,desc')
        fillups = (fillups[periods]['fillup_ts'], fillups[1]['next_fillup_ts'])

        users = self.db.balance.query(limit=10,
                                      sort='balance,asc',
                                      fields='user',
                                      _filter={
                                          'user__op': {
                                              'startswith': 'U',
                                              'notin_': self.pool_excludes
                                          }
                                      })
        users = utils.jsearch('[].user', users)
        messages = []

        for channel in self.secret_word_channels:
            messages += utils.call_slack_api(self.botThread.slack_client,
                                             'conversations.history',
                                             True,
                                             'messages',
                                             total_limit=10000,
                                             limit=1000,
                                             latest=fillups[1],
                                             oldest=fillups[0],
                                             channel=channel)

        user_search = ('[?contains([{}], user) '
                       '&& !contains(lower(text), `moin`) '
                       '&& !starts_with(text, `!ak`)]').format(', '.join(
                           [f'`{u}`' for u in users]))
        messages = utils.jsearch(user_search, messages)
        word_pool = {}

        for message in messages:
            user = message['user']
            if user not in word_pool:
                word_pool[user] = []

            word_pool[user] += re.findall(r':[a-zA-Z0-9_-]+:', message['text'])
            string = re.sub(r'(:[a-zA-Z0-9_-]+:)|(<@U[A-Z0-9]+>)', '',
                            message['text'])
            words = re.findall(r'\b\w+\b', string)
            word_pool[user] += [
                w.lower() for w in words
                if len(w) > 3 and w.lower() not in self.common_words
            ]

        users = [k for k in word_pool.keys() if word_pool[k]]
        last_ten = self.db.secret_word.query(sort='id,desc', limit=10)
        last_ten = utils.jsearch('[].secret_word', last_ten)
        if len(users) > 2:
            word = last_ten[0]
            i = 0
            while word in last_ten:
                user = choice(users)
                word = choice(word_pool[user])
                i += 1

                if i > 9:
                    word, user = self._generate_secret_word(periods + 1)
        else:
            word, user = self._generate_secret_word(periods + 1)

        return word, user
Ejemplo n.º 7
0
 def _fetch_slack_emojis(self):
     return utils.call_slack_api(self.client, 'emoji.list', False, 'emoji')