def set_description(update, context: CallbackContext, user: Operator):
    user.deal_data.reserve_desc = update.message.text
    BH.update_deal_reserve(user)

    TgCommons.send_mdv2(update.effective_user,
                        GlobalTxt.DEAL_UPDATED.format(user.deal_data.deal_id))
    return TgCommons.restart(update, context)
示例#2
0
def render_cur_page(update, context, user: Operator, edit_message=False):
    couriers_num = len(user.send.couriers)
    page_number = user.send.page_number
    user.send.total_pages = math.ceil(couriers_num / UserData.COURIERS_PER_PAGE)

    courier_surname_starts_with_elt = Txt.COURIERS_LIST_SURNAME_STARTS_ELT \
        .format(user.send.courier_surname_starts_with) if user.send.courier_surname_starts_with else ''

    message = Txt.COURIERS_LIST_HEADER.format(courier_surname_starts_with_elt)

    if user.send.total_pages > 1:
        message += Txt.COURIERS_PAGE_HEADER.format(page_number + 1, user.send.total_pages)

    couriers_tuples = list(user.send.couriers.items())
    page = couriers_tuples[page_number * UserData.COURIERS_PER_PAGE:
                           (page_number + 1) * UserData.COURIERS_PER_PAGE]

    keyboard = []

    for bitrix_id, p_data in page:
        keyboard.append([InlineKeyboardButton(p_data, callback_data=bitrix_id)])

    if page_number > 0:
        keyboard.append([InlineKeyboardButton(Txt.PREV_PAGE_TEXT, callback_data=Txt.PREV_PAGE_CB)])

    if page_number < user.send.total_pages - 1:
        if page_number > 0:  # if prev page button has been added
            keyboard[-1].append(InlineKeyboardButton(Txt.NEXT_PAGE_TEXT, callback_data=Txt.NEXT_PAGE_CB))
        else:
            keyboard.append([InlineKeyboardButton(Txt.NEXT_PAGE_TEXT, callback_data=Txt.NEXT_PAGE_CB)])

    if edit_message:
        TgCommons.edit_mdv2(update.effective_message, message, keyboard)
    else:
        TgCommons.send_mdv2(update.effective_user, message, keyboard)
示例#3
0
def append_postcard(update, context: CallbackContext, user: Operator):
    photos: List[PhotoSize] = update.message.photo

    photo_big = photos[-1]

    unique_id_big = photo_big.file_unique_id
    photo_content_big = photo_big.get_file().download_as_bytearray()
    file_extension_big = photo_big.get_file().file_path.split('.')[-1]

    if photo_content_big:
        # store raw photo data to save it on disk later
        user.equip.add_deal_postcard(
            Photo(name_big=unique_id_big + '_B.' + file_extension_big,
                  data_big=photo_content_big))
    else:
        logger.error('No photo content big/small from user %s',
                     update.message.from_user.id)

    if user.state == State.EQUIP_SET_POSTCARD_FACIAL:
        TgCommons.send_mdv2(update.effective_user,
                            Txt.DEAL_REQUEST_POSTCARD_REVERSE_SIDE)
        return State.EQUIP_SET_POSTCARD_REVERSE
    else:  # reverse loaded
        request_checklist(update, context, user)
        return State.EQUIP_SET_CHECKLIST
示例#4
0
def load_checklist_photo(update, context, user: Operator):
    if not user.equip.photos:
        keyboard = [[
            InlineKeyboardButton(text=Txt.FINISH_PHOTO_LOADING,
                                 callback_data=Txt.FINISH_PHOTO_LOADING_CB)
        ]]
        TgCommons.send_mdv2(update.effective_user, Txt.NO_PHOTOS_TEXT,
                            keyboard)
        return State.EQUIP_SET_PHOTOS

    photos: List[PhotoSize] = update.message.photo

    photo = photos[-1]
    unique_id = photo.file_unique_id
    photo_content = photo.get_file().download_as_bytearray()
    file_extension = photo.get_file().file_path.split('.')[-1]

    encoded_data = base64.b64encode(photo_content).decode('ascii')
    user.deal_data.photo_data = encoded_data
    user.deal_data.photo_name = unique_id + '.' + file_extension

    StorageHandlers.save_deal(user, user.deal_data.deal_id)
    BH.update_deal_image(user)

    TgCommons.send_mdv2(update.effective_user,
                        GlobalTxt.DEAL_UPDATED.format(user.deal_data.deal_id))
    return TgCommons.restart(update, context)
示例#5
0
def set_deal_number(update, context: CallbackContext, user: Operator):
    deal_id = update.message.text

    result = BH.set_deal_number(user, deal_id)

    if result == BW.BW_NO_SUCH_DEAL:
        TgCommons.send_mdv2(update.effective_user,
                            GlobalTxt.NO_SUCH_DEAL.format(deal_id))
        return None
    elif result == SendBH.BH_ALREADY_HAS_COURIER:
        with BW.COURIERS_LOCK:
            courier = Utils.prepare_external_field(BW.COURIERS,
                                                   user.deal_data.courier_id)

        keyboard = [[
            InlineKeyboardButton(
                text=SendTxt.CHANGE_COURIER_BUTTON_TEXT,
                callback_data=SendTxt.CHANGE_COURIER_BUTTON_CB)
        ],
                    [
                        InlineKeyboardButton(
                            text=Txt.LEAVE_COURIER_BUTTON_TEXT.format(
                                courier.upper()),
                            callback_data=SendTxt.LEAVE_COURIER_BUTTON_CB)
                    ]]
        TgCommons.send_mdv2(
            update.effective_user,
            SendTxt.COURIER_ALREADY_SET_HEADER.format(courier.upper(),
                                                      deal_id), keyboard)
        return State.SEND_CHANGE_COURIER

    SendHandlers.send_couriers_header(update, context, user)
    return State.SEND_SET_COURIER
示例#6
0
def leave_courier(update, context: CallbackContext, user: Operator):
    courier = Utils.prepare_external_field(BW.COURIERS,
                                           user.deal_data.courier_id,
                                           BW.COURIERS_LOCK)
    message = Txt.COURIER_LEFT.format(user.deal_data.deal_id, courier)
    TgCommons.send_mdv2(update.effective_user, message)
    return TgCommons.restart(update, context)
def view_reserve_photo(update: Update, context: CallbackContext,
                       user: Operator):
    cur_deal_index = user.florist_order.cur_deal_index
    deals = user.florist_order.deals_list

    d = deals[cur_deal_index]

    with BW.OAUTH_LOCK:
        access_token = context.bot_data[cfg.BOT_ACCESS_TOKEN_PERSISTENT_KEY]

        deal_photos_links = [
            creds.BITRIX_MAIN_PAGE +
            el.replace('auth=', 'auth=' + access_token)
            for el in d.order_reserve
        ]

        msg_text = Txt.RESERVE_VIEWER_TEXT
        if d.reserve_desc:
            msg_text += d.reserve_desc + '\n'

        media_list = [InputMediaPhoto(media=el) for el in deal_photos_links]
        keyboard = [[
            InlineKeyboardButton(text=Txt.EQUIP_BUTTON_TEXT,
                                 callback_data=Txt.EQUIP_BUTTON_CB)
        ],
                    [
                        InlineKeyboardButton(
                            text=Txt.BACK_TO_ORDER_BUTTON_TEXT,
                            callback_data=Txt.BACK_TO_ORDER_BUTTON_CB)
                    ]]

        TgCommons.send_media_group(update.effective_user, media_list)
        TgCommons.send_mdv2(update.effective_user, msg_text, keyboard)
def request_description(update, context: CallbackContext, user: Operator):
    if not user.reserve.photos:
        TgCommons.send_mdv2(update.effective_user, Txt.NO_PHOTOS_TEXT)
        return None

    TgCommons.send_mdv2(update.effective_user, Txt.REQUEST_DESCRIPTION)
    return State.PROCESS_DESCRIPTION
def finish_deal(update, context, user):
    message = Txt.APPROVE_DEAL_FINISH_TEXT.format(user.deal_data.deal_id)
    keyboard = [[
        InlineKeyboardButton(Txt.APPROVE_DEAL_FINISH_BUTTON_TEXT,
                             callback_data=Txt.APPROVE_DEAL_FINISH_BUTTON_CB)
    ]]
    TgCommons.send_mdv2(update.effective_user, message, keyboard)
    return State.COURIER_FINISHES_DEAL
