예제 #1
0
    def order_start(self, command_body, arguments: list, action):
        if len(arguments) == 0:
            return "Use format: */ni food _URL_*"

        ordering_link = arguments[0]
        user = self.get_user_by_slack_user_id(command_body['user_id'])

        self.food_order_service.remove_incomplete_food_order_items(
            datetime.today(), command_body['channel_name'])

        post_at: int = round(
            (datetime.now() + CHECKOUT_REMINDER_IN).timestamp())
        reminder_response = self.slack_client.api_call(
            "chat.scheduleMessage",
            channel=command_body['channel_name'],
            user=user.slack_user_id,
            post_at=post_at,
            text=
            "@{} Looks like you forgot to order from {}.\nUrge your friends to place an order and call */ni order*"
            .format(command_body['user_name'], ordering_link),
            link_names=True)
        if not reminder_response["ok"]:
            self.logger.error(reminder_response)
            raise RuntimeError('failed')

        scheduled_message_id = reminder_response['scheduled_message_id']
        order = self.food_order_service.create_food_order(
            user, datetime.today(), ordering_link, scheduled_message_id,
            command_body['channel_name'])
        self.logger.info("Created order %d for user %s with reminder %s",
                         order.food_order_id, command_body['user_name'],
                         scheduled_message_id)

        order_prompt_response = self.slack_client.api_call(
            "chat.postMessage",
            channel=command_body['channel_name'],
            mrkdwn=True,
            as_user=True,
            attachments=[
                Attachment(
                    callback_id=string_helper.get_full_class_name(
                        FoodOrderPayload),
                    attachment_type="default",
                    text=get_user_name(user) + " orders from " + ordering_link,
                    color="#3AA3E3",
                    actions=[
                        Action(name="order-prompt",
                               text="I'm ordering right now",
                               type="button",
                               value="order-" + str(order.food_order_id)),
                        Action(name="order-prompt",
                               text="Not today",
                               type="button",
                               value="pas-" + str(order.food_order_id)),
                    ]).dump()
            ])
        if not order_prompt_response["ok"]:
            self.logger.error(order_prompt_response)
            raise RuntimeError('failed')
        return None
예제 #2
0
    def create_select_period_for_listing_model(self, command_body,
                                               inner_user_id):
        actions = [
            Action(name=inner_user_id
                   if inner_user_id is not None else command_body['user_id'],
                   text="Select time range...",
                   type=ActionType.SELECT.value,
                   options=[
                       TextSelectOption(text=tr.value, value=tr.value)
                       for tr in TimeRanges
                   ])
        ]
        attachments = [
            Attachment(text="Show record for",
                       fallback="Select time range to list saved time records",
                       color="#3AA3E3",
                       attachment_type="default",
                       callback_id=string_helper.get_full_class_name(
                           ListCommandPayload),
                       actions=actions)
        ]

        return Message(text="I'm going to list saved time records...",
                       response_type="ephemeral",
                       mrkdwn=True,
                       attachments=attachments)
