0

I have Django+aiogram bot and that's how my mailing works:
Manager/admin create a instance of the Post/Poll model in the admin panel -> Admin approve/disapprove it in telegram PM with inline keyboard -> if approves ("post" callback data) -> do_mailing function immediately works

But I added the "scheduled_time = models.DateTimeField" field in my Post/Poll models and I want do_mailing function to work in that scheduled_time once, without repeats.

Post model:

class Post(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='Менеджер')
    text = models.TextField(verbose_name='Текст')
    scheduled_time = models.DateTimeField(blank=True, null=True, verbose_name="Дата и время рассылки")
    approved = models.BooleanField(default=False, verbose_name='Подтверждён')
    total_media_count = models.PositiveIntegerField(default=0, verbose_name='Сколько фотографий загрузите?')
    group = models.ForeignKey('bot_users.BotUserGroup', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='Группа пользователей бота')
    created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name='Создано')

There's a handler for "post" callback data which works after admin's approval:

@dp.callback_query_handler(lambda query: query.data == 'post', state="*")
async def post_handler(callback_query: CallbackQuery, state: FSMContext):
    admin = callback_query.message['chat']['id']
    message = callback_query.message.text.split('\n')
    await change_post_status(message[0].split(': ')[1])
    await do_mailing(message)
    logging.info(f"Пост {message[0].split(': ')[1]} был одобрен на рассылку админом")
    await bot.send_message(chat_id=admin, text=f"Рассылка поста {message[0].split(': ')[1]} одобрена!")
    await bot.send_message(chat_id=message[-1].split(': ')[1].split(' ')[0], text=f"Рассылка поста {message[0].split(': ')[1]} одобрена!")

So do_mailing function is what I need to schedule. I can get "scheduled_time" field from the "message" variable, so don't think about it, let's just say that I have this field of the instance in this handler

How can I do that schedule?

Maybe aiocron, maybe celery, schedule etc. The easiest way would be the best)

Bte Deni
  • 41
  • 5
  • What have you tried so far. You quote several solutions, what is the problem with those solutions ? – Itération 122442 Aug 25 '23 at 11:59
  • I've tried aiocron (just tried to see if it works with datetime), but it didn't work. I'm more trying to find a solution for this, cause I've never done something like that before. – Bte Deni Aug 25 '23 at 12:08

0 Answers0