def warehouse_return_deal(update, context, user):
    message = Txt.APPROVE_WAREHOUSE_RETURN_TEXT.format(user.deal_data.deal_id)
    keyboard = [[
        InlineKeyboardButton(
            Txt.APPROVE_WAREHOUSE_RETURN_BUTTON_TEXT,
            callback_data=Txt.APPROVE_WAREHOUSE_RETURN_BUTTON_CB)
    ]]
    TgCommons.send_mdv2(update.effective_user, message, keyboard)
    return State.COURIER_RETURNS_DEAL_TO_WAREHOUSE
def render_cur_deal(update, context, user):
    deal_id = user.deal_data.deal_id
    d = user.data.get_deal(deal_id)

    deal_terminal = Txt.DEAL_TERMINAL_ELT if d.terminal_needed else ''
    deal_change_sum = Txt.DEAL_CHANGE_ELT.format(
        d.change_sum) if d.change_sum is not None else ''
    deal_to_pay = Txt.DEAL_TO_PAY_ELT.format(
        d.to_pay) if d.to_pay is not None else ''

    message = Txt.DEAL_VIEW_TEMPLATE.format(
        deal_id, d.time, d.date, d.address,
        Txt.ADDRESS_RESOLUTION_LINK + Utils.escape_mdv2_textlink(d.address),
        d.flat, d.recipient_name, d.recipient_phone, d.recipient_phone,
        d.district, d.delivery_comment, d.incognito, deal_terminal,
        deal_change_sum, deal_to_pay, d.subdivision, d.sender, d.source,
        d.contact_phone, d.contact_phone)

    keyboard = [[
        InlineKeyboardButton(Txt.ORDER_CONTENT_BUTTON_TEXT,
                             callback_data=Txt.ORDER_CONTENT_BUTTON_CB)
    ]]

    if user.data.deals_type == UserData.DealsType.DELIVERS_TODAY:
        keyboard.extend(
            [[
                InlineKeyboardButton(Txt.FINISH_DEAL_BUTTON_TEXT,
                                     callback_data=Txt.FINISH_DEAL_BUTTON_CB)
            ],
             [
                 InlineKeyboardButton(
                     Txt.WAREHOUSE_RETURN_DEAL_BUTTON_TEXT,
                     callback_data=Txt.WAREHOUSE_RETURN_DEAL_BUTTON_CB)
             ]])

    keyboard.append([
        InlineKeyboardButton(
            Txt.BACK_TO_CUR_DEALS_LIST_BUTTON_TEXT,
            callback_data=Txt.BACK_TO_CUR_DEALS_LIST_BUTTON_CB)
    ])

    with BW.OAUTH_LOCK:
        access_token = context.bot_data[cfg.BOT_ACCESS_TOKEN_PERSISTENT_KEY]

        deal_photos_links = [
            creds.BITRIX_MAIN_PAGE +
            el.replace('auth=', 'auth=' + access_token)
            for el in d.order_big_photos
        ]

        media_list = [InputMediaPhoto(media=el) for el in deal_photos_links]

        if media_list:
            TgCommons.send_media_group(update.effective_user, media_list)

        TgCommons.send_mdv2(update.effective_user, message, keyboard)
示例#12
0
def choose_courier(update, context, user: Operator):
    courier_id = context.match.group(1)
    user.deal_data.courier_id = courier_id

    BH.send_deal(user)

    courier = Utils.prepare_external_field(BW.COURIERS, courier_id, BW.COURIERS_LOCK)
    message = Txt.DEAL_UPDATED.format(user.deal_data.deal_id, courier)
    TgCommons.send_mdv2(update.effective_user, message)
    return TgCommons.restart(update, context)
