Beispiel #1
0
    def start(self, bot, update, job_queue):
        """Start the bot, schedule tasks and printing welcome message."""
        logging.info(f'Requested /start from user {update.message.chat_id}')

        # If security check passes
        for ctx in security_check(bot, update):
            # self.last_check = aware_now()
            # Welcome message
            ctx.send(
                f'*Welcome!*\n'
                f'TrelloBot will now make your life better. ', )

            # List boards, blacklisted and not
            count = Counter()
            abm, bbm = '*Allowed boards*', '*Not allowed boards*'
            with ctx.spawn(abm) as aem, ctx.spawn(bbm) as bem:
                with ctx.spawn('*Status*: fetching data') as stm:
                    for b in self._trello.fetch_boards():
                        if b.blacklisted:
                            bem.append(f'\n - {b} {b.id}')
                        else:
                            c, _ = self._update_due(b.id, ctx, job_queue)
                            count += c  # Keep stats
                            aem.append(f'\n - {b} {b.id}')
                    stm.override(f'*Status*: Done. ' + self._report(count))

            ctx.send(f'Refreshing every {TrelloBot.check_int} mins')

            # Start repeated job
            job_queue.run_repeating(
                self.check_updates,
                TrelloBot.check_int * 60.0,
                context=(update, job_queue),
            )
            self.started = True
Beispiel #2
0
 def bl_board(self, bot, update):
     """Blacklist boards."""
     logging.info('Requested /blb')
     for ctx in security_check(bot, update):
         bids = update.message.text.strip().split()
         for bid in bids:
             self._trello.blacklist_brd(bid)
Beispiel #3
0
    def ls(self, bot, update):
        """List the requested resources from Trello."""
        logging.info('Requested /ls')
        for ctx in security_check(bot, update):
            ctx.send(f'ECHO: {update.message}')
            target = update.message.text.strip().split()
            # List organizations if nothing was specified
            if len(target) == 1:
                with ctx.spawn('Listing Organizations:\n') as msg:
                    later = []
                    for o in self._trello.fetch_orgs():
                        if o.blacklisted:
                            later.append(f' - {o.name}')
                        else:
                            msg.append(f' - {o.name}\n')
                    # Print blacklisted organizations after
                    if later:
                        msg.append('Blacklisted:\n')
                        msg.append('\n'.join(later))

            elif len(target) == 2 and target[1] in self._trello.org_names():
                org = target[1]
                with ctx.spawn(f'Listing Boards in org {org}:\n') as msg:
                    later = []
                    for b, bl in self._trello.fetch_boards(org):
                        if bl:
                            later.append(f' - {b.name}')
                        else:
                            msg.append(f' - {b.name}\n')
                    if later:
                        msg.append('Blacklisted:\n')
                        msg.append('\n'.join(later))
            else:
                ctx.send('Sorry, I cannot list anything else right now.')
Beispiel #4
0
 def bl_org(self, bot, update):
     """Blacklist organizations."""
     logging.info('Requested /blo')
     for ctx in security_check(bot, update):
         # Get org IDs to whitelist
         oids = update.message.text.strip().split()
         for oid in oids:
             self._trello.blacklist_org(oid)
Beispiel #5
0
 def today_due(self, bot, update):
     """Send user a list with cards due today."""
     for ctx in security_check(bot, update):
         with ctx.spawn('*Due today*') as em:
             # Show upcoming cards
             for dd in self._dues:
                 # Skip past due
                 if dd.date() != aware_now().date():
                     continue
                 for c in self._dues[dd]:
                     em.append(f'\n - {c}')
Beispiel #6
0
def test_security_check():
    """Test security stuff."""
    bot = MagicMock()
    update = MagicMock()

    # Set my chat id
    update.message.chat_id = int(open('allowed.txt').read().strip())
    # Check that right chat id will pass
    for ctx in security_check(bot, update):
        # We should enter the block
        assert isinstance(ctx, Messenger)

    # Change to unauth user
    update.message.chat_id = 123456
    with patch('trellobot.security.Messenger') as mockmsg:
        # Check that other chat id will not pass
        for ctx in security_check(bot, update):
            # We should NOT enter in this block
            assert False
        # Check that message was sent
        assert mockmsg.call_count == 1
Beispiel #7
0
 def demo(self, bot, update):
     """Demo buttons and callbacks."""
     # If security check passes
     for ctx in security_check(bot, update):
         ctx.send(f'A *markdown* message :)',
                  keyboard=[
                      [
                          {
                              'text': 'Greetings',
                              'callback_data': 'puny human'
                          },
                      ],
                      [
                          {
                              'text': 'Adieu',
                              'callback_data': 'my friend'
                          },
                      ],
                  ])
Beispiel #8
0
 def upcoming_due(self, bot, update):
     """Send user a list with upcoming cards."""
     logging.info('Requested /upcoming')
     for ctx in security_check(bot, update):
         logging.info('Authorized user requested upcoming dues.')
         # Check if we loaded due cards
         if not hasattr(self, '_dues'):
             ctx.send('No data fetched, did you start?')
             return
         # Check all cards for upcoming dues
         pdm, cdm = '*Past dues*:', '*Dues*:'
         with ctx.spawn(pdm) as pem, ctx.spawn(cdm) as fem:
             # Show upcoming cards
             for dd in self._dues:
                 # Past dues in a separated list
                 if dd < aware_now():
                     for c in self._dues[dd]:
                         pem.append(f'\n - {c}')
                 else:
                     for c in self._dues[dd]:
                         fem.append(f'\n - {c}')