async def enroll_event(obj: types.base.TelegramObject): callback = False callback_query = None if isinstance(obj, types.CallbackQuery): callback = True callback_query = obj message = obj.message uid = callback_query.from_user.id elif isinstance(obj, types.Message): message = obj uid = message.from_user.id else: return state = dp.current_state(user=uid) with session_scope() as session: if callback: event_id = await user_notify_context.get(user=uid, key=message.message_id) else: event_q = session.query(Event) \ .filter(Event.title == message.text) if event_q.count() == 0: return event: Event = event_q.all()[0] event_id = event.id await state.set_data({'event_id': event_id}) user_q = session.query(User) \ .filter(User.uid == uid) user: User = user_q.all()[0] enrolled_q = session.query(Enrollment) \ .join(User) \ .filter(User.uid == uid) \ .filter(Enrollment.event_id == event_id) if enrolled_q.count() == 0: # create enrollment enrollment = Enrollment(user_id=user.id, event_id=event_id, complete=False) \ .insert_me(session) logging.info(f'enrollment created: {enrollment}') else: enrollment = enrolled_q.all()[0] # build message if enrollment.complete: m_text = MESSAGES['registration_exists'] remove_keyboard = None else: m_text = MESSAGES['invoice_prompt'] remove_keyboard = ReplyKeyboardRemove() await state.set_state(UserStates.all()[3]) await message.reply(m_text, parse_mode=ParseMode.MARKDOWN, reply=False, reply_markup=remove_keyboard) if callback: await bot.answer_callback_query(callback_query.id)
async def process_publish_message(message: types.Message): uid = message.from_user.id state = dp.current_state(user=uid) state_data = await state.get_data() if message.text.lower() == 'отмена': await state.set_state(MenuStates.all()[0]) return else: await state.set_state(PublishStates.all()[1]) with session_scope() as session: users_q = session.query(User) \ .filter(User.active == True) \ .filter(User.receive_notifications == True) names_list = [x.full_name for x in users_q.all()] await message.reply(MESSAGES['admin_publish_user_list'] + full_names_list_str(names_list), reply=False) result = await message.reply(message.text, reply=False, reply_markup=keyboard_publish) await admin_nav_context.save(user=uid, key='event_id_message_' + str(result.message_id), value=state_data['event_id'])
async def show_event_list_task(uid, markup_only=False): state = dp.current_state(user=uid) with session_scope() as session: events_q = session.query(Event) \ .join(User, User.uid == uid) \ .outerjoin(Enrollment, and_(Enrollment.user_id == User.id, Enrollment.event_id == Event.id)) \ .filter(Event.status == 1) \ .filter(or_(Enrollment.id == None, Enrollment.complete == False)) \ .order_by(Event.edit_datetime.desc()) if events_q.count() > 0: m_text = MESSAGES['show_event_menu'] events_keyboard = events_reply_keyboard(events_q.all()) if markup_only: return events_keyboard else: await bot.send_message(uid, text=m_text, reply_markup=events_keyboard) await state.set_state(UserStates.all()[2]) else: m_text = MESSAGES['no_current_events'] await bot.send_message(uid, text=m_text) return None
async def process_create_event_data(message: types.Message): uid = message.from_user.id state = dp.current_state(user=uid) state_str = str(await state.get_state()) state_number = int(state_str[-1:]) state_data = await state.get_data() or {} if state_str in CreateEventStates.all(): input_data = message.text if message.text != '-' else '' if input_data == button_cancel.text: await state.set_state(MenuStates.all()[0]) await state.set_data({}) await process_start_command_admin(message) return state_data[str(state_number)] = input_data await state.set_data(state_data) if state_number < len(CreateEventStates.all()) - 1: state_number += 1 await state.set_state(CreateEventStates.all()[state_number]) await message.reply(MESSAGES['create_event_prompt_data_' + str(state_number)], reply=False, reply_markup=keyboard_cancel) else: with session_scope() as session: Event(title=state_data['1'], description=state_data['2'], access_info=state_data['3']) \ .insert_me(session) await message.reply(MESSAGES['create_event_done'], reply=False) await state.set_state(MenuStates.all()[0]) await state.set_data({}) await process_start_command_admin(message)
async def decorator(message: types.Message, **partial_data): user_id = message.from_user.id user_state = partial_data.get( 'state', dp.current_state(user=user_id, chat=user_id)) kwargs = { 'user_id': user_id, 'user_state': user_state, 'message': message, } return await resolve_state(func)(**kwargs)
async def process_admin_command(message: types.Message): uid = message.from_user.id state = dp.current_state(user=uid) if uid in config.app.check_admins: if uid not in config.app.admins: config.app.admins.append(uid) await state.set_state(None) await message.reply(MESSAGES['admin_enable'], reply=False) else: config.app.admins.remove(uid) await state.set_state(None) await message.reply(MESSAGES['admin_disable'], reply=False)
async def publish(callback_query: types.CallbackQuery): message = callback_query.message uid = callback_query.from_user.id state = dp.current_state(user=uid) # store event_id of this message event_id, _ = await admin_nav_context.get(user=uid, key=message.message_id) await state.set_data({'event_id': event_id}) await state.set_state(PublishStates.all()[0]) await message.reply(MESSAGES['admin_publish_message'], reply=False) await bot.answer_callback_query(callback_query.id)
async def decorator(callback_query: types.CallbackQuery, **partial_data): message = callback_query.message user_id = callback_query.from_user.id user_state = partial_data.get( 'state', dp.current_state(user=user_id, chat=user_id)) kwargs = { 'user_id': user_id, 'user_state': user_state, 'message': message, 'callback_query': callback_query, } result = await resolve_state(func)(**kwargs) await bot.answer_callback_query(callback_query.id) return result
async def process_start_command(message: types.Message): uid = message.from_user.id state = dp.current_state(user=uid) with session_scope() as session: user_q = session.query(User) \ .filter(User.uid == uid) if user_q.count() == 0: await state.set_state(UserStates.all()[1] ) # greet and prompt for name and surname await message.reply(MESSAGES['greet_new_user'], parse_mode=ParseMode.MARKDOWN, reply=False) else: await show_event_list_task(message.from_user.id)
async def invoice(message: types.Message): uid = message.from_user.id state = dp.current_state(user=uid) state_data = await state.get_data() or {} if message.document is not None: invoice_type = 'document' file_id = message.document.file_id else: invoice_type = 'photo' file_id = message.photo[-1].file_id with session_scope() as session: enroll_q = session.query(Enrollment, Event) \ .join(User) \ .join(Event) \ .filter(User.uid == uid) \ .filter(Event.id == state_data['event_id']) enroll, event = enroll_q.all()[0] enroll.file_type = invoice_type enroll.file_id = file_id enroll.complete = True enroll.edit_datetime = clock.now() logging.info(f'enrollment updated! got invoice: {enroll}') access_info = event.access_info state = dp.current_state(user=uid) await state.set_state(None) await state.set_data({}) events_keyboard = await show_event_list_task(uid, markup_only=True) await message.reply(MESSAGES['registration_complete'] + text(access_info), parse_mode=ParseMode.MARKDOWN, reply=False, reply_markup=events_keyboard)
async def publish_edit_submit(callback_query: types.CallbackQuery): message = callback_query.message uid = callback_query.from_user.id state = dp.current_state(user=uid) event_id = await admin_nav_context.get(user=uid, key='event_id_message_' + str(message.message_id)) if callback_query.data == button_publish_edit.callback_data: await state.set_state(PublishStates.all()[0]) await message.reply(MESSAGES['admin_publish_edit'], reply=False) await bot.answer_callback_query(callback_query.id) return with session_scope() as session: users_q = session.query(User) \ .filter(User.active == True) \ .filter(User.receive_notifications == True) result_map = {} for user in users_q.all(): try: result = await bot.send_message( user.uid, message.text, reply_markup=get_notifications_keyboard( flag=user.receive_notifications)) await user_notify_context.save(user=user.uid, key=result.message_id, value=event_id) result_map[user.full_name] = True except Exception as e: logging.error('Exception sending notification to user:'******', exception is:\n\n' + str(e)) result_map[user.full_name] = False result_str = 'Уведомление отправлено:\n' user_ls = [usr for usr in result_map.keys()] result_str = result_str + '\n'.join([ name + (' - да' if result_map[name] else ' - нет') for name in user_ls ]) await message.reply(result_str, reply=False) await message.edit_reply_markup(reply_markup=None) await state.set_state(MenuStates.all()[0]) await bot.answer_callback_query(callback_query.id)
async def decorator(obj: types.base.TelegramObject, **partial_data): callback_query = None if isinstance(obj, types.CallbackQuery): callback_query = obj message = obj.message user_id = callback_query.from_user.id elif isinstance(obj, types.Message): message = obj user_id = message.from_user.id else: return user_state = partial_data.get( 'state', dp.current_state(user=user_id, chat=user_id)) kwargs = { 'user_id': user_id, 'user_state': user_state, 'message': message, 'callback_query': callback_query, } result = await resolve_state(func)(**kwargs) if callback_query: await bot.answer_callback_query(callback_query.id) return result
async def chat(message: types.Message): state = dp.current_state(user=message.from_user.id) current_state = await state.get_state() m_text = MESSAGES['help_' + str(current_state)] await message.reply(m_text, reply=False)
async def process_start_command_admin(message: types.Message): uid = message.from_user.id state = dp.current_state(user=uid) await state.set_state(MenuStates.all()[0]) await show_events_task_admin(message)