def view_order_content(update, context, user):
    d = user.data.get_deal(user.deal_data.deal_id)

    message = d.order
    keyboard = [[
        InlineKeyboardButton(Txt.BACK_TO_CUR_DEAL_BUTTON_TEXT,
                             callback_data=Txt.BACK_TO_CUR_DEAL_BUTTON_CB)
    ]]

    TgCommons.send_mdv2(update.effective_user, message, keyboard)
    return State.COURIER_VIEWS_DEAL_ORDER
def choose_florist(update, context, user: Operator):
    florist_id = context.match.group(1)
    user.deal_data.florist_id = florist_id

    BH.update_deal_florist(user)

    with BW.FLORISTS_LOCK:
        florist = Utils.prepare_external_field(
            BW.FLORISTS, user.deal_data.florist_id).upper()

    message = Txt.DEAL_UPDATED.format(user.deal_data.deal_id, florist)
    TgCommons.send_mdv2(update.effective_user, message)
    return TgCommons.restart(update, context)
def finish_deal_helper(update, context, user):
    deal_id = user.deal_data.deal_id

    result = BitrixHandlers.finish_deal(user)

    if result == BW.BW_WRONG_STAGE:
        TgCommons.send_mdv2(update.effective_user,
                            Txt.DEAL_IS_IN_WRONG_STAGE_TXT.format(deal_id))
        return user.state

    TgCommons.send_mdv2(update.effective_user,
                        Txt.DEAL_FINISHED.format(deal_id))
    return TgCommons.restart(update, context)
def no_reserve_approve(update, context: CallbackContext, user: Operator):
    photo_stub_name = 'no_reserve_needed.png'
    photo_stub_path = pathlib.Path(
        __file__).parent.resolve() / 'data' / photo_stub_name

    with open(photo_stub_path, 'rb') as f:
        stub_bytes = f.read()
        user.reserve.add_deal_photo(Photo(photo_stub_name, stub_bytes))

    BH.update_deal_no_reserve(user)

    TgCommons.send_mdv2(update.effective_user,
                        GlobalTxt.DEAL_UPDATED.format(user.deal_data.deal_id))
    return TgCommons.restart(update, context)
示例#17
0
def set_deal_number(update, context: CallbackContext, user: Operator):
    deal_id = update.effective_message.text

    result = BH.set_deal_number(user, deal_id)

    if result == BW.BW_NO_SUCH_DEAL:
        TgCommons.send_mdv2(update.effective_user,
                            GlobalTxt.NO_SUCH_DEAL.format(deal_id))
        return None

    if result == BW.BW_WRONG_STAGE:
        TgCommons.send_mdv2(update.effective_user, Txt.WRONG_DEAL_STAGE)
        return None

    # deal has been already equipped
    if user.deal_data.stage == BFM.DEAL_IS_EQUIPPED_STATUS_ID:
        user.equip.repeating = True
        keyboard = [[
            InlineKeyboardButton(text=Txt.EQUIP_REPEATEDLY_BUTTON_TEXT,
                                 callback_data=Txt.EQUIP_REPEATEDLY_BUTTON_CB)
        ]]
        TgCommons.send_mdv2(
            update.effective_user,
            Txt.APPROVE_ALREADY_EQUIPPED_DEAL_TXT.format(deal_id), keyboard)
        return State.EQUIP_REPEATEDLY_APPROVE

    TgCommons.send_mdv2(update.effective_user, Txt.ASK_FOR_PHOTO_TEXT)
    return State.EQUIP_SET_PHOTOS
def set_warehouse_return_reason(update, context, user):
    deal_id = user.deal_data.deal_id
    user.data.warehouse_return_reason = update.message.text

    result = BitrixHandlers.return_to_warehouse(user)

    if result == BW.BW_WRONG_STAGE:
        TgCommons.send_mdv2(update.effective_user,
                            Txt.DEAL_IS_IN_WRONG_STAGE_TXT.format(deal_id))
        return user.state

    TgCommons.send_mdv2(update.effective_user,
                        Txt.DEAL_RETURNED_TO_WAREHOUSE.format(deal_id))
    return TgCommons.restart(update, context)
