async def set_delay(message: types.Message, state: FSMContext): try: time_start = datetime.datetime.strptime(message.text, '%H:%M %d.%m.%y') except ValueError: return await error_delay(message, state) now_today = datetime.datetime.utcnow() + datetime.timedelta(hours=3) if time_start < now_today: return await error_delay(message, state) elif time_start - now_today < datetime.timedelta(minutes=10): return await error_delay(message, state) async with state.proxy() as data: data['post']['time_start'] = time_start data['post']['flag'] = 'delay' offset = data['offset'] data['offset'] += 1 header = get_header(data['post']) header_keyboard = edit_header_keyboard(data['post']) text = get_text_with_img(data['post']) post_keyboard = edit_post_keyboard(data['post']) await message.delete() await States.TASK_SETTINGS.set() await bot.edit_message_text(text=header, chat_id=message.chat.id, message_id=message.message_id - (2 + offset), reply_markup=header_keyboard) await bot.edit_message_text(text=text, chat_id=message.chat.id, message_id=message.message_id - (1 + offset), reply_markup=post_keyboard)
async def set_interval(message: types.Message, state: FSMContext): interval = int(re.search(r'\d{2,4}', message.text).group()) if 9 < interval < 1441: async with state.proxy() as data: data['post']['interval'] = interval offset = data['offset'] data['offset'] += 1 text = get_text_with_img(data['post']) header = get_header(data['post']) post_keyboard = edit_post_keyboard(data['post']) header_keyboard = edit_header_keyboard(data['post']) await message.delete() await States.TASK_SETTINGS.set() await bot.edit_message_text(text=header, chat_id=message.chat.id, message_id=message.message_id - (2 + offset), reply_markup=header_keyboard) try: await bot.edit_message_text(text=text, chat_id=message.chat.id, message_id=message.message_id - (1 + offset), reply_markup=post_keyboard) except MessageNotModified: return else: await error_interval(message, state)
async def set_channels(message: types.Message, state: FSMContext): channels = [] channels.extend(list(set(re.findall(r'@\w{5,}\b', message.text)))) channels.extend(list(set(re.findall(r't.me/\w{5,}\b', message.text)))) if channels: async with state.proxy() as data: offset = data['offset'] data['offset'] += 1 data['post']['channels'] = channels text = get_text_with_img(data['post']) header = get_header(data['post']) post_keyboard = edit_post_keyboard(data['post']) header_keyboard = edit_header_keyboard(data['post']) await message.delete() await States.TASK_SETTINGS.set() await bot.edit_message_text(text=header, chat_id=message.chat.id, message_id=message.message_id - (2 + offset), reply_markup=header_keyboard) try: await bot.edit_message_text(text=text, chat_id=message.chat.id, message_id=message.message_id - (1 + offset), reply_markup=post_keyboard) except MessageNotModified: return else: await error_channel(message, state)
async def del_delay(callback: types.CallbackQuery, state: FSMContext): async with state.proxy() as data: data['post']['time_start'] = None data['post']['flag'] = '' header = get_header(data['post']) header_keyboard = edit_header_keyboard(data['post']) text = get_text_with_img(data['post']) post_keyboard = edit_post_keyboard(data['post']) await bot.edit_message_text(text=header, chat_id=callback.message.chat.id, message_id=callback.message.message_id - 1, reply_markup=header_keyboard) await bot.edit_message_text(text=text, chat_id=callback.message.chat.id, message_id=callback.message.message_id, reply_markup=post_keyboard)
async def create_content(message: types.Message, state: FSMContext): text = message.text async with state.proxy() as data: data['post']['text'] = text header = get_header(data['post']) header_keyboard = edit_header_keyboard((data['post'])) post_keyboard = edit_post_keyboard(data['post']) await States.TASK_SETTINGS.set() await bot.delete_message(chat_id=message.chat.id, message_id=message.message_id - 4) await bot.delete_message(chat_id=message.chat.id, message_id=message.message_id - 3) await bot.delete_message(chat_id=message.chat.id, message_id=message.message_id - 2) await bot.delete_message(chat_id=message.chat.id, message_id=message.message_id - 1) await message.answer(text=header, reply_markup=header_keyboard) await message.answer(text=text, reply_markup=post_keyboard)
async def show_task(message: types.Message, state: FSMContext): task_data = db.get_task_data(message.from_user.id, name=message.text[1:]) async with state.proxy() as data: offset = data['offset'] data['offset'] += 1 data['post'] = task_data header = get_header(data['post']) text = get_text_with_img(data['post']) post_keyboard = action_post_keyboard(data['post']) if data['post']['flag'] == 'sleep': header_keyboard = edit_header_keyboard(data['post']) else: header_keyboard = None await message.delete() try: await bot.edit_message_text(text=header, chat_id=message.chat.id, message_id=message.message_id - (2 + offset), reply_markup=header_keyboard) except MessageCantBeEdited: await message.answer(text=header, reply_markup=header_keyboard) async with state.proxy() as data: data['offset'] -= 1 except MessageNotModified: pass try: await bot.edit_message_text(text=text, chat_id=message.chat.id, message_id=message.message_id - (1 + offset), reply_markup=post_keyboard) except MessageCantBeEdited: await message.answer(text=text, reply_markup=post_keyboard) except MessageNotModified: return
async def stop_post(callback: types.CallbackQuery, state: FSMContext): async with state.proxy() as data: data['post']['flag'] = 'sleep' data['post']['time_start'] = None db.edit_task(user_id=data['post']['user_id'], name=data['post']['name'], flag=data['post']['flag'], time_start=data['post']['time_start']) t = task_list.pop(data['post']['name'], None) if t: t.cancel() text = get_text_with_img(data['post']) post_keyboard = action_post_keyboard(data['post']) header = get_header(data['post']) header_keyboard = edit_header_keyboard(data['post']) await bot.edit_message_text(text=header, chat_id=callback.message.chat.id, message_id=callback.message.message_id - 1, reply_markup=header_keyboard) await bot.edit_message_text(text=text, chat_id=callback.message.chat.id, message_id=callback.message.message_id, reply_markup=post_keyboard)