Exemple #1
0
def comments_processor(message: Message, **kwargs):
    user = kwargs.get('user')
    selected_user = kwargs.get('selected_user')
    estimate_value = kwargs.get('estimate')
    rating_id = kwargs.get('rating_id')
    chat_id = message.chat.id

    def error():
        error_message = strings.get_string('estimates.comment', user.language)
        telegram_bot.send_message(chat_id, error_message)
        telegram_bot.register_next_step_handler_by_chat_id(
            chat_id,
            comments_processor,
            user=user,
            selected_user=selected_user,
            estimate=estimate_value)

    if not message.text:
        error()
        return
    if strings.get_string('go_back', user.language) in message.text:
        _to_estimates(user, chat_id, selected_user)
        return
    comment = message.text
    rating = ratings.set_comment_for_rating(rating_id, comment)
    scheduler.remove_timer_for_comment(rating.id)
    success_message = strings.get_string(
        'estimates.success',
        user.language).format(name=selected_user.name,
                              department=selected_user.department.name)
    Navigation.to_main_menu(user, chat_id, message_text=success_message)
    if estimate_value < 4:
        Helpers.send_bad_rating_to_managers(rating, user, selected_user)
Exemple #2
0
def favorite_query_handler(query: CallbackQuery):
    user_id = query.from_user.id
    user = users.get_user_by_telegram_id(user_id)

    def error(string_code):
        telegram_bot.answer_callback_query(query.id,
                                           strings.get_string(string_code),
                                           show_alert=True)

    if not user:
        error('catalog.add_favorite.not_allowed')
        return

    if not query.data.isnumeric():
        error('catalog.add_favorite.error')
        return
    file_id = int(query.data)
    file = files.get_file_by_id(file_id)
    if not file:
        error('catalog.add_favorite.error')
        return
    if file in user.favorites_files.all():
        user.favorites_files.remove(file)
        success_message = strings.get_string('catalog.add_favorite.removed')
        new_keyboard = keyboards.from_file_to_inline_keyboard_favorite(file)
    else:
        user.favorites_files.add(file)
        success_message = strings.get_string('catalog.add_favorite.added')
        new_keyboard = keyboards.from_file_to_inline_keyboard_favorite(
            file, remove=True)
    telegram_bot.answer_callback_query(query.id, success_message)
    telegram_bot.edit_message_reply_markup(user_id, query.message.message_id,
                                           query.inline_message_id,
                                           new_keyboard)
Exemple #3
0
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)
Exemple #4
0
def settings_processor(message: Message, **kwargs):
    user = kwargs.get('user')
    chat_id = message.chat.id

    def error():
        error_message = strings.get_string('settings.menu', user.language)
        telegram_bot.send_message(chat_id, error_message)
        telegram_bot.register_next_step_handler_by_chat_id(chat_id,
                                                           settings_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)
    elif strings.get_string('settings.languages',
                            user.language) in message.text:
        languages_message = strings.get_string('settings.select_language',
                                               user.language)
        languages_keyboard = keyboards.keyboard_by_user_language(user)
        telegram_bot.send_message(chat_id,
                                  languages_message,
                                  reply_markup=languages_keyboard)
        telegram_bot.register_next_step_handler_by_chat_id(chat_id,
                                                           languages_processor,
                                                           user=user)
    else:
        error()
        return
Exemple #5
0
def confirmation_processor(message: Message, **kwargs):
    chat_id = message.chat.id
    user = kwargs.get('user')
    department_name = kwargs.get('department_name')

    def error():
        error_message = strings.get_string('sos.confirm', user.language)
        telegram_bot.send_message(chat_id, error_message)
        telegram_bot.register_next_step_handler_by_chat_id(
            chat_id,
            confirmation_processor,
            user=user,
            department_name=department_name)

    if message.text:
        if strings.get_string('go_back', user.language) in message.text:
            _to_select_department(user, chat_id)
            return
        else:
            error()
            return
    elif message.location:
        latitude = message.location.latitude
        longitude = message.location.longitude
        sos_signal = sos.create_sos(user, latitude, longitude, department_name)
        success_message = strings.get_string(
            'sos.success', user.language).format(department_name)
        Navigation.to_main_menu(user, chat_id, success_message)
        Helpers.send_sos_signal_to_managers(sos_signal, user)
    else:
        error()
        return
