-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from jiyoon607/jjy_sms
✨ Feat: 상황 별 문자 발송
- Loading branch information
Showing
4 changed files
with
81 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters