def departments_select_processor(message: Message, **kwargs): chat_id = message.chat.id user = kwargs.get('user') def error(): error_message = strings.get_string('sos.select_department', user.language) telegram_bot.send_message(chat_id, error_message) telegram_bot.register_next_step_handler_by_chat_id( chat_id, departments_select_processor, user=user) if not message.text: error() return if strings.get_string('go_back', user.language) in message.text: Navigation.to_main_menu(user, chat_id) return department_name = message.text confirm_message = strings.get_string('sos.confirm', user.language) confirm_keyboard = keyboards.get_keyboard('sos.confirm', user.language) telegram_bot.send_message(chat_id, confirm_message, reply_markup=confirm_keyboard) telegram_bot.register_next_step_handler_by_chat_id( chat_id, confirmation_processor, user=user, department_name=department_name)
def estimates_processor(message: Message, **kwargs): user = kwargs.get('user') selected_user = kwargs.get('selected_user') chat_id = message.chat.id def error(): error_message = strings.get_string('estimate.select_estimate', user.language) telegram_bot.send_message(chat_id, error_message) telegram_bot.register_next_step_handler_by_chat_id(chat_id, estimates_processor, user=user, selected_user=selected_user) if not message.text: error() return # BUG: Go back doesn't work if strings.get_string('go_back', user.language) in message.text: _to_users(user, chat_id, user.department.name) return value = strings.estimate_value_from_string(message.text, user.language) if not value: error() return comment_message = strings.get_string('estimates.comment', user.language) if value < 4: comment_templates = selected_user.department.comment_templates.all() comment_message = strings.get_string('estimates.comment_with_templates', user.language) comments_keyboard = keyboards.keyboard_from_comments_templates(comment_templates, user.language) else: comments_keyboard = keyboards.get_keyboard('go_back', user.language) new_rating = ratings.create_rating(user, selected_user, value) scheduler.add_timer_for_comment(chat_id, new_rating.id, timer_for_comment) telegram_bot.send_message(chat_id, comment_message, reply_markup=comments_keyboard, parse_mode='HTML') telegram_bot.register_next_step_handler_by_chat_id(chat_id, comments_processor, user=user, selected_user=selected_user, estimate=value, rating_id=new_rating.id)
def to_main_menu(chat_id, message_text=None): if message_text: menu_message = message_text else: menu_message = strings.get_string('main_menu.menu') main_menu_keyboard = keyboards.get_keyboard('main_menu') telegram_bot.send_message(chat_id, menu_message, reply_markup=main_menu_keyboard, parse_mode='HTML')
def to_search(chat_id): from .search import search_handler index_message = strings.get_string('search.type_query') search_keyboard = keyboards.get_keyboard('search.index') telegram_bot.send_message(chat_id, index_message, reply_markup=search_keyboard) telegram_bot.register_next_step_handler_by_chat_id( chat_id, search_handler)
def _to_estimates(user, chat_id, selected_user): estimates_message = strings.get_string( 'estimates.select_estimate', user.language).format(selected_user.name) estimates_keyboard = keyboards.get_keyboard('estimates.estimates', user.language) telegram_bot.send_message(chat_id, estimates_message, reply_markup=estimates_keyboard) telegram_bot.register_next_step_handler_by_chat_id( chat_id, estimates_processor, user=user, selected_user=selected_user)
def start_handler(message: Message): user_id = message.from_user.id chat_id = message.chat.id def not_allowed(): not_allowed_message = strings.get_string('registration.not_allowed') remove_keyboard = keyboards.get_keyboard('remove') telegram_bot.send_message(chat_id, not_allowed_message, reply_markup=remove_keyboard) current_user = users.get_user_by_telegram_id(user_id) if current_user: main_menu_keyboard = keyboards.get_main_keyboard_by_user_role( current_user) answer_text = strings.get_string( 'registration.user_exists', current_user.language).format(name=current_user.name) telegram_bot.send_message(chat_id, answer_text, reply_markup=main_menu_keyboard, parse_mode='HTML') return msg_text = message.text message_text_parts = msg_text.split(' ') try: token = message_text_parts[1] except IndexError: not_allowed() return user = users.get_user_by_token(token) if not user: not_allowed() return confirmation_result = users.confirm_user(user, user_id) if confirmation_result is True: if user.is_manager: welcome_message = strings.get_string( 'registration.welcome_manager').format(name=user.name) else: welcome_message = strings.get_string( 'registration.welcome_common').format(name=user.name) telegram_bot.send_message(chat_id, welcome_message, parse_mode='HTML') language_message = strings.get_string('registration.languages') language_keyboard = keyboards.get_keyboard('registration.languages') telegram_bot.send_message(chat_id, language_message, reply_markup=language_keyboard) telegram_bot.register_next_step_handler_by_chat_id(chat_id, language_processor, user=user) pass else: not_allowed() return
def user_file_handler(message: Message): user_telegram_file = message.audio file_size_mb = user_telegram_file.file_size / 5120**2 if file_size_mb > 1: too_much_size_message = strings.get_string('user_files.too_much_size') telegram_bot.reply_to(message, too_much_size_message) else: try: wait_message = strings.get_string('user_files.wait') telegram_bot.reply_to(message, wait_message) telegram_file_info = telegram_bot.get_file( user_telegram_file.file_id) telegram_file_path = telegram_file_info.file_path file_caption = 'audio_' + secrets.token_hex(5) telegram_file = requests.get( 'https://api.telegram.org/file/bot{0}/{1}'.format( API_TOKEN, telegram_file_path)) file_storage = FileSystemStorage() filename = 'users/' + file_caption extension = os.path.splitext( os.path.basename(telegram_file_path))[1] if os.path.exists( os.path.join(file_storage.location, filename + extension)): filename += secrets.token_hex(5) filename += extension filepath = os.path.join(file_storage.location, filename) open(filepath, 'wb').write(telegram_file.content) user = users.get_user_by_telegram_id(message.from_user.id) new_file = File.objects.create( name=file_caption, file_path=file_storage.path(filename), file_url=file_storage.url(filename), is_user_file=True, confirmed=False, caption='@send_sound_bot', user=user) type_filename_message = strings.get_string( 'user_files.type_file_name') remove_keyboard = keyboards.get_keyboard('remove') telegram_bot.send_message(message.chat.id, type_filename_message, reply_markup=remove_keyboard) telegram_bot.register_next_step_handler(message, file_name_handler, file_id=new_file.id) except Exception as e: error_message = strings.get_string('user_files.error') Navigation.to_main_menu(message.chat.id, error_message) logging.error(e)
def languages_processor(message: Message, **kwargs): user = kwargs.get('user') chat_id = message.chat.id language_ru = strings.get_string('languages.ru') language_en = strings.get_string('languages.en') language_uz = strings.get_string('languages.uz') def error(): error_message = strings.get_string('settings.select_language', user.language) telegram_bot.send_message(chat_id, error_message) telegram_bot.register_next_step_handler_by_chat_id(chat_id, languages_processor, user=user) if not message.text: error() return if strings.get_string('go_back', user.language) in message.text: Navigation.to_settings(user, chat_id) return if language_ru in message.text: language = 'ru' elif language_en in message.text: language = 'en' elif language_uz in message.text: language = 'uz' else: error() return users.set_user_language(user, language) success_message = strings.get_string('settings.languages_success', user.language) settings_keyboard = keyboards.get_keyboard('settings', user.language) telegram_bot.send_message(chat_id, success_message, reply_markup=settings_keyboard) telegram_bot.register_next_step_handler_by_chat_id(chat_id, settings_processor, user=user)
def not_allowed(): not_allowed_message = strings.get_string('registration.not_allowed') remove_keyboard = keyboards.get_keyboard('remove') telegram_bot.send_message(chat_id, not_allowed_message, reply_markup=remove_keyboard)
def to_settings(user, chat_id): from .settings import settings_processor settings_message = strings.get_string('settings.menu', user.language) settings_keyboard = keyboards.get_keyboard('settings', user.language) telegram_bot.send_message(chat_id, settings_message, reply_markup=settings_keyboard) telegram_bot.register_next_step_handler_by_chat_id(chat_id, settings_processor, user=user)