コード例 #1
0
    def process_trello_board_conv(self, bot, update, user_data):
        logger.info("Got to process_trello_board")

        trello_token = user_data["trello_token"]

        chosen_board_name = update.message.text.split("(")[0].strip()
        chosen_board_id = update.message.text.split("(")[1].replace(")", "")

        trello = Trello(trello_token)
        starred_boards = trello.get_starred_boards()

        if starred_boards is None:
            return self.error(update, user_data, "invalid trello token")

        candidate_boards = [v for k, v in starred_boards.items() if chosen_board_id in k]
        final_board = None
        for candidate_board in candidate_boards:
            if candidate_board['name'] == chosen_board_name:
                final_board = candidate_board
                break
        if final_board is None:
            return self.error(update, user_data, "No valid board found")
        else:
            chosen_board_id = final_board['id']
            chosen_board_name = final_board['name']

        board_lists = trello.get_board_lists(chosen_board_id)
        if board_lists is None:
            return self.error(update, {}, "Trello token expired. Restart doing /setup and "
                                          "then saving your stuff again.")

        inbox_list_id = None
        for k, l in board_lists.items():
            if l['name'] == DEFAULT_LIST_NAME:
                inbox_list_id = l['id']
                break
        else:
            if len(board_lists.items()) > 0:
                inbox_list_id = [v for k, v in board_lists.items()][0]['id']

        self.setup_user(tg_id=update.message.from_user.id,
                        trello_token=trello_token,
                        chosen_board_id=chosen_board_id,
                        chosen_board_name=chosen_board_name,
                        inbox_list_id=inbox_list_id)

        update.message.reply_text("Setup completed. You can now fully use the bot.")
        return ConversationHandler.END
コード例 #2
0
    def append_card(self, update, content, card_name,
                    list_name=None,
                    list_id=None,
                    content_type='text'):

        if (list_name is None) & (list_id is None):
            raise Exception("No list provided!")

        trello = Trello(self._USER_SETUPS[self.get_tg_id(update)]['trello_token'])

        if list_id is not None:
            list_name = DEFAULT_LIST_NAME
        else:
            board_lists = trello.get_board_lists(self._USER_SETUPS[self.get_tg_id(update)]['board_id'])
            if board_lists is None:
                return self.error(update, {}, "Trello token expired. Restart doing /setup and "
                                              "then saving your stuff again.")

            if list_name[0] == "_":
                list_name = list_name[1:]
                # Check if it already exists
                for k, l in board_lists.items():
                    if l['name'] == list_name:
                        list_id = l['id']
                        break
                else:
                    # NEW LIST!
                    list_id = trello.create_list_in_board(list_name,
                                                          self._USER_SETUPS[self.get_tg_id(update)]['board_id'])
            else:
                for k, l in board_lists.items():
                    if l['name'] == list_name:
                        list_id = l['id']
                        break
                else:
                    return self.error(update, {}, "No list found matching your choice. Restart please.")

        result = trello.create_card_in_list(list_id, card_name, content, content_type)
        logger.info("Done! Created card with ID: {}".format(result))
        update.message.reply_text("Done! Put the {} into #{} as *{}".format(content_type,
                                                                            list_name,
                                                                            card_name))
コード例 #3
0
def process_anything_text(bot, update, user_data):
    if not app.is_user_setup(update):
        update.message.reply_text("You are not authenticated yet. Use /setup please.")
        user_data.clear()
        return

    content = update.message.text
    content_type = 'text'
    if len(update.message.entities) > 0:
        if update.message.entities[0].type == 'url':
            content_type = 'url'

    user_data['_content'] = content
    user_data['_content_type'] = content_type
    user_data['_card_name'] = str(content)[:DEFAULT_CARD_NAME_LEN] if content_type != 'url' else content

    trello = Trello(app._USER_SETUPS[app.get_tg_id(update)]['trello_token'])
    board_lists = trello.get_board_lists(app._USER_SETUPS[app.get_tg_id(update)]['board_id'])

    if board_lists is None:
        board_lists = []

    update.message.reply_text(
        "Where do you want to save it?",
        reply_markup=ReplyKeyboardMarkup(
            [
                [
                    '#{list_name}'.format(list_name=l['name']) for k, l in board_lists.items()
                ],
                ['/cancel']
            ],
            one_time_keyboard=True,
        ),
    )

    return _CONV_STATE_CHOOSE_LIST