diff --git a/manager/views.py b/manager/views.py index 7ffd9f5..9b8b89d 100644 --- a/manager/views.py +++ b/manager/views.py @@ -18,6 +18,8 @@ from .filters import WaitingFilter from rest_framework.decorators import action from waiting.tasks import check_ready_to_confirm +from utils.sendmessages import sendsms +from django.utils import timezone # FAQ 리스트 조회만 가능한 ViewSet class FAQViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): @@ -151,6 +153,9 @@ def action(self, request, *args, **kwargs): ) waiting.waiting_status = 'arrived' waiting.confirmed_at = timezone.now() + # 문자 메시지 발송 + phone_number = waiting.user.phone_number + sendsms(phone_number, f"[라인나우] 입장 순서가 되었어요. 3분 내로 입장을 확정해 주세요!") elif action == 'cancel': if waiting.waiting_status in ['canceled', 'time_over_canceled']: return custom_response( @@ -161,6 +166,9 @@ def action(self, request, *args, **kwargs): ) waiting.waiting_status = 'canceled' waiting.canceled_at = timezone.now() + # 문자 메시지 발송 + phone_number = waiting.user.phone_number + sendsms(phone_number, f"[라인나우] 부스 사정으로 인해, 대기가 취소되었어요") else: return custom_response( data=None, diff --git a/sms/urls.py b/sms/urls.py index b3a1c19..bc9fa95 100644 --- a/sms/urls.py +++ b/sms/urls.py @@ -1,12 +1,11 @@ from django.urls import path, include from rest_framework.routers import DefaultRouter -from .views import * app_name = 'sms' -router = DefaultRouter() -router.register(r'sendsms', sendsms, basename='sendsms') +#router = DefaultRouter() +#router.register(r'sendsms', sendsms, basename='sendsms') urlpatterns = [ - path('sms/sendsms', sendsms, name='sendsms') + #path('sms/sendsms', sendsms, name='sendsms') ] \ No newline at end of file diff --git a/utils/sendmessages.py b/utils/sendmessages.py new file mode 100644 index 0000000..b5d28ad --- /dev/null +++ b/utils/sendmessages.py @@ -0,0 +1,59 @@ +from rest_framework import status +from utils.responses import custom_response +from django.conf import settings +import requests, re + +SSODAA_BASE_URL = settings.SSODAA_BASE_URL +SEND_PHONE = settings.SEND_PHONE + +# 문자 발송하기 +def sendsms(dest_phone, msg_body): + # 데이터 형식 검증 + phone_pattern = re.compile(r'^\d{10,11}$') # 10자리 또는 11자리 숫자만 + if not phone_pattern.match(dest_phone): + return { + "message": "Invalid destination phone number.", + } + + if not isinstance(msg_body, str): + return { + "message": "Invalid message content.", + } + + + headers = { + 'x-api-key': settings.SMS_API_KEY, + 'Content-Type': 'application/json; charset=utf-8' + } + + data = { + "token_key": settings.SMS_TOKEN_KEY, + "msg_type": 'sms', + "dest_phone": dest_phone, + "send_phone": SEND_PHONE, + "msg_body": msg_body, + } + print(data) + return custom_response(data=data, message="perfect", code=status.HTTP_200_OK) + """ + try: + url = f"{SSODAA_BASE_URL}/sms/send/sms" + response = requests.post(url, json=data, headers=headers) + response = response.json() + status_code = int(response.get('code')) + + if status_code == 200: + content = response.get('content') + data = { + "sent_messages":content.get('sent_messages'), + "send_phone":content.get('send_phone'), + } + return custom_response(data=data, message=content.get('message'), code=status.HTTP_200_OK) + + else: + error = response.get('error') + return custom_response(message=error, code=status.HTTP_403_FORBIDDEN, success=False) + + except requests.exceptions.RequestException: + return custom_response(message="Failed to send messages.", code=status.HTTP_400_BAD_REQUEST, success=False) + """ \ No newline at end of file diff --git a/waiting/tasks.py b/waiting/tasks.py index 9cf4687..b6857c2 100644 --- a/waiting/tasks.py +++ b/waiting/tasks.py @@ -1,5 +1,6 @@ from celery import shared_task from django.utils import timezone +from utils.sendmessages import sendsms @shared_task def check_ready_to_confirm(waiting_id): @@ -10,17 +11,27 @@ def check_ready_to_confirm(waiting_id): # 3분이 지났으면 확인하고 취소 처리 if waiting.is_ready_to_confirm_expired() and waiting.waiting_status == 'ready_to_confirm': waiting.set_time_over_canceled() # 시간 초과로 취소 처리 + # 문자 메시지 발송 + phone_number = waiting.user.phone_number + print(phone_number) + sendsms(phone_number, f"[라인나우] 3분 내에 부스 입장을 확정하지 않아, 대기가 취소되었어요") except Waiting.DoesNotExist: pass @shared_task def check_confirmed(waiting_id): + print("기다림은 끝") """10분 후 입장 확정 상태에서 도착하지 않으면 time_over_canceled 상태로 변경""" from .models import Waiting try: + print("데이터가져와") waiting = Waiting.objects.get(pk=waiting_id) # 10분이 지났으면 취소 처리 if waiting.is_confirmed_expired() and waiting.waiting_status == 'confirmed': waiting.set_time_over_canceled() # 시간 초과로 취소 처리 + # 문자 메시지 발송 + phone_number = waiting.user.phone_number + print(phone_number) + sendsms(phone_number, f"[라인나우] 10분 내에 부스에 입장하지 않아, 대기가 취소되었어요") except Waiting.DoesNotExist: pass \ No newline at end of file