def festive_decision(update: Update, context, user):
    if not user:
        TgCommons.send_mdv2_chat(update.effective_chat,
                                 Txt.USER_NOT_FOUND.format(Utils.prepare_str(update.effective_user.full_name)))
        return None

    user.festive_data.clear()

    action = context.match.group(1)
    deal_id = context.match.group(2)

    if action == HttpTxt.FESTIVE_APPROVE_BUTTON_KEY:
        BH.approve_deal(deal_id)
        TgCommons.send_mdv2_chat(update.effective_chat, Txt.APPROVED_HEADER.format(deal_id))
        TgCommons.edit_mdv2(update.effective_message, msg_text=Txt.APPROVED_HEADER.format('')
                                                               + update.effective_message.text_markdown_v2,
                            need_cancel=False)
        return ConversationHandler.END
    elif action == HttpTxt.FESTIVE_DECLINE_BUTTON_KEY:
        user.festive_data.deal_id = deal_id
        user.festive_data.deal_message = update.effective_message

        BH.get_info(context, deal_id, user)

        msg = Txt.REQUEST_DECLINE_COMMENT.format(deal_id)
        TgCommons.send_mdv2_chat(chat=update.effective_chat, msg_text=msg,
                                 reply_id=update.callback_query.message.message_id)
        return State.WRITING_DECLINE_COMMENT
示例#20
0
def request_checklist(update, context, user: Operator):
    terminal_elt = Txt.DEAL_TERMINAL_ELT if user.deal_data.terminal_needed else ''
    change_elt = Txt.DEAL_CHANGE_ELT.format(
        user.deal_data.change_sum) if user.deal_data.change_sum else ''

    courier = Utils.prepare_external_field(BW.COURIERS,
                                           user.deal_data.courier_id,
                                           BW.COURIERS_LOCK)

    message = Txt.CHECKLIST_REQUEST.format(user.deal_data.deal_id, courier,
                                           user.deal_data.payment_type,
                                           terminal_elt, change_elt,
                                           user.deal_data.to_pay)

    TgCommons.send_mdv2(update.effective_user, message)
def render_cur_deal(update: Update, user: Operator):
    cur_deal_index = user.florist_order.cur_deal_index
    deals = user.florist_order.deals_list

    if not deals:
        TgCommons.send_mdv2(update.effective_user, Txt.NO_ORDERS)
        return None

    d = deals[cur_deal_index]
    keyboard = []

    if d.order_reserve:
        keyboard.append([
            InlineKeyboardButton(
                text=Txt.VIEW_RESERVE_PHOTO_BUTTON_TEXT,
                callback_data=Txt.VIEW_RESERVE_PHOTO_BUTTON_CB)
        ])

    keyboard.append([
        InlineKeyboardButton(text=Txt.EQUIP_BUTTON_TEXT,
                             callback_data=Txt.EQUIP_BUTTON_CB)
    ])

    if cur_deal_index > 0:
        keyboard.append([
            InlineKeyboardButton(text=Txt.PREV_ORDER_BUTTON_TEXT,
                                 callback_data=Txt.PREV_ORDER_BUTTON_CB)
        ])

    if cur_deal_index < len(deals) - 1:
        if cur_deal_index > 0:  # if prev page button has been added
            keyboard[-1].append(
                InlineKeyboardButton(text=Txt.NEXT_ORDER_BUTTON_TEXT,
                                     callback_data=Txt.NEXT_ORDER_BUTTON_CB))
        else:
            keyboard.append([
                InlineKeyboardButton(text=Txt.NEXT_ORDER_BUTTON_TEXT,
                                     callback_data=Txt.NEXT_ORDER_BUTTON_CB)
            ])

    postcard_elt = Txt.POSTCARD_ELT.format(
        d.postcard_text) if d.postcard_text else ''
    deal_text = Txt.ORDER_TEMPLATE.format(cur_deal_index + 1, len(deals),
                                          d.deal_id, d.supply_type, d.order,
                                          d.order_comment, postcard_elt, d.sum,
                                          d.date, d.time)

    TgCommons.send_mdv2(update.effective_user, deal_text, keyboard)
