fitness_bot/app/bot/tasks.py
2025-02-18 20:58:04 +07:00

74 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import traceback
from datetime import timedelta
from celery.app import shared_task
from django.core.mail import send_mail
from django.utils.timezone import now
from bot.models.cheque import Cheque
from bot.models.user_state import UserState
from project.logging_settings import info_logger, error_logger
from project.settings import EMAIL_HOST_USER, TELEGRAM_API_URL
from settings.models import Settings
import requests
# info_logger.info("Это информационное сообщение1")
def send_message(method, data):
telegram_token = Settings.get_setting("TELEGRAM_TOKEN")
url = TELEGRAM_API_URL + telegram_token + '/' + method
response = requests.post(url, json=data)
return response
@shared_task
def send_email(email, code):
subject = "Код подтверждения для регистрации"
message = f"Ваш код подтверждения: {code}"
recipient_list = [email]
try:
send_mail(subject, message, EMAIL_HOST_USER, recipient_list)
print(f"Код отправлен на {email}")
except Exception:
error_logger.error(traceback.format_exc())
@shared_task
def send_message_to_user(chat_id, message):
try:
send_message("sendMessage", {
'chat_id': chat_id,
'text': message,
})
except Exception:
error_logger.error(traceback.format_exc())
@shared_task
def send_message_to_user_generic(obj):
try:
send_message("sendMessage", obj)
except Exception:
error_logger.error(traceback.format_exc())
@shared_task
def remind_about_cheque():
try:
last_month = now() - timedelta(days=30)
users = UserState.objects.filter(is_registered=True)
for user in users:
last_receipt = Cheque.objects.filter(user=user).order_by(
'-uploaded_at').first()
if not last_receipt or last_receipt.uploaded_at < last_month:
send_message_to_user.delay(
user.chat_id,
"Пожалуйста, загрузите новый чек за текущий месяц!"
)
except Exception:
error_logger.error(traceback.format_exc())