def inline_button_handler(bot: Bot, update: Update): callback_data = update.callback_query.data if callback_data == 'ru_': return functions.set_ru(bot, update) if callback_data == 'uz_': return functions.set_uz(bot, update) if callback_data == 'first_name': return functions.get_first_name(bot, update) if callback_data == 'last_name': return functions.get_last_name(bot, update) if callback_data == 'middle_name': return functions.get_middle_name(bot, update) if callback_data == 'back_to_main': return functions.main_menu(bot, update, edit=True) if callback_data == 'back_to_request': return functions.request_menu(bot, update, edit=True) if callback_data == 'region': return functions.get_region(bot, update) if callback_data == 'address': return functions.get_address(bot, update) if callback_data == 'organization': return functions.get_org(bot, update) if callback_data == 'application_type': return functions.get_application_type(bot, update) if callback_data == 'application_text': return functions.get_application_text(bot, update) if callback_data == 'correct': return functions.save_user(bot, update) if callback_data == 'back_to_application_menu': return functions.user_application_menu(bot, update) if callback_data == 'save_application': return functions.save_application(bot, update) if callback_data == 'cancel': return functions.cancel_application(bot, update) menu = { 'start_request': functions.request_menu, 'uz': functions.set_uz, 'ru': functions.set_ru, 'correct': functions.save_user, 'cancel': functions.cancel, 'set_first_name': functions.get_first_name, 'set_last_name': functions.get_last_name, 'set_middle_name': functions.get_middle_name, 'set_region': functions.get_region, 'set_district': functions.get_district, 'set_gathering': functions.get_gathering, 'set_address': functions.get_address, 'set_org': functions.get_org, 'set_application_type': functions.get_application_type, 'set_application_text': functions.get_application_text, } state = 0 for item in menu: if callback_data.find(item) != -1: data = callback_data.split(item)[1] state = menu[item](bot, update, data) return state
def set_verification_code(bot: Bot, update: Update): user_model = apps.get_model('main', 'TelegramProfile') user = user_model.objects.get(external_id=update.effective_chat.id) code = update.message.text err_text = 'Вы ввели неверный код подтверждения' if user.lang == 'uz' \ else 'Siz noto\'gri kod kiritingiz' text = 'Вы успешно авторизовались' if user.lang == 'ru' \ else 'Siz muafoqiyatli sistemaga keidingiz' if VerifyCode.objects.filter( code=code, expire_date__gte=datetime.datetime.now(), telegram_id=update.effective_chat.id).exists(): obj = VerifyCode.objects.get(code=code, telegram_id=update.effective_chat.id) bot.send_message(chat_id=update.effective_chat.id, text=text) user.confirmed = True request = cache.get(f'request_{update.effective_chat.id}') phone_number = obj.phone_number request['phone_number'] = phone_number request['telegram_id'] = update.effective_chat.id cache.set(f'request_{update.effective_chat.id}', request) user.save() return functions.main_menu(bot, update, edit=False) else: bot.send_message(chat_id=update.effective_chat.id, text=err_text) return states.SET_CODE
def start(bot: Bot, update: Update): user_model = apps.get_model('main', 'TelegramProfile') try: user = user_model.objects.get(external_id=update.effective_chat.id) except Exception: user = user_model.create_user( external_id=update.effective_chat.id, username=update.message.from_user.username, ) if user.lang: return functions.main_menu(bot, update, edit=False) else: keyboard = [ [ InlineKeyboardButton("🇺🇿 O'zbek tili", callback_data='uz_'), InlineKeyboardButton('🇷🇺 Русский язык', callback_data='ru_'), ] ] bot.send_message( chat_id=update.effective_chat.id, text='*Tilni tanlang* / *Выберите язык*', parse_mode='Markdown', reply_markup=InlineKeyboardMarkup(keyboard) ) return states.GET_LANG
def all_applications(bot: Bot, update: Update, data: str = None, edit: bool = False) -> int: if cache.get(f'return_{update.effective_chat.id}'): return functions.request_menu(bot, update) user_model = apps.get_model('main', 'TelegramProfile') user = user_model.objects.get(external_id=update.effective_chat.id) state_data = text_manager.objects.filter( text_id='ALL_APPLICATIONS').values()[0] cancel_btn = '❌' back_btn = '◀️' applications = UserApplication.objects.filter( user_profile__telegram_id__external_id=update.effective_chat.id, status__lte=3) applications_list = UserApplication.objects.filter( organization__in=applications.values_list('organization', flat=True), status__lte=3).order_by('created_at') if len(applications) > 0: for application in applications: for index, item in enumerate(applications_list): if application == item: application.queue = int(index) + 1 application.save() keyboard = [[ InlineKeyboardButton( cancel_btn, callback_data='cancel/{application}'.format( application=application.id)), InlineKeyboardButton(back_btn, callback_data='back_to_main') ]] status = application.status_name org = application.organization date = application.get_date queue = application.queue text = state_data[user.lang].format(organization=org, status=status, date=date, queue=queue) if edit: bot.edit_message_text( chat_id=update.effective_chat.id, text=text, parse_mode='Markdown', message_id=update.callback_query.message.message_id, reply_markup=InlineKeyboardMarkup(keyboard, resize_keyboard=True)) else: bot.send_message(chat_id=update.effective_chat.id, text=text, reply_markup=InlineKeyboardMarkup( keyboard, resize_keyboard=True), parse_mode='Markdown') return states.ALL_APPLICATIONS else: text = 'Sizda murojaatlar yo\'q' if user.lang == 'uz' else 'У вас еще нет заявок' bot.send_message(chat_id=update.effective_chat.id, text=text) return functions.main_menu(bot, update, edit=False)
def save_user(bot: Bot, update: Update, data: str = None): user_model = apps.get_model('main', 'TelegramProfile') user = user_model.objects.get(external_id=update.effective_chat.id) request = cache.get(f'request_{update.effective_chat.id}') if user.lang == 'uz': text = 'Sizning profilingiz yangilandi' else: text = 'Ваш профиль обновлен' bot.answer_callback_query(callback_query_id=update.callback_query.id, text=text) save_user_to_db(request) return functions.main_menu(bot, update, edit=True)
def set_ru(bot: Bot, update: Update, data: str = None): user_model = apps.get_model('main', 'TelegramProfile') user = user_model.objects.get(external_id=update.effective_chat.id) if user.lang == 'ru': bot.answer_callback_query( callback_query_id=update.callback_query.inline_message_id, text="Выбран русский язык.") return states.INLINE user.lang = 'ru' user.save() bot.delete_message(chat_id=update.effective_chat.id, message_id=update.callback_query.message.message_id) return functions.main_menu(bot, update, edit=False)
def save_application(bot: Bot, update: Update, data: str = None): user = TelegramProfile.objects.get(external_id=update.effective_chat.id) application_request = cache.get(f'application_{update.effective_chat.id}') user_request = cache.get(f'request_{update.effective_chat.id}') err_text = 'O\'zingiz xaqingizda ma\'lumot qoldiring' if user.lang == 'uz' else 'Вы не заполнили все поля' if not validate_user_info(user_request): bot.delete_message(chat_id=update.effective_chat.id, message_id=update.callback_query.message.message_id) bot.send_message(chat_id=update.effective_chat.id, text=err_text) return functions.request_menu(bot, update, edit=False) if user.lang == 'uz': text = 'Arizangiz qabul qilindi' else: text = 'Ваша заявка принята' bot.answer_callback_query(callback_query_id=update.callback_query.id, text=text) save_application_to_db(application_request) cache.delete(f'application_{update.effective_chat.id}') return functions.main_menu(bot, update, edit=True)
def cancel_application(bot: Bot, update: Update): user_model = apps.get_model('main', 'TelegramProfile') user = user_model.objects.get(external_id=update.effective_chat.id) text = '"{organization}"ga qilgan arizangizni bekor qilmoqchimisiz?' if user.lang == 'uz' \ else 'Вы уверены, что хотите отменить заявку для "{organization}"' callback_data = update.callback_query.data application = callback_data.split('/') if callback_data == 'back_to_main': return functions.main_menu(bot, update, True) try: user_application = UserApplication.objects.get(id=application[1]) except Exception as e: print(e) try: user_profile = UserProfile.objects.get( telegram_id__external_id=update.effective_chat.id) except Exception as err: print(err) pass try: CancellationMessage.objects.get( application=user_application, user_profile=user_profile, ) except Exception as e: CancellationMessage.objects.create( application=user_application, user_profile=user_profile, ) keyboard = [[ InlineKeyboardButton(text='🆗', callback_data='YES'), InlineKeyboardButton(text='⬅️', callback_data='NO') ]] bot.edit_message_text( chat_id=update.effective_chat.id, message_id=update.callback_query.message.message_id, text=text.format(organization=user_application.organization.title), reply_markup=InlineKeyboardMarkup(keyboard, resize_keyboard=True)) return states.CANCEL_APPLICATION