Skip to content

Commit

Permalink
Merge pull request #94 from jiyoon607/jjy_sms
Browse files Browse the repository at this point in the history
✨ Feat: 상황 별 문자 발송
  • Loading branch information
dudtlstm authored Oct 6, 2024
2 parents 508ebe0 + 883142d commit d3a7b41
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
8 changes: 8 additions & 0 deletions manager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions sms/urls.py
Original file line number Diff line number Diff line change
@@ -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')
]
59 changes: 59 additions & 0 deletions utils/sendmessages.py
Original file line number Diff line number Diff line change
@@ -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)
"""
11 changes: 11 additions & 0 deletions waiting/tasks.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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

0 comments on commit d3a7b41

Please sign in to comment.