예제 #3
0
    def prepare_debts_list(self, debts):
        attachments = []
        for debt in debts:
            user = self.user_service.get_user_by_id(debt.user_id)
            phone_text = "\nPay with BLIK using phone number: *" + user.phone + "*" if user.phone else ''

            actions = []
            debt_text = "You owe " + str(
                debt.debt) + " PLN for " + get_user_name(user) + phone_text
            if debt.debt < 0:
                actions.append(
                    Action(name="debt-pay",
                           text="I just paid " + str(-debt.debt) + " PLN",
                           type="button",
                           value="pay-" + str(debt.user_id)))
                debt_text = "You owe " + str(
                    -debt.debt) + " PLN for " + get_user_name(
                        user) + phone_text
            elif debt.debt > 0:
                debt_text = get_user_name(user) + " owes you " + str(
                    debt.debt) + " PLN"

            attachments.append(
                Attachment(callback_id=string_helper.get_full_class_name(
                    FoodOrderPayload),
                           attachment_type="default",
                           text=debt_text,
                           color="#3AA3E3",
                           actions=actions).dump())
        return attachments
    def create_project_selected_message(last_time_entries,
                                        selected_project) -> Message:

        actions = [
            Action(name="time_entries_list",
                   text="Select time entry",
                   type=ActionType.SELECT.value,
                   options=[
                       TextSelectOption(
                           text=string_helper.make_option_time_string(te),
                           value=te.time_entry_id) for te in last_time_entries
                   ],
                   confirm=Confirmation(title="Delete confirmation",
                                        text="Click 'Remove' to confirm",
                                        ok_text="Remove",
                                        dismiss_text="Cancel")),
        ]
        attachments = [
            Attachment(text="Select time entry to remove",
                       fallback="Select time entry",
                       color="#3AA3E3",
                       attachment_type="default",
                       callback_id=string_helper.get_full_class_name(
                           DeleteTimeEntryPayload),
                       actions=actions)
        ]
        return Message(text="There are the last time entries for *" +
                       selected_project.name + "*:",
                       response_type="ephemeral",
                       mrkdwn=True,
                       attachments=attachments)
 def create_vacation_selected_message(vacation_id):
     actions = [
         Action(name="remove",
                text="Remove",
                style="danger",
                type=ActionType.BUTTON.value,
                value=str(vacation_id)),
         Action(name="cancel",
                text="Cancel",
                type=ActionType.BUTTON.value,
                value="remove")
     ]
     attachments = [
         Attachment(text="Click 'Remove' to confirm:",
                    color="#3AA3E3",
                    attachment_type="default",
                    callback_id=string_helper.get_full_class_name(
                        RequestFreeDaysPayload),
                    actions=actions)
     ]
     return Message(text="Vacation will be removed...",
                    response_type="ephemeral",
                    mrkdwn=True,
                    attachments=attachments)
 def create_select_user_message(action_name, subaction_name,
                                user_options_list):
     actions = [
         Action(name=action_name + ":" + subaction_name,
                text="Select user...",
                type=ActionType.SELECT.value,
                options=user_options_list),
     ]
     attachments = [
         Attachment(text="Select user for " + action_name + "...",
                    fallback="Select user",
                    color="#3AA3E3",
                    attachment_type="default",
                    callback_id=string_helper.get_full_class_name(
                        ProjectAddPayload),
                    actions=actions)
     ]
     return Message(text="",
                    response_type="ephemeral",
                    attachments=attachments)
 def create_select_project_message(action_name, user_default_project_id,
                                   project_options_list):
     actions = [
         Action(name=str(action_name),
                text="Select project...",
                type=ActionType.SELECT.value,
                value=user_default_project_id,
                options=project_options_list),
     ]
     attachments = [
         Attachment(text="Select project first",
                    fallback="Select project",
                    color="#3AA3E3",
                    attachment_type="default",
                    callback_id=string_helper.get_full_class_name(
                        ProjectAddPayload),
                    actions=actions)
     ]
     return Message(text="I'm going to (un)assign user to the project...",
                    response_type="ephemeral",
                    attachments=attachments)
 def create_select_project_message(user_default_project_id,
                                   project_options_list):
     actions = [
         Action(name="projects_list",
                text="Select project...",
                type=ActionType.SELECT.value,
                value=user_default_project_id,
                options=project_options_list),
     ]
     attachments = [
         Attachment(text="Select project first",
                    fallback="Select project",
                    color="#3AA3E3",
                    attachment_type="default",
                    callback_id=string_helper.get_full_class_name(
                        DeleteTimeEntryPayload),
                    actions=actions)
     ]
     return Message(text="I'm going to remove time entry :wastebasket:...",
                    response_type="ephemeral",
                    attachments=attachments)
    def select_vacation_to_remove(self, command_body, argument, action):

        user = self.get_user_by_slack_user_id(command_body['user_id'])

        user_vacations: List[
            Vacation] = self.vacation_service.get_ten_newest_user_vacations(
                user.user_id)

        if not user_vacations:
            return Message(text="There is nothing to delete... ",
                           response_type="ephemeral",
                           mrkdwn=True).dump()

        actions = [
            Action(
                name="vacation_list",
                text="Select vacation to delete",
                type=ActionType.SELECT.value,
                options=[
                    TextSelectOption(
                        text=string_helper.make_option_vacations_string(vac),
                        value=vac.vacation_id) for vac in user_vacations
                ]),
        ]
        attachments = [
            Attachment(text="Select vacation to delete",
                       fallback="Select vacation to delete",
                       color="#3AA3E3",
                       attachment_type="default",
                       callback_id=string_helper.get_full_class_name(
                           RequestFreeDaysPayload),
                       actions=actions)
        ]
        return Message(
            text="I'm going to remove some vacations :wastebasket:...",
            response_type="ephemeral",
            mrkdwn=True,
            attachments=attachments).dump()
예제 #10
0
    def report_pre_dialog(self, command_body, arguments, action):

        message_text = "I'm going to generate report..."
        inner_user_id = None

        if len(arguments):
            user = arguments[0]
            inner_user_id = self.extract_slack_user_id(user)

            self.get_user_by_slack_user_id(inner_user_id)

        actions = [
            Action(name=inner_user_id
                   if inner_user_id is not None else command_body['user_id'],
                   text="Select time range...",
                   type=ActionType.SELECT.value,
                   options=[
                       TextSelectOption(text=tr.value, value=tr.value)
                       for tr in TimeRanges
                   ])
        ]

        attachments = [
            Attachment(text="Generate report for",
                       fallback="Select time range to report",
                       color="#3AA3E3",
                       attachment_type="default",
                       callback_id=string_helper.get_full_class_name(
                           ReportGenerateFormPayload),
                       actions=actions)
        ]

        return Message(text=message_text,
                       response_type="ephemeral",
                       mrkdwn=True,
                       attachments=attachments).dump()