def timetable_args_error(context: CallbackContext, chat_id, error_type, language_code): """send argument error message""" cf.send_message( context=context, chat_id=chat_id, text=get_text('timetable_args_error_text', language_code).text({'error_type': error_type}), )
def report_sent(update: Update, context: CallbackContext): """take message to report and send it to all admins""" language_code = update.effective_user.language_code chat_id = update.effective_chat.id data = { 'user': mention_html(update.effective_user.id, update.effective_user.first_name), } for admin_id in database.get_all_admins_chat_ids(): cf.send_message( context=context, chat_id=admin_id, text=get_text('report_template_text', language_code).text(data), ) context.bot.forward_message( chat_id=admin_id, from_chat_id=chat_id, message_id=update.message.message_id, ) cf.send_message( context=context, chat_id=chat_id, text=get_text('report_sent_text', language_code).text(), ) return consts.MAIN_STATE
def unknown_callback(update: Update, context: CallbackContext): """handles unknown callbacks""" language_code = update.effective_user.language_code cf.send_message(context=context, chat_id=update.effective_chat.id, text=get_text('unknown_callback_text', language_code).text())
def __user_time_input_chg(update: Update, context: CallbackContext, validation, attr_name, error_state): """change mailing parameters with validation""" language_code = update.effective_user.language_code new_info = update.message.text user_id = update.effective_user.id chat_id = update.effective_chat.id # validate new_info. if False send error message and try again if validation(new_info): # update database database.set_user_attrs(user_id=user_id, attrs={attr_name: new_info}) # reset mailing job jobs.reset_mailing_job(context, user_id, chat_id, language_code) # get and send mailing page text, reply_markup = __get_mailing_page(user_id, language_code) cf.send_message( context=context, chat_id=chat_id, text=text, reply_markup=reply_markup, ) return consts.PARAMETERS_MAIN_STATE cf.send_message( context=context, chat_id=chat_id, text=get_text('format_error_parameters_text', language_code).text(), ) return error_state
def admin_notify(update: Update, context: CallbackContext): """sends provided text to specified users""" language_code = update.effective_user.language_code user_nick = context.chat_data.pop('notify_username_admin', None) disable_notification = context.chat_data.pop('n_mode', None) notification_text = update.message.text # send notification if user_nick is not None: cf.send_notification( context=context, user_nick=user_nick, text=notification_text, disable_notification=disable_notification, language_code=language_code, ) else: cf.send_notification_to_all( context=context, sender_id=update.effective_user.id, text=notification_text, disable_notification=disable_notification, language_code=language_code, ) # notify sender that everything is ok cf.send_message( context=context, chat_id=update.effective_chat.id, text=get_text('notification_sent_notify_admin_text', language_code).text() ) return consts.MAIN_STATE
def error(update: Update, context: CallbackContext): language_code = update.effective_user.language_code cf.send_message( context=context, chat_id=update.effective_chat.id, text=get_text(f'{name}_parameters_error_text', language_code).text(), )
def help_cmd(update: Update, context: CallbackContext): """help command callback""" language_code = update.effective_user.language_code cf.send_message( context=context, chat_id=update.effective_chat.id, text=get_text('help_main_text', language_code).text(), reply_markup=keyboard.help_keyboard(consts.MAIN_PAGE, language_code), )
def parameters(update: Update, context: CallbackContext): """start parameters conversation""" language_code = update.effective_user.language_code user_id = update.effective_user.id cf.send_message( context=context, chat_id=update.effective_chat.id, text=get_text('main_parameters_text', language_code).text( cf.pretty_user_parameters(database.get_user_parameters(user_id), language_code), ), reply_markup=keyboard.parameters_keyboard(language_code), ) return consts.PARAMETERS_MAIN_STATE
def timetable(update: Update, context: CallbackContext): """ sends timetable for current today otherwise sends timetable for specified day: 0 - 7 -> monday - sunday """ language_code = update.effective_user.language_code user_id = update.effective_user.id chat_id = update.effective_chat.id args = context.args week_parity = tm.get_week_parity() attendance, utcoffset = database.get_user_attrs( attrs_names=[consts.ATTENDANCE, consts.UTCOFFSET], user_id=user_id, ).values() if len(args) >= 1: # check if arg is integer try: weekday = int(args[0]) except ValueError: return timetable_args_error(context, chat_id, 'type', language_code) if weekday > 6 or weekday < 0: # wrong day index return timetable_args_error(context, chat_id, 'value', language_code) weekday = tm.weekdays[weekday] else: # current day weekday = tm.get_today_weekday(utcoffset) cf.send_message( context=context, chat_id=chat_id, text=get_weekday_timetable( weekday=weekday, subject_names=database.get_user_subject_names(user_id), attendance=attendance, week_parity=week_parity, language_code=language_code, ), reply_markup=keyboard.timetable_keyboard( weekday=weekday, attendance=attendance, week_parity=week_parity, language_code=language_code, ), )
def cancel_callback(update: Update, context: CallbackContext): """manage cancel button""" data, language_code = cf.manage_callback_query(update) parsed_data = data.split('_') if parsed_data[0] == consts.CANCEL: cf.edit_message( update=update, text=get_text('cancel_main_text', update.effective_user.language_code).text(), ) return consts.MAIN_STATE cf.send_message( context=context, chat_id=update.effective_chat.id, text=get_text('wrong_callback_text', language_code).text(), )
def admin(update: Update, context: CallbackContext): """ admin's control panel current functions: '/admin [-ls]' - list of all users '/admin [-n <--user=user_nick | --all> [--s=<0 | 1>] ]' - send a notification to the specified user or to all users. Flag --s (silence) is set to 0 by default '/admin [-m |-um <--user=user_nick | --all>]' - mute/unmute reports from user """ language_code = update.effective_user.language_code args = context.args ret_lvl = consts.MAIN_STATE reply_markup = None # unauthorized user tries to access admin panel if not database.get_user_attr('admin', user_id=update.effective_user.id): text = get_text('unauthorized_user_admin_text', language_code).text() # empty args elif len(args) == 0: text = get_text('no_args_admin_text', language_code).text() # notifications elif args[0] == '-n': text, ret_lvl, reply_markup = admin_request_notify(context, args, language_code) # list of the users elif args[0] == '-ls': text, ret_lvl, reply_markup = admin_ls(0, language_code) # mute/unmute users elif args[0] == '-m' or args[0] == '-um': text, ret_lvl = admin_mute(args, language_code) else: text = get_text('invalid_flag_admin_text', language_code).text() cf.send_message( context=context, chat_id=update.effective_chat.id, text=text, reply_markup=reply_markup, ) return ret_lvl
def report(update: Update, context: CallbackContext): """will wait for message to report if unmuted""" language_code = update.effective_user.language_code if database.get_user_attr(consts.MUTED, update.effective_user.id): text = get_text('cannot_send_report_text', language_code).text() ret_lvl = consts.MAIN_STATE reply_markup = None else: text = get_text('report_text', language_code).text() ret_lvl = consts.REPORT_MESSAGE_STATE reply_markup = keyboard.cancel_operation( consts.REPORT_STATE)(language_code) cf.send_message( context=context, chat_id=update.effective_chat.id, text=text, reply_markup=reply_markup, ) return ret_lvl
def error_callback(update: Update, context: CallbackContext): """ Error callback function notifies user that error occurred, sends feedback to all admins """ language_code = update.effective_user.language_code # notify user cf.send_message(context=context, chat_id=update.effective_chat.id, text=get_text('error_handler_user_text', language_code).text()) # make logs logger.error( f'{str(context.error)}\n\n{"".join(traceback.format_tb(sys.exc_info()[2]))}' ) # collect data about error data = {'error': str(context.error)} if update.effective_user: data['user'] = mention_html(update.effective_user.id, update.effective_user.first_name) else: data['user'] = '******' if update.callback_query: data[ 'info'] = f'type = query\ncontent = "{update.callback_query.data}"' else: data['info'] = f'type = message\ncontent = "{update.message.text}"' text = get_text('error_handler_dev_text', language_code) # send collected data to all admins for dev_id in database.get_all_admins_chat_ids(): cf.send_message( context=context, chat_id=dev_id, text=text.text(data), )
def inner(update: Update, context: CallbackContext): language_code = update.effective_user.language_code user_id = update.effective_user.id # get subject info, page is main by default main_info, attendance = get_subject_info( subject=subject, user_id=user_id, page=consts.MAIN_PAGE, language_code=language_code, ) cf.send_message( context=context, chat_id=update.effective_chat.id, text=main_info, reply_markup=keyboard.subject_keyboard( subject=subject, attendance=attendance, page=consts.MAIN_PAGE, language_code=language_code, ), )
def start(update: Update, context: CallbackContext): """ adds user into the database, if he was not there sets job if new user sends greeting message """ language_code = update.effective_user.language_code chat_id = update.effective_chat.id user_id = update.effective_user.id new_user = database.add_user(user_id, update.effective_user.username, chat_id, language_code) if new_user: jobs.reset_mailing_job(context, user_id, chat_id, language_code) cf.send_message( context=context, chat_id=chat_id, text=get_text('start_text', language_code=language_code).text(), ) return consts.MAIN_STATE
def send_weekday_timetable(context: CallbackContext, user_id, chat_id, weekday, language_code, footer=None): """Sends timetable for specified day""" # get user parameters attendance, utcoffset, notification_status = database.get_user_attrs( attrs_names=[consts.ATTENDANCE, consts.UTCOFFSET, consts.NOTIFICATION_STATUS], user_id=user_id, ).values() disable_notification = notification_status == consts.NOTIFICATION_DISABLED if weekday == consts.TODAY: # get current day weekday = tm.get_today_weekday(utcoffset) week_parity = tm.get_week_parity() tt = get_weekday_timetable( weekday=weekday, subject_names=database.get_user_subject_names(user_id), attendance=attendance, week_parity=week_parity, language_code=language_code, footer=footer, ) cf.send_message( context=context, chat_id=chat_id, text=tt, reply_markup=keyboard.timetable_keyboard( weekday=weekday, attendance=attendance, week_parity=week_parity, language_code=language_code ), disable_notification=disable_notification, )
def doc(update: Update, context: CallbackContext): """ show documentation if argument specified, shows docs for command shows special docs for admins """ language_code = update.effective_user.language_code args = context.args if_admin = database.get_user_attr('admin', user_id=update.effective_user.id) if len(args) > 2: text = get_text('quantity_error_doc_text', language_code).text() else: if len(args) == 0: text = get_text('doc_text', language_code).text({ 'command': consts.ALL, 'admin': if_admin }) else: if args[0] not in consts.DOC_COMMANDS: text = get_text('wrong_command_error_doc_text', language_code).text() else: text = get_text('doc_text', language_code).text({ 'command': args[0], 'admin': if_admin }) if not if_admin and args[0] == 'admin': text += get_text('doc_unavailable_text', language_code).text() cf.send_message( context=context, chat_id=update.effective_chat.id, text=text, )
def deadline(update: Update, context: CallbackContext): chat_id = update.effective_chat.id text, reply_markup = make_deadline(update, consts.TODAY) cf.send_message(context, chat_id, text, reply_markup)