def update_handler(bot, update, motlin_token, params): query = update.callback_query chat_id = query.message.chat_id if 'DELIVEREDYES' in query.data: customer_chat_id = query.data.replace('DELIVEREDYES', '') motlin_lib.confirm_order_shipping( motlin_token, params['redis_conn'].get_value(customer_chat_id, 'order')) delete_messages(bot, chat_id, query.message.message_id) motlin_lib.delete_the_cart(motlin_token, customer_chat_id) clear_settings_and_task_queue(chat_id, params) clear_settings_and_task_queue(customer_chat_id, params) return 'UPDATE_HANDLER' elif 'DELIVEREDTO' in query.data: confirm_deliviry(bot, chat_id, query.data.replace('DELIVEREDTO', ''), query.message.message_id) else: send_courier_message(bot, [ query.data, chat_id, motlin_token, motlin_lib.get_address(motlin_token, 'customeraddress', 'customerid', query.data), params['redis_conn'].get_value(query.data, 'delivery_price'), bool(params['redis_conn'].get_value( query.data, 'cash_payment')), params['redis_conn'], get_delivery_time(params['redis_conn'], query.data, CLIENT_REMINDER_PERIOD) ]) delete_messages(bot, chat_id, query.message.message_id) return 'UPDATE_HANDLER'
def waiting_email(bot, update, motlin_token, redis_conn): chat_id = update.message.chat_id if update.message.text and validate_email(update.message.text): save_customer_email(bot, str(update.message.chat_id), motlin_token, update.message.text) if not motlin_lib.get_customer(motlin_token, 'email', update.message.text): motlin_lib.add_new_customer(motlin_token, update.message.text) show_customers_menu(bot, chat_id, motlin_token) delete_messages(bot, chat_id, update.message.message_id, 2) return 'HANDLE_CUSTOMERS' else: bot.send_message( chat_id=update.message.chat_id, text='Вы ввели не корректный email. Поробуйте еще раз:') return 'WAITING_EMAIL'
def handle_cart(bot, update, motlin_token, params): query = update.callback_query chat_id = query.message.chat_id if query.data == 'HANDLE_MENU': current_page = params['redis_conn'].get_value(chat_id, 'current_page') show_store_menu(bot, chat_id, motlin_token, query.message.message_id, current_page) return query.data elif query.data == str(chat_id): show_customers_menu(bot, chat_id, motlin_token) delete_messages(bot, chat_id, query.message.message_id) return 'HANDLE_CUSTOMERS' else: motlin_lib.delete_from_cart(motlin_token, chat_id, query.data) show_products_in_cart(bot, chat_id, motlin_token, query.message.message_id) return 'HANDLE_CART'
def waiting_phone(bot, update, motlin_token, params): chat_id = update.message.chat_id if update.message.text and phonenumbers.is_valid_number( phonenumbers.parse(update.message.text, 'RU')): save_customer_phone(bot, str(update.message.chat_id), motlin_token, update.message.text) show_customers_menu(bot, chat_id, motlin_token) delete_messages(bot, chat_id, update.message.message_id, 2) return 'HANDLE_CUSTOMERS' else: bot.send_message( chat_id=update.message.chat_id, text='Вы ввели не корректный номер телефона. Поробуйте еще раз:') delete_messages(bot, update.message.chat_id, update.message.message_id, message_numbers=2) return 'WAITING_PHONE'
def handle_waiting(bot, update, motlin_token, params): query = update.callback_query longitude, latitude = None, None if query and query.data == 'HANDLE_MENU': chat_id = query.message.chat_id order_id = confirm_order(bot, chat_id, query.message.message_id) params['redis_conn'].add_value(chat_id, 'order', order_id) return query.data elif query and query.data == 'HANDLE_WAITING': bot.send_message(chat_id=query.message.chat_id, text='Пришлите, пожалуйста, Ваш адрес или геолокацию') return query.data elif update.message.text: longitude, latitude = geo_lib.fetch_coordinates( params['ya_api_key'], update.message.text) customer_address = update.message.text delete_messages(bot, update.message.chat_id, update.message.message_id - 1) elif update.message.location: longitude, latitude = update.message.location.longitude, update.message.location.latitude customer_address = geo_lib.fetch_address(params['ya_api_key'], longitude, latitude) delete_messages(bot, update.message.chat_id, update.message.message_id - 1) if not longitude == latitude is None: chat_id = update.message.chat_id nearest_address = find_nearest_address(motlin_token, longitude, latitude) params['redis_conn'].add_value(chat_id, 'nearest_pizzeria', nearest_address['address']) choose_deliviry(bot, chat_id, motlin_token, nearest_address) save_customer_address( bot, str(chat_id), motlin_token, customer_address, longitude, latitude, geo_lib.fetch_address_decryption(params['ya_api_key'], longitude, latitude)) return 'HANDLE_DELIVERY' else: bot.send_message( chat_id=update.message.chat_id, text= 'Вы ввели не корректный адрес или геопозицию. Поробуйте еще раз:') return 'HANDLE_WAITING'
def handle_payment(bot, update, motlin_token, params): if update.message and update.message.successful_payment: chat_id = update.message.chat_id order_id = confirm_order(bot, chat_id, motlin_token) params['redis_conn'].add_value(chat_id, 'order', order_id) delivery_type = params['redis_conn'].get_value(chat_id, 'delivery_type') if delivery_type == 'PICKUP_DELIVERY': motlin_lib.confirm_order_shipping(motlin_token, order_id) motlin_lib.delete_the_cart(motlin_token, chat_id) clear_settings_and_task_queue(chat_id, params) else: handle_delivery(bot, update, motlin_token, params) return 'UPDATE_HANDLER' elif update.callback_query and update.callback_query.data == 'CASH_PAYMENT': chat_id = update.callback_query.message.chat_id order_id = confirm_order(bot, chat_id, motlin_token, True, update.callback_query.message.message_id) params['redis_conn'].add_value(chat_id, 'order', order_id) params['redis_conn'].add_value(chat_id, 'cash_payment', 1) handle_delivery(bot, update, motlin_token, params) delivery_type = params['redis_conn'].get_value(chat_id, 'delivery_type') if delivery_type == 'PICKUP_DELIVERY': motlin_lib.confirm_order_shipping(motlin_token, order_id) motlin_lib.delete_the_cart(motlin_token, chat_id) clear_settings_and_task_queue(chat_id, params) return 'UPDATE_HANDLER' elif update.callback_query and update.callback_query.data == 'CARD_PAYMENT': chat_id = update.callback_query.message.chat_id params['redis_conn'].add_value(chat_id, 'cash_payment', 0) description, currency, price = motlin_lib.get_payment_info( motlin_token, chat_id) bot.send_invoice(chat_id, 'Оплата заказа', description, 'Tranzzo payment', params['payment_token'], 'payment', currency, [LabeledPrice('Заказ', price * 100)]) delete_messages(bot, chat_id, update.callback_query.message.message_id) return 'PAYMENT_WAITING' else: return 'HANDLE_PAYMENT'
def handle_customers(bot, update, motlin_token, redis_conn): query = update.callback_query chat_id = query.message.chat_id if query.data == 'CUSTOMERS_MAIL': bot.send_message(chat_id=chat_id, text='Ваш адрес электронной почты:') delete_messages(bot, chat_id, query.message.message_id) return 'WAITING_EMAIL' elif query.data == 'CUSTOMERS_PHONE': bot.send_message(chat_id=chat_id, text='Ваш контактный телефон:') delete_messages(bot, chat_id, query.message.message_id) return 'WAITING_PHONE' else: bot.send_message(chat_id=chat_id, text='Пришлите, пожалуйста, Ваш адрес или геолокацию') delete_messages(bot, chat_id, query.message.message_id) return 'HANDLE_WAITING'
def handle_delivery(bot, update, motlin_token, params, customer_chat_id=''): if customer_chat_id: query, chat_id, message_id = None, int(customer_chat_id), None elif update.callback_query: query, chat_id = update.callback_query, update.callback_query.message.chat_id message_id = query.message.message_id elif update.message: query, chat_id, message_id = None, update.message.chat_id, 0 pizzeria_address = motlin_lib.get_address( motlin_token, 'pizzeria', 'address', params['redis_conn'].get_value(chat_id, 'nearest_pizzeria')) delivery_type = params['redis_conn'].get_value(chat_id, 'delivery_type') delivery_price = params['redis_conn'].get_value(chat_id, 'delivery_price') pay_by_cash = bool(params['redis_conn'].get_value(chat_id, 'cash_payment')) if query and 'COURIER_DELIVERY' in query.data: delivery_price = query.data.replace('COURIER_DELIVERY', '') params['redis_conn'].add_value(chat_id, 'delivery_type', 'COURIER_DELIVERY') params['redis_conn'].add_value( chat_id, 'delivery_price', int(delivery_price if delivery_price else 0)) params['job'].run_once(show_reminder, CLIENT_REMINDER_PERIOD, context=chat_id) delete_messages(bot, chat_id, message_id, message_numbers=2) elif query and pizzeria_address and query.data == 'PICKUP_DELIVERY': params['redis_conn'].add_value(chat_id, 'delivery_type', 'PICKUP_DELIVERY') bot.send_location(chat_id=chat_id, latitude=pizzeria_address['latitude'], longitude=pizzeria_address['longitude']) bot.send_message( chat_id=chat_id, text= f'Вы сможете забрать пиццу по адресу: {pizzeria_address["address"]}' ) delete_messages(bot, chat_id, message_id, message_numbers=2) elif pizzeria_address and delivery_type == 'COURIER_DELIVERY': courier_id = pizzeria_address['telegramid'] params['job'].run_repeating( send_or_update_courier_messages, COURIER_REMINDER_PERIOD, first=0, context={ 'chat_id': chat_id, 'courier_id': courier_id, 'motlin_token': motlin_token, 'customer_address': motlin_lib.get_address(motlin_token, 'customeraddress', 'customerid', str(chat_id)), 'delivery_price': delivery_price, 'cash': pay_by_cash, 'redis_conn': params['redis_conn'], 'delivery_time': get_delivery_time(params['redis_conn'], chat_id, CLIENT_REMINDER_PERIOD) }) params['redis_conn'].add_value(courier_id, 'state', 'UPDATE_HANDLER') return 'UPDATE_HANDLER' else: return 'HANDLE_DELIVERY' choose_payment_type(bot, chat_id) return 'HANDLE_PAYMENT'