示例#22
0
def send_festive_deal_message(
        bot, deal_id, deal_stage, deal_order, deal_date, deal_time, deal_sum,
        deal_accepted, deal_source, deal_contact, deal_subdivision,
        deal_reserve_desc, deal_delivery_type, deal_district, deal_address,
        deal_delivery_comment, deal_pay_method, deal_pay_type, deal_prepaid,
        deal_terminal, deal_change, deal_to_pay, deal_pay_status, photo_urls):

    reserve_desc = Txt.DEAL_RESERVE_DESC_ELT.format(deal_reserve_desc) \
        if deal_reserve_desc != GlobalTxt.FIELD_IS_EMPTY_PLACEHOLDER else ''
    prepaid = Txt.DEAL_PREPAID_ELT.format(
        deal_prepaid) if deal_pay_type == BFM.DEAL_PAY_PREPAID_FRIENDLY else ''
    terminal = Txt.DEAL_TERMINAL_ELT.format(
        deal_terminal
    ) if deal_pay_type == BFM.DEAL_PAY_PERSONAL_FRIENDLY else ''
    change = Txt.DEAL_CHANGE_ELT.format(
        deal_change) if deal_terminal != BFM.DEAL_PAY_TERMINAL_FRIENDLY else ''

    deal_message = Txt.DEAL_FESTIVE_PROCESSED_TEMPLATE.format(
        deal_id, deal_stage, deal_order, deal_date, deal_time, deal_sum,
        deal_accepted, deal_source, deal_contact, deal_subdivision,
        reserve_desc, deal_delivery_type, deal_district, deal_address,
        deal_delivery_comment, deal_pay_method, deal_pay_type, prepaid,
        terminal, change, deal_to_pay, deal_pay_status)

    keyboard = [[
        InlineKeyboardButton(text=Txt.FESTIVE_APPROVE_BUTTON_TEXT,
                             callback_data=Txt.FESTIVE_APPROVE_BUTTON_KEY +
                             Cmd.CMD_DELIMETER + deal_id)
    ],
                [
                    InlineKeyboardButton(
                        text=Txt.FESTIVE_DECLINE_BUTTON_TEXT,
                        callback_data=Txt.FESTIVE_DECLINE_BUTTON_KEY +
                        Cmd.CMD_DELIMETER + deal_id)
                ]]

    chat = Chat(bot=bot,
                id=creds.FESTIVE_APPROVAL_CHAT_ID,
                type=Chat.SUPERGROUP)

    # 1024 symbols of caption only, if more -> need a message
    if photo_urls:
        media_list = [InputMediaPhoto(media=el) for el in photo_urls]
        bot.send_media_group(chat_id=creds.FESTIVE_APPROVAL_CHAT_ID,
                             media=media_list)

    TgCommons.send_mdv2_chat(chat, deal_message, keyboard)
def error_handler(update, context: CallbackContext):
    try:
        logger.error(msg="Exception while handling Telegram update:",
                     exc_info=context.error)

        # don't confuse user with particular error data
        if update:
            if update.effective_chat.type == Chat.PRIVATE:
                # don't confuse user with particular errors data
                TgCommons.send_mdv2(update.effective_user,
                                    GlobalTxt.UNKNOWN_ERROR)
            elif update.effective_chat.type in (Chat.GROUP, Chat.SUPERGROUP):
                TgCommons.send_mdv2_chat(update.effective_chat,
                                         GlobalTxt.UNKNOWN_ERROR)
    except Exception as e:
        logger.error(msg="Exception while handling lower-level exception:",
                     exc_info=e)
def send_florists_header(update, context, user):
    deal_info = Txt.DEAL_INFO_TEMPLATE.format(
        user.deal_data.deal_id, user.deal_data.order, user.deal_data.contact,
        user.deal_data.florist, user.deal_data.order_received_by,
        user.deal_data.incognito, user.deal_data.order_comment,
        user.deal_data.delivery_comment, user.deal_data.total_sum,
        user.deal_data.payment_type, user.deal_data.payment_method,
        user.deal_data.payment_status, user.deal_data.prepaid,
        user.deal_data.to_pay, user.deal_data.courier,
        user.deal_data.order_type)

    keyboard = [[
        InlineKeyboardButton(text=Txt.SHOW_ALL_FLORISTS_BUTTON_TEXT,
                             callback_data=Txt.SHOW_ALL_FLORISTS_BUTTON_CB)
    ]]
    TgCommons.send_mdv2(update.effective_user,
                        deal_info + '\n\n' + Txt.CHOOSE_FLORIST_HEADER,
                        keyboard)