Exemple #6
0
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)
Exemple #7
0
def company_rating_processor(message: Message, **kwargs):
    user = kwargs.get('user')
    chat_id = message.chat.id

    def error():
        error_message = strings.get_string('ratings.select_company', language)
        telegram_bot.send_message(chat_id, error_message)
        telegram_bot.register_next_step_handler_by_chat_id(chat_id, 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
    company_name = message.text
    company = companies.get_company_by_name(company_name)
    if not company:
        error()
        return
    wait_message = strings.get_string('ratings.please_wait', user.language)
    telegram_bot.send_message(chat_id, wait_message)
    ratings_img = ratings.get_img_ratings_for_company(company)
    telegram_bot.send_chat_action(chat_id, 'upload_photo')
    telegram_bot.send_photo(chat_id, open(ratings_img, 'rb'))
    telegram_bot.register_next_step_handler_by_chat_id(
        chat_id, company_rating_processor, user=user)
Exemple #8
0
def language_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_msg = strings.get_string('registration.languages')
        telegram_bot.send_message(chat_id, error_msg)
        telegram_bot.register_next_step_handler_by_chat_id(chat_id,
                                                           language_processor,
                                                           user=user)

    if not message.text:
        error()
        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)
    main_menu_keyboard = keyboards.get_main_keyboard_by_user_role(user)
    welcome_text = strings.get_string('registration.welcome', user.language)
    telegram_bot.send_message(chat_id,
                              welcome_text,
                              reply_markup=main_menu_keyboard,
                              parse_mode='HTML')
Exemple #9
0
def search_handler(message: Message):
    user_id = message.from_user.id

    def error():
        error_message = strings.get_string('search.type_query')
        telegram_bot.send_message(user_id, error_message)
        telegram_bot.register_next_step_handler_by_chat_id(
            user_id, search_handler)

    def not_found():
        not_found_message = strings.get_string('search.not_found')
        telegram_bot.send_message(user_id, not_found_message)
        telegram_bot.register_next_step_handler_by_chat_id(
            user_id, search_handler)

    if not message.text:
        error()
        return
    if strings.get_string('back') in message.text:
        Navigation.to_main_menu(user_id)
        return
    found_files = files.search_files(message.text)
    if not found_files:
        not_found()
        return
    files_keyboard = keyboards.from_files_list_to_keyboard(found_files)
    select_message = strings.get_string('catalog.files.select')
    telegram_bot.send_message(user_id,
                              select_message,
                              reply_markup=files_keyboard)
    telegram_bot.register_next_step_handler_by_chat_id(user_id, file_handler)
Exemple #10
0
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
Exemple #11
0
 def to_catalog(chat_id):
     from bot.catalog import category_handler
     root_categories = files.get_parent_categories()
     if not root_categories:
         empty_message = strings.get_string('catalog.categories.empty')
         telegram_bot.send_message(chat_id, empty_message)
         return
     select_message = strings.get_string('catalog.categories.select')
     categories_keyboard = keyboards.from_categories_list_to_keyboard(root_categories, include_from_users=True)
     telegram_bot.send_message(chat_id, select_message, reply_markup=categories_keyboard)
     telegram_bot.register_next_step_handler_by_chat_id(chat_id, category_handler, current_category=None,
                                                        keyboard=categories_keyboard)
Exemple #12
0
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)
Exemple #13
0
def favorites_handler(message: Message):
    user_id = message.from_user.id
    user = users.get_user_by_telegram_id(user_id)

    user_files = user.favorites_files.all()
    if not user_files:
        empty_message = strings.get_string('favorites.empty')
        telegram_bot.send_message(user_id, empty_message)
        return
    select_message = strings.get_string('catalog.files.select')
    files_keyboard = keyboards.from_files_list_to_keyboard(user_files)
    telegram_bot.send_message(user_id,
                              select_message,
                              reply_markup=files_keyboard)
    telegram_bot.register_next_step_handler_by_chat_id(user_id, file_handler)
Exemple #14
0
def file_handler(message: Message, *args, **kwargs):
    user_id = message.from_user.id
    current_category = kwargs.get('current_category')
    user = users.get_user_by_telegram_id(user_id)

    def error():
        telegram_bot.send_message(user_id,
                                  strings.get_string('catalog.files.select'))
        telegram_bot.register_next_step_handler_by_chat_id(
            user_id, file_handler, current_category=current_category)

    if not message.text:
        error()
        return
    if strings.get_string('back') in message.text:
        if 'from_users' in kwargs:
            _go_back(user_id, current_category, from_users=True)
        else:
            _go_back(user_id, current_category)
    else:
        file = files.get_file_by_name(message.text)
        if not file:
            error()
            return
        Helpers.send_file(user_id, file, user)
        telegram_bot.register_next_step_handler_by_chat_id(
            user_id, file_handler, current_category=current_category)
Exemple #15
0
 def to_main_menu(user, chat_id, message_text=None):
     if message_text:
         menu_message = message_text
     else:
         menu_message = strings.get_string('menu.common', user.language)
     menu_keyboard = keyboards.get_main_keyboard_by_user_role(user)
     telegram_bot.send_message(chat_id, menu_message, reply_markup=menu_keyboard, parse_mode='HTML')
