def done_orders_handler(update: Update, context: CallbackContext): current_user = update.callback_query.message.chat.username order_id = update.callback_query.data.split('-')[1] current_order = post_sql_query(f'SELECT * FROM ORDERS WHERE order_id ='\ f' "{order_id}";')[0] customer_details = post_sql_query(f'SELECT * FROM USERS WHERE username ='******' "{current_order[1]}";')[0] if current_order[10] == 'Выполнен': update.callback_query.edit_message_text( text='Заказ отменен.\nПерейти в меню - /menu') else: update.callback_query.edit_message_text( text=f'Заявка отмечена как выполненная. Ожидайте подтверждения от '\ f'@{customer_details[0]}\nПерейти в меню - /menu') inline_buttons = InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton(text='Подтвердить!', callback_data=f'confirm-{order_id}') ], ], ) context.bot.send_message(chat_id=customer_details[-1], text=f'Заявка №{order_id} отмечена выполненной '\ f'пользователем @{current_user}. '\ f'Нажмите "Подтвердить!" для закрытия заявки.\n'\ f'Перейти в меню - /menu', reply_markup=inline_buttons,) return ConversationHandler.END
def confirmed_orders_handler(update: Update, context: CallbackContext): current_user = update.callback_query.message.chat.username order_id = update.callback_query.data.split('-')[1] current_order = post_sql_query(f'SELECT * FROM ORDERS WHERE order_id ='\ f' "{order_id}";')[0] post_sql_query(f'UPDATE ORDERS SET status = "Выполнен" '\ f'WHERE order_id = "{order_id}";') if current_user != current_order[1]: carrier_details = post_sql_query(f'SELECT * FROM USERS WHERE username ='******' "{current_order[9]}";')[0] context.bot.send_message( chat_id=carrier_details[-1], text=f'Заявка №{order_id} закрыта пользователем @{current_user}') update.callback_query.edit_message_text(text='Заявка закрыта.') return ConversationHandler.END
def handle_text_message(event): current_user = event.source.user_id text = event.message.text user_list = post_sql_query('SELECT user_id,current_stage FROM USERDATA') if user_in_db(current_user, user_list)[0]: stage = user_in_db(current_user, user_list)[1] handlers.message_handler(event, stage, current_user, text) else: time.sleep(5) line_bot_api.reply_message( event.reply_token,TextSendMessage(text=resources.sentence2)) time.sleep(2) line_bot_api.push_message( current_user, TextSendMessage(text=resources.sentence2_1)) sql_query = f'INSERT INTO USERDATA (user_id, current_stage, stage1) '\ f'VALUES ("{current_user}","3","{text}");' post_sql_query(sql_query)
def order_acception_handler(update: Update, context: CallbackContext): current_user = update.callback_query.message.chat.username order_id = update.callback_query.data.split('-')[1] current_order = post_sql_query(f'SELECT * FROM ORDERS WHERE order_id ='\ f' "{order_id}";')[0] customer_details = post_sql_query(f'SELECT * FROM USERS WHERE username ='******' "{current_order[1]}";')[0] carrier_details = post_sql_query(f'SELECT * FROM USERS WHERE username ='******' "{current_user}";')[0] now = datetime.now() diff = (datetime.strptime(now.strftime("%m/%d/%Y, %H:%M:%S"), "%m/%d/%Y, %H:%M:%S") - datetime.strptime(current_order[-1], "%m/%d/%Y, %H:%M:%S")).days if diff >= 3: update.callback_query.edit_message_text( text='Заказ уже неавктивен.\nПерейти в меню - /menu') post_sql_query(f'UPDATE ORDERS SET status = "Выполнен" '\ f'WHERE order_id = "{order_id}";') return ConversationHandler.END if current_order[10] == 'Взят в работу': update.callback_query.edit_message_text( text='Заказ уже взят в работу.\nПерейти в меню - /menu') if current_order[10] == 'Выполнен': update.callback_query.edit_message_text( text='Заказ отменен.\nПерейти в меню - /menu') if update.callback_query.data[:5] == 'order': post_sql_query(f'UPDATE ORDERS SET status = "Взят в работу", '\ f'carrier_username = "******" WHERE order_id ='\ f' "{current_order[0]}";') update.callback_query.edit_message_text( text=f'Вы подтвердили транспортировку по заявке №{current_order[0]}\n'\ f'Заказчик: @{current_order[1]}' ) inline_buttons = InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton(text='Выполнено!', callback_data=f'done-{order_id}') ], ], ) update.callback_query.message.reply_text( text='Когда заявка будет выполнена, нажмите '\ '"Выполнено!" чтобы закрыть ее. ', reply_markup=inline_buttons, ) context.bot.send_message(chat_id=customer_details[-1], text=f'Заявка №{current_order[0]} была принята '\ f'пользователем @{current_user}.') else: if customer_details[2] == 'role-2': update.callback_query.message.reply_text( text=f'Отправлено диспетчером!\n'\ f'Имя: {customer_details[1]}\n'\ f'Номер телефона: {customer_details[6]}\n'\ f'Telegram: @{customer_details[0]}' ) else: update.callback_query.message.reply_text( text=f'Название компании: {customer_details[4]}\n'\ f'Имя: {customer_details[1]}\n'\ f'Номер телефона: {customer_details[6]}\n'\ f'Telegram: @{customer_details[0]}\n' f'ИНН: {customer_details[5]}\n'\ f'Тип компании: {OWNERSHIP_MAP[customer_details[3]]}\n'\ ) return ConversationHandler.END
def confirmation_handler(update: Update, context: CallbackContext): context.user_data[PAYMENT] = PAYMENT_MAP[update.callback_query.data] current_user = update.callback_query.message.chat.username startpoint = context.user_data[STARTPOINT] endpoint = context.user_data[ENDPOINT] weight = context.user_data[WEIGHT] cargo_type = context.user_data[CARGO] start_date = context.user_data['CALENDAR'] price = context.user_data[PRICE] payment_type = context.user_data[PAYMENT] weight_limitations = context.user_data[WEIGHT_LIMITATIONS] mileage = context.user_data[MILEAGE] logger.info('user_data: %s', context.user_data) now = datetime.now() register_order(username=current_user, startpoint=startpoint, endpoint=endpoint, weight=weight, cargo_type=cargo_type, start_date=start_date, price=price, payment_type=payment_type, carrier_username='', status='Ожидает исполнителя', weight_limitations=weight_limitations, mileage=mileage, reg_date=now.strftime("%m/%d/%Y, %H:%M:%S")) order_id = post_sql_query(f'SELECT order_id FROM ORDERS WHERE username ='******' "{current_user}"')[-1][0] inline_buttons = InlineKeyboardMarkup(inline_keyboard=[ [InlineKeyboardButton(text='Перейти в меню', callback_data='menu-re')], [ InlineKeyboardButton(text='Отменить заявку!', callback_data=f'confirm-{order_id}') ], ], ) update.callback_query.edit_message_text( text=f'Ваша заявка успешно оформлена.'\ f'№{order_id}\nПункт погрузки: {startpoint}\n'\ f'Пункт выгрузки: {endpoint}\n'\ f'Расстояние: {mileage}км\n'\ f'Вес и тип: {weight}т {cargo_type}\n'\ f'Тариф: {price} грн/т, {payment_type}\nДата отгрузки: '\ f'{start_date}\nОграничения: {weight_limitations}', reply_markup=inline_buttons, ) inline_buttons = InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton(text='Принять заказ', callback_data=f'order-{order_id}') ], [ InlineKeyboardButton(text='Информация о заказчике', callback_data=f'customer-{order_id}') ], ], ) try: carriers_list = post_sql_query(f'SELECT chat_id FROM USERS WHERE role ='\ f' "1"')[0] for carrier in carriers_list: context.bot.send_message(chat_id=carrier, text=f'Новая заявка!\nПункт погрузки: {startpoint}\n'\ f'Пункт выгрузки: {endpoint}\n'\ f'Расстояние: {mileage}км\n'\ f'Вес и тип: {weight}т {cargo_type}\n'\ f'Тариф: {price} грн/т, {payment_type}\nДата отгрузки: '\ f'{start_date}\nОграничения: {weight_limitations}', reply_markup=inline_buttons,) except IndexError: pass inline_buttons = InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton(text='Посмотреть детали', url=f'tg://resolve?domain={TG_APP_NAME}') ], ], ) context.bot.send_message(chat_id=ADMIN, text=f'Новая заявка! От @{current_user}\n'\ f'Пункт погрузки: {startpoint}\n'\ f'Пункт выгрузки: {endpoint}\n'\ f'Расстояние: {mileage}км\n'\ f'Вес и тип: {weight}т {cargo_type}\n'\ f'Тариф: {price} грн/т, {payment_type}\nДата отгрузки: '\ f'{start_date}\nОграничения: {weight_limitations}') context.bot.send_message(chat_id=GROUP, text=f'Новая заявка!\n'\ f'Пункт погрузки: {startpoint}\n'\ f'Пункт выгрузки: {endpoint}\n'\ f'Расстояние: {mileage}км\n'\ f'Вес и тип: {weight}т {cargo_type}\n'\ f'Тариф: {price} грн/т, {payment_type}\nДата отгрузки: '\ f'{start_date}\nОграничения: {weight_limitations}', reply_markup=inline_buttons,) return ConversationHandler.END
def menu_choice_handler(update: Update, context: CallbackContext): current_user = update.callback_query.message.chat.username choice = update.callback_query.data try: user_role = post_sql_query(f'SELECT role FROM USERS WHERE username ='******' "{current_user}"')[0][0] except IndexError: request.reply_text('Нажмите /start для заполнения анкеты!', ) return ConversationHandler.END if choice == 'new_order': update.callback_query.edit_message_text( text='Введите пункт погрузки (область, город/cело/смт и тд.)') return STARTPOINT elif choice == 'previous_orders': query_customer = post_sql_query(f'SELECT * FROM ORDERS WHERE status = '\ f'"Выполнен" AND username = "******"') query_carrier = post_sql_query(f'SELECT * FROM ORDERS WHERE status = '\ f'"Выполнен" AND carrier_username = "******"') if user_role == 'role-2' or user_role == 'role-3': for row in query_customer: update.callback_query.edit_message_text( text=f'Пункт погрузки: {row[2]}\n'\ f'Пункт выгрузки: {row[3]}\n'\ f'Расстояние: {row[12]}км\n'\ f'Вес и тип: {row[4]}т {row[5]}\n'\ f'Ограничения: {row[11]}\n' f'Тариф: {row[7]} грн/т, {row[8]}\n'\ f'Дата отгрузки: {row[6]}\n'\ f'Исполнитель: @{row[9]}' ) else: for row in query_carrier: update.callback_query.edit_message_text( text=f'Пункт погрузки: {row[2]}\n'\ f'Пункт выгрузки: {row[3]}\n'\ f'Расстояние: {row[12]}км\n'\ f'Вес и тип: {row[4]}т, {row[5]}\n'\ f'Ограничения: {row[11]}\n' f'Тариф: {row[7]} грн/т {row[8]}\n'\ f'Дата отгрузки: {row[6]}\n'\ f'Заказчик: @{row[1]}' ) inline_buttons = InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton(text='Вернуться в меню', callback_data='menu') ], ], ) update.callback_query.message.reply_text( text='Выше перечень всех предыдущих заказов.', reply_markup=inline_buttons, ) elif choice == 'active_orders': query_customer = post_sql_query(f'SELECT * FROM ORDERS WHERE status != '\ f'"Выполнен" AND username = "******"') query_carrier = post_sql_query(f'SELECT * FROM ORDERS WHERE status != '\ f'"Выполнен" AND carrier_username = "******"') if user_role == 'role-2' or user_role == 'role-3': for row in query_customer: update.callback_query.edit_message_text( text=f'Пункт погрузки: {row[2]}\n'\ f'Пункт выгрузки: {row[3]}\n'\ f'Расстояние: {row[12]}км\n'\ f'Вес и тип: {row[4]}т {row[5]}\n'\ f'Ограничения: {row[11]}\n' f'Тариф: {row[7]} грн/т, {row[8]}\n'\ f'Дата отгрузки: {row[6]}\n'\ f'Исполнитель: @{row[9]}' ) else: for row in query_carrier: update.callback_query.edit_message_text( text=f'Пункт погрузки: {row[2]}\n'\ f'Пункт выгрузки: {row[3]}\n'\ f'Расстояние: {row[12]}км\n'\ f'Вес и тип: {row[4]}т, {row[5]}\n'\ f'Ограничения: {row[11]}\n' f'Тариф: {row[7]} грн/т {row[8]}\n'\ f'Дата отгрузки: {row[6]}\n'\ f'Заказчик: @{row[1]}' ) inline_buttons = InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton(text='Вернуться в меню', callback_data='menu') ], ], ) update.callback_query.message.reply_text( text='Выше перечень всех активных заказов.', reply_markup=inline_buttons, ) return ConversationHandler.END
def menu_handler(update: Update, context: CallbackContext): try: request = update.callback_query current_user = request.message.chat.username except AttributeError: request = update.message current_user = request.chat.username try: user_role = post_sql_query(f'SELECT role FROM USERS WHERE username ='******' "{current_user}"')[0][0] except IndexError: request.reply_text('Нажмите /start для заполнения анкеты!', ) return ConversationHandler.END if user_role == 'role-3' or user_role == 'role-2': inline_buttons = InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton(text='Создать заявку', callback_data='new_order') ], [ InlineKeyboardButton(text='Предыдущие заявки', callback_data='previous_orders') ], [ InlineKeyboardButton(text='Активные заявки', callback_data='active_orders') ], ], ) else: inline_buttons = InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton(text='Предыдущие заявки', callback_data='previous_orders') ], [ InlineKeyboardButton(text='Активные заявки', callback_data='active_orders') ], ], ) if request == update.message: request.reply_text( text=f'Вы в главном меню, чтобы вернуться сюда в любой момент '\ 'нажмите или введите /menu.\nВыберите действие которое нужно '\ 'выполнить.', reply_markup=inline_buttons, ) else: if request.data[-2:] == 're': request.message.reply_text( text=f'Вы в главном меню, чтобы вернуться сюда в любой момент '\ 'нажмите или введите /menu.\nВыберите действие которое нужно '\ 'выполнить.', reply_markup=inline_buttons, ) else: request.edit_message_text( text=f'Вы в главном меню, чтобы вернуться сюда в любой момент '\ 'нажмите или введите /menu.\nВыберите действие которое нужно '\ 'выполнить.', reply_markup=inline_buttons, ) return ConversationHandler.END