示例#25
0
def finish_photo_loading(update: Update, context: CallbackContext,
                         user: Operator):
    if not user.equip.photos:
        keyboard = [[
            InlineKeyboardButton(text=Txt.FINISH_PHOTO_LOADING,
                                 callback_data=Txt.FINISH_PHOTO_LOADING_CB)
        ]]
        TgCommons.send_mdv2(update.effective_user, Txt.NO_PHOTOS_TEXT,
                            keyboard)
        return None

    if user.deal_data.has_postcard:
        TgCommons.send_mdv2(
            update.effective_user,
            Txt.DEAL_HAS_POSTCARD.format(user.deal_data.postcard_text))
        return State.EQUIP_SET_POSTCARD_FACIAL
    else:
        request_checklist(update, context, user)
        return State.EQUIP_SET_CHECKLIST
示例#26
0
def append_photo(update, context: CallbackContext, user: Operator):
    photos: List[PhotoSize] = update.message.photo

    photo_big = photos[-1]

    if len(photos) > 1:
        photo_small = photos[1]
    else:
        photo_small = photos[0]

    unique_id_big = photo_big.file_unique_id
    photo_content_big = photo_big.get_file().download_as_bytearray()
    file_extension_big = photo_big.get_file().file_path.split('.')[-1]

    file_extension_small = photo_small.get_file().file_path.split('.')[-1]
    unique_id_small = photo_small.file_unique_id
    photo_content_small = photo_small.get_file().download_as_bytearray()

    if photo_content_big and photo_content_small:
        logger.info(
            f'Appending photo: big photo content size: {len(photo_content_big)}'
        )
        # store raw photo data to save it on disk later
        user.equip.add_deal_photo(
            Photo(unique_id_small + '_S.' + file_extension_small,
                  unique_id_big + '_B.' + file_extension_big,
                  photo_content_small, photo_content_big))

        keyboard = [[
            InlineKeyboardButton(text=Txt.FINISH_PHOTO_LOADING,
                                 callback_data=Txt.FINISH_PHOTO_LOADING_CB)
        ]]
        TgCommons.send_mdv2(update.effective_user, Txt.PHOTO_LOADED_TEXT,
                            keyboard)

        logger.info(
            f'User bitrix id {user.bitrix_user_id} uploaded photo {unique_id_small}.{file_extension_small}'
        )
    else:
        logger.error('No photo content big/small from user %s',
                     update.message.from_user.id)

    return None
def restart_operator(update, context: CallbackContext, user):
    message = GlobalTxt.MENU_TEXT
    keyboard = [
        [InlineKeyboardButton(text=GlobalTxt.MENU_DEAL_PROCESS_BUTTON_TEXT,
                              callback_data=GlobalTxt.MENU_DEAL_PROCESS_BUTTON_CB)],
        [InlineKeyboardButton(text=GlobalTxt.MENU_DEAL_SET_FLORIST_BUTTON_TEXT,
                              callback_data=GlobalTxt.MENU_DEAL_SET_FLORIST_BUTTON_CB)],
        [InlineKeyboardButton(text=GlobalTxt.MENU_DEAL_EQUIP_BUTTON_TEXT,
                              callback_data=GlobalTxt.MENU_DEAL_EQUIP_BUTTON_CB)],
        [InlineKeyboardButton(text=GlobalTxt.MENU_DEAL_SEND_BUTTON_TEXT,
                              callback_data=GlobalTxt.MENU_DEAL_SEND_BUTTON_CB)],
        [InlineKeyboardButton(text=GlobalTxt.MENU_DEAL_COURIER_BUTTON_TEXT,
                              callback_data=GlobalTxt.MENU_DEAL_COURIER_BUTTON_CB)],
        [InlineKeyboardButton(text=GlobalTxt.MENU_DEAL_FLORIST_ORDERS_BUTTON_TEXT,
                              callback_data=GlobalTxt.MENU_DEAL_FLORIST_ORDERS_BUTTON_CB)]
    ]

    TgCommons.send_mdv2(update.effective_user, message, keyboard)
    return State.IN_OPERATOR_MENU
