There is a user model
class BotUser(models.Model): chat_id = models.BigIntegerField('Чат ID', null=True, blank=True) APPROVAL_CHOICES = ( ('1', 'Сотрудник БА'), ('2', 'Экипаж'), ('3', 'Авиакомпания'), ('4', 'Фрилансер'), ('5', 'Пассажир') ) category = models.CharField('Категория',max_length=120, null=True, blank=True, default='0',choices=APPROVAL_CHOICES) There is also a model for polling with the ability to alert
class Question(TranslatableModel): APPROVAL_CHOICES = ( ('1', 'Сотрудник БА'), ('2', 'Экипаж'), ('3', 'Авиакомпания'), ('4', 'Фрилансер'), ('5', 'Пассажир') ) category = models.CharField('Категория',max_length=120, null=True, blank=True, default='0',choices=APPROVAL_CHOICES) translations = TranslatedFields(interview = models.TextField('Опрос', null=True, blank=True),) nofify = models.BooleanField('Оповестить пользователей о новом вопросе', default=False, blank=True) Next, I connect the celeri and try to make a function.
@task def send(user_category): try: bot = telebot.TeleBot('*****:AAGcHJX1AfS***JulZjC9h9FVdR7BzyrI') if user_category == '0': for user in BotUser.objects.all(): try: mes = '📝Появился новый опрос! \n🤖Нажмите на кнопку «Опросы» чтоб принять участие' bot.send_message(user.chat_id, mes) return HttpResponse('OK') except Exception as e: print(e) pass else: for user in BotUser.objects.filter(category=user_category): try: mes = '📝Появился новый опрос! \n🤖Нажмите на кнопку «Опросник» чтоб принять участие' bot.send_message(user.chat_id, mes) return HttpResponse('OK') except Exception as e: print(e) pass except Exception as e: print(e) bot.send_message(359144124, str(e)) And I’ll add 2 models to the end (Question)
def save(self, *args, **kwargs): if self.pk is None and self.nofify == True: send((self.category,)) super(Question, self).save(*args, **kwargs) Ie here is the source code of the entire class.
class Question(TranslatableModel): APPROVAL_CHOICES = ( ('1', 'Сотрудник БА'), ('2', 'Экипаж'), ('3', 'Авиакомпания'), ('4', 'Фрилансер'), ('5', 'Пассажир') ) category = models.CharField('Категория',max_length=120, null=True, blank=True, default='0',choices=APPROVAL_CHOICES) translations = TranslatedFields(interview = models.TextField('Опрос', null=True, blank=True),) nofify = models.BooleanField('Оповестить пользователей о новом вопросе', default=False, blank=True) def __str__(self): return ' qq ' class Meta: verbose_name = 'Опрос' verbose_name_plural = 'Опрос' def save(self, *args, **kwargs): if self.pk is None and self.nofify == True: send((self.category,)) super(Question, self).save(*args, **kwargs) Logically, when creating a model, an alert should be sent to all users in the bot, but this does not happen D:
when running celery -A freenfobot worker -l info
-------------- celery@milkiweedgtlt v3.1.25 (Cipater) ---- **** ----- --- * *** * -- Linux-4.4.0-116-generic-x86_64-with-Ubuntu-16.04-xenial -- * - **** --- - ** ---------- [config] - ** ---------- .> app: freenfobot:0x7fee49616518 - ** ---------- .> transport: redis://localhost:6379// - ** ---------- .> results: redis://localhost:6379/ - *** --- * --- .> concurrency: 1 (prefork) -- ******* ---- --- ***** ----- [queues] -------------- .> celery exchange=celery(direct) key=celery [tasks] . bot.models.send [2018-03-16 01:29:12,548: INFO/MainProcess] Connected to redis://localhost:6379// [2018-03-16 01:29:12,560: INFO/MainProcess] mingle: searching for neighbors [2018-03-16 01:29:13,570: INFO/MainProcess] mingle: all alone [2018-03-16 01:29:13,583: WARNING/MainProcess] /home/freenfobot/env/lib/python3.6/site-packages/celery/fixups/django.py:265: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments! warnings.warn('Using settings.DEBUG leads to a memory leak, never ' [2018-03-16 01:29:13,584: WARNING/MainProcess] celery@milkiweedgtlt ready. There are no errors, the function simply does not want to be executed, please tell me how to solve it, or is there any alternative?