From 387bbdac0b6ebdbdb48a097c42dd8b92c01915d5 Mon Sep 17 00:00:00 2001 From: gmlstjq123 <133740938+gmlstjq123@users.noreply.github.com> Date: Mon, 21 Aug 2023 18:05:47 +0900 Subject: [PATCH] Create lambda_function.py --- lambda_function.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lambda_function.py diff --git a/lambda_function.py b/lambda_function.py new file mode 100644 index 0000000..c399cd4 --- /dev/null +++ b/lambda_function.py @@ -0,0 +1,55 @@ +import json +import boto3 + +def send_sms_message(event, context): + phone = "{phone-number}" + # SQS 메시지 정보 추출 + records = event.get('Records', []) + if not records: + return { + 'statusCode': 400, + 'body': json.dumps('SQS 메시지 정보가 없습니다.') + } + + sqs_message = records[0].get('body') + if not sqs_message: + return { + 'statusCode': 400, + 'body': json.dumps('SQS 메시지가 없습니다.') + } + # SQS 메시지의 JSON 데이터 추출 + try: + data = json.loads(sqs_message) + phoneNumber = data.get('phoneNumber') + content = data.get('content') + except Exception as e: + return { + 'statusCode': 400, + 'body': json.dumps('SQS 메시지 파싱 오류: ' + str(e)) + } + # 전화번호와 내용 확인 및 처리 + if phoneNumber and content: + print("전화번호: ", phoneNumber) + print("내용: ", content) + + sns = boto3.client('sns', region_name='ap-northeast-1', aws_access_key_id='{AccessKey}', + aws_secret_access_key='{SecretKey}') + + try: + print(phoneNumber + " / " + content ) + response = sns.publish( + PhoneNumber=phone, + Message=phoneNumber + " / " + content + ) + print(response) + return { + 'statusCode': 200, + 'body': json.dumps('메시지가 성공적으로 전송되었습니다.') + } + except Exception as e: + error_message = str(e) + print(error_message) + return { + 'statusCode': 500, + 'body': json.dumps('메시지 전송 중 오류가 발생하였습니다. 에러 메시지: ' + error_message) + }