def stat_unprocessed(context: CallbackContext):
    unprocessed, subdivs = BH.stat_unprocessed()
    msg = ''

    if unprocessed:
        msg += Txt.UNPROCESSED_STAT_TEMPLATE.format(unprocessed)

    for s_name, s_count in subdivs.items():
        msg += Txt.DECLINED_SUBDIV_STAT_TEMPLATE.format(s_name, s_count)
        subdiv_chat_id = creds.FESTIVE_UNAPPROVED_SUBDIVS.get(s_name)
        subdiv_chat = Chat(bot=context.bot,
                           id=subdiv_chat_id,
                           type=Chat.SUPERGROUP)
        subdiv_msg = Txt.SPECIFIC_SUBDIV_STAT_TEMPLATE.format(s_count)
        TgCommons.send_mdv2_chat(subdiv_chat, subdiv_msg)

    chat = Chat(bot=context.bot,
                id=creds.FESTIVE_APPROVAL_CHAT_ID,
                type=Chat.SUPERGROUP)
    TgCommons.send_mdv2_chat(chat, msg)
def set_deal_number(update, context: CallbackContext, user: Operator):
    deal_id = update.effective_message.text

    result = BH.set_deal_number(user, deal_id)

    if result == BW.BW_NO_SUCH_DEAL:
        TgCommons.send_mdv2(update.effective_user,
                            GlobalTxt.NO_SUCH_DEAL.format(deal_id))
        return None
    elif result == BH.BH_ALREADY_HAS_FLORIST:
        with BW.FLORISTS_LOCK:
            florist = Utils.prepare_external_field(
                BW.FLORISTS, user.deal_data.florist_id).upper()

        keyboard = [[
            InlineKeyboardButton(text=Txt.CHANGE_FLORIST_BUTTON_TEXT,
                                 callback_data=Txt.CHANGE_FLORIST_BUTTON_CB)
        ],
                    [
                        InlineKeyboardButton(
                            text=Txt.LEAVE_FLORIST_BUTTON_TEXT.format(florist),
                            callback_data=Txt.LEAVE_FLORIST_BUTTON_CB)
                    ]]
        TgCommons.send_mdv2(
            update.effective_user,
            Txt.FLORIST_ALREADY_SET_HEADER.format(florist, deal_id), keyboard)
        return State.SETTING_FLORIST_CHANGE_FLORIST
    elif result == BW.BW_WRONG_STAGE:
        TgCommons.send_mdv2(update.effective_user, Txt.WRONG_DEAL_STAGE)
        return None

    send_florists_header(update, context, user)
    return State.SETTING_FLORIST_SET_FLORIST
def set_deal_number(update, context: CallbackContext, user: Operator):
    deal_id = update.message.text
    result = BH.set_deal_number(user, deal_id)

    if result == BW.BW_NO_SUCH_DEAL:
        TgCommons.send_mdv2(update.effective_user,
                            GlobalTxt.NO_SUCH_DEAL.format(deal_id))
        return None

    if result == BW.BW_WRONG_STAGE:
        TgCommons.send_mdv2(update.effective_user, Txt.WRONG_DEAL_STAGE)
        return None

    keyboard = [[
        InlineKeyboardButton(text=Txt.RESERVE_YES_TXT,
                             callback_data=Txt.RESERVE_YES_KEY)
    ],
                [
                    InlineKeyboardButton(
                        text=Txt.SWITCH_STAGE_WAITING_FOR_SUPPLY_TXT,
                        callback_data=Txt.SWITCH_STAGE_WAITING_FOR_SUPPLY_KEY)
                ],
                [
                    InlineKeyboardButton(
                        text=Txt.SWITCH_STAGE_PROCESSED_TXT,
                        callback_data=Txt.SWITCH_STAGE_PROCESSED_KEY)
                ]]

    TgCommons.send_mdv2(update.effective_user,
                        Txt.WILL_YOU_RESERVE.format(deal_id), keyboard)
    return State.PROCESS_WILL_YOU_RESERVE