Exemple #16
0
 def settings(message: Message):
     if not message.text:
         return False
     user = Access._auth(message)
     if not user:
         return False
     return Access._private(message) and strings.get_string('menu.settings', user.language) in message.text
Exemple #17
0
 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')
Exemple #18
0
def file_handler(message: Message):
    user_id = message.from_user.id
    user = users.get_user_by_telegram_id(user_id)

    def error():
        telegram_bot.send_message(user_id,
                                  strings.get_string('catalog.files.select'))
        telegram_bot.register_next_step_handler_by_chat_id(
            user_id, file_handler)

    if not message.text:
        error()
        return
    if Helpers.check_for_start_command(message):
        Navigation.to_main_menu(user_id)
        return
    if strings.get_string('back') in message.text:
        Navigation.to_search(user_id)
    else:
        file = files.get_file_by_name(message.text)
        if not file:
            error()
            return
        Helpers.send_file(user_id, file, user)
        telegram_bot.register_next_step_handler_by_chat_id(
            user_id, file_handler)
Exemple #19
0
 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)
Exemple #20
0
 def error():
     error_message = strings.get_string('sos.confirm', user.language)
     telegram_bot.send_message(chat_id, error_message)
     telegram_bot.register_next_step_handler_by_chat_id(
         chat_id,
         confirmation_processor,
         user=user,
         department_name=department_name)
Exemple #21
0
 def estimates(message: Message):
     if not message.text:
         return False
     user = Access._auth(message)
     if not user:
         return False
     if user.is_manager:
         return False
     return Access._private(message) and strings.get_string('menu.put_estimate', user.language) in message.text
Exemple #22
0
 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)
Exemple #23
0
 def error():
     error_message = strings.get_string('estimates.comment', user.language)
     telegram_bot.send_message(chat_id, error_message)
     telegram_bot.register_next_step_handler_by_chat_id(
         chat_id,
         comments_processor,
         user=user,
         selected_user=selected_user,
         estimate=estimate_value)
Exemple #24
0
def notify_users_about_estimates():
    confirmed_users = users.get_confirmed_users()
    for user in confirmed_users:
        notify_message = strings.get_string('notifications.message',
                                            user.language)
        try:
            telegram_bot.send_message(user.telegram_user_id, notify_message)
        except ApiException:
            pass
        time.sleep(0.1)
Exemple #25
0
def _to_users(user, chat_id, department_name, error_callback=None):
    department_users = users.find_users_by_department_name(department_name, user.id, user.department.company_id)
    if not department_users:
        if error_callback:
            error_callback()
        return
    users_message = strings.get_string('estimates.select_user', user.language)
    users_keyboard = keyboards.keyboard_from_users_list(department_users, user.language)
    telegram_bot.send_message(chat_id, users_message, reply_markup=users_keyboard)
    telegram_bot.register_next_step_handler_by_chat_id(chat_id, users_processor, user=user)
Exemple #26
0
def estimates_handler(message: Message):
    chat_id = message.chat.id
    user_id = message.from_user.id
    user = users.get_user_by_telegram_id(user_id)
    if not user.dispatcher:
        empty_message = strings.get_string('estimates.no_dispatcher',
                                           user.language)
        Navigation.to_main_menu(user, chat_id, empty_message)
        return
    _to_estimates(user, chat_id, user.dispatcher)
Exemple #27
0
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)
Exemple #28
0
def _to_companies_select(user, chat_id):
    companies_message = strings.get_string('ratings.select_company',
                                           user.language)
    all_companies = companies.get_all_companies()
    companies_keyboard = keyboards.keyboard_from_companies_list(
        all_companies, user.language)
    telegram_bot.send_message(chat_id,
                              companies_message,
                              reply_markup=companies_keyboard)
    telegram_bot.register_next_step_handler_by_chat_id(
        chat_id, company_rating_processor, user=user)
Exemple #29
0
 def sos(message: Message):
     if not message.text:
         return False
     user = Access._auth(message)
     if not user:
         return False
     if user.is_manager:
         return False
     if user.department.code_name != Department.DefaultNames.DRIVERS:
         return False
     return Access._private(message) and strings.get_string('menu.sos', user.language) in message.text
Exemple #30
0
def _to_select_department(user, chat_id):
    departments_message = strings.get_string('sos.select_department',
                                             user.language)
    departments = companies.get_all_departments()
    departments_keyboard = keyboards.keyboard_from_departments_list(
        departments, user.language)
    telegram_bot.send_message(chat_id,
                              departments_message,
                              reply_markup=departments_keyboard)
    telegram_bot.register_next_step_handler_by_chat_id(
        chat_id, departments_select_processor, user=user)