Skip to content

Commit 76b7441

Browse files
Merge pull request #10 from Palbahngmiyine/main
Add Python examples
2 parents d623bac + 31aad84 commit 76b7441

File tree

10 files changed

+235
-18
lines changed

10 files changed

+235
-18
lines changed

examples/balance/get_balance.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from solapi import SolapiMessageService
2+
3+
# API 키와 API Secret을 설정합니다
4+
message_service = SolapiMessageService(
5+
api_key="YOUR_API_KEY", api_secret="YOUR_API_SECRET"
6+
)
7+
8+
try:
9+
# 잔액을 조회합니다
10+
balance_response = message_service.get_balance()
11+
12+
print("잔액 조회 성공!")
13+
print(f"현재 잔액: {balance_response.balance}원")
14+
print(f"포인트: {balance_response.point}P")
15+
16+
except Exception as e:
17+
print(f"잔액 조회 실패: {str(e)}")

examples/group/get_groups.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from solapi import SolapiMessageService
2+
3+
# API 키와 API Secret을 설정합니다
4+
message_service = SolapiMessageService(
5+
api_key="YOUR_API_KEY", api_secret="YOUR_API_SECRET"
6+
)
7+
8+
# 메시지 그룹을 생성하고 조회합니다
9+
try:
10+
# 그룹 목록을 조회합니다
11+
groups_response = message_service.get_groups()
12+
print("메시지 그룹 목록:")
13+
for group_id, group in groups_response.group_list.items():
14+
print("----")
15+
print(f"\nGroup ID: {group_id}")
16+
print(f"생성 시간: {group.date_created}")
17+
print(f"메시지 수: {group.count.total}")
18+
print(f"상태: {group.status}")
19+
print(f"처리 로그: {group.log}")
20+
print("----")
21+
22+
except Exception as e:
23+
print(f"그룹 조회 실패: {str(e)}")

examples/images/example.jpg

60.5 KB
Loading

examples/messages/get_messages.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from solapi import SolapiMessageService
2+
# from solapi.model.request.messages.get_messages import GetMessagesRequest
3+
4+
message_service = SolapiMessageService(
5+
api_key="YOUR_API_KEY", api_secret="YOUR_API_SECRET"
6+
)
7+
8+
try:
9+
response = message_service.get_messages(
10+
# 메시지를 조회할 때 아래와 같이 조건을 지정할 수 있습니다.
11+
# GetMessagesRequest(
12+
# start_date="2025-01-01",
13+
# end_date="2025-01-03"
14+
# )
15+
)
16+
print(response)
17+
except Exception as e:
18+
print(e)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from solapi import SolapiMessageService
2+
from solapi.model import Message
3+
from solapi.model.kakao.kakao_option import KakaoOption
4+
5+
# API 키와 API Secret을 설정합니다
6+
message_service = SolapiMessageService(
7+
api_key="YOUR_API_KEY", api_secret="YOUR_API_SECRET"
8+
)
9+
10+
# 카카오 알림톡 발송을 위한 옵션을 생성합니다.
11+
kakao_option = KakaoOption(
12+
pf_id="계정에 등록된 카카오 비즈니스 채널ID",
13+
template_id="계정에 등록된 카카오 알림톡 템플릿 ID",
14+
# 만약에 템플릿에 변수가 있다면 아래와 같이 설정합니다.
15+
# variables={
16+
# "#{name}": "홍길동",
17+
# "#{age}": 30
18+
# }
19+
)
20+
21+
# 단일 메시지를 생성합니다
22+
message = Message(
23+
from_="발신번호", # 발신번호 (등록된 발신번호만 사용 가능)
24+
to="수신번호", # 수신번호
25+
kakao_options=kakao_option,
26+
)
27+
28+
# 메시지를 발송합니다
29+
try:
30+
response = message_service.send(message)
31+
print("메시지 발송 성공!")
32+
print(f"Group ID: {response.group_info.group_id}")
33+
print(f"요청한 메시지 개수: {response.group_info.count.total}")
34+
print(f"성공한 메시지 개수: {response.group_info.count.registered}")
35+
except Exception as e:
36+
print(f"메시지 발송 실패: {str(e)}")

examples/simple/send_many.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from solapi import SolapiMessageService
2+
from solapi.model import Message, SendRequestConfig
3+
4+
# API 키와 API Secret을 설정합니다
5+
message_service = SolapiMessageService(
6+
api_key="YOUR_API_KEY", api_secret="YOUR_API_SECRET"
7+
)
8+
9+
# 여러 메시지를 생성합니다
10+
messages = [
11+
Message(from_="발신번호", to="수신번호1", text="첫 번째 메시지입니다."),
12+
Message(from_="발신번호", to="수신번호2", text="두 번째 메시지입니다."),
13+
Message(from_="발신번호", to="수신번호3", text="세 번째 메시지입니다."),
14+
]
15+
16+
# SendRequestConfig를 사용하여 중복 수신번호 허용 설정
17+
config = SendRequestConfig(
18+
allow_duplicates=True # 중복 수신번호 허용
19+
)
20+
21+
# 메시지를 발송합니다
22+
try:
23+
response = message_service.send(messages, config)
24+
print("메시지 발송 성공!")
25+
print(f"Group ID: {response.group_info.group_id}")
26+
print(f"요청한 메시지 개수: {response.group_info.count.total}")
27+
print(f"성공한 메시지 개수: {response.group_info.count.registered}")
28+
29+
# 실패한 메시지가 있는 경우
30+
if response.failed_message_list:
31+
print("\n실패한 메시지 목록:")
32+
for failed in response.failed_message_list:
33+
print(f"수신번호: {failed.message.to}")
34+
print(f"실패 사유: {failed.error.message}\n")
35+
except Exception as e:
36+
print(f"메시지 발송 실패: {str(e)}")

examples/simple/send_mms.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from os.path import abspath
2+
from solapi import SolapiMessageService
3+
from solapi.model import Message
4+
from solapi.model.request.storage import FileTypeEnum
5+
6+
# API 키와 API Secret을 설정합니다
7+
message_service = SolapiMessageService(
8+
api_key="YOUR_API_KEY", api_secret="YOUR_API_SECRET"
9+
)
10+
11+
# 이미지 파일을 업로드합니다
12+
try:
13+
# 이미지 파일 업로드 (MMS 타입으로 지정)
14+
file_response = message_service.upload_file(
15+
file_path=abspath(
16+
"../images/example.jpg"
17+
), # 실제 이미지 파일 경로로 변경해주세요
18+
upload_type=FileTypeEnum.MMS,
19+
)
20+
21+
print("파일 업로드 성공!")
22+
print(f"File ID: {file_response.file_id}")
23+
24+
# MMS 메시지를 생성하고 발송합니다
25+
message = Message(
26+
from_="발신번호", # 발신번호 (등록된 발신번호만 사용 가능)
27+
to="수신번호", # 수신번호
28+
# subject="MMS 제목", # MMS 제목, 제목을 지정하지 않는다면 필요하지 않습니다.
29+
text="MMS 메시지 내용입니다.",
30+
image_id=file_response.file_id, # 업로드된 파일의 ID를 지정
31+
)
32+
33+
# 메시지를 발송합니다
34+
response = message_service.send(message)
35+
print("\nMMS 발송 성공!")
36+
print(f"Group ID: {response.group_id}")
37+
print(f"요청한 메시지 개수: {response.group_info.count.total}")
38+
print(f"성공한 메시지 개수: {response.group_info.count.registered}")
39+
40+
except Exception as e:
41+
print(f"MMS 발송 실패: {str(e)}")

examples/simple/send_sms.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from solapi import SolapiMessageService
2+
from solapi.model import Message
3+
4+
# API 키와 API Secret을 설정합니다
5+
message_service = SolapiMessageService(
6+
api_key="YOUR_API_KEY", api_secret="YOUR_API_SECRET"
7+
)
8+
9+
# 단일 메시지를 생성합니다
10+
message = Message(
11+
from_="발신번호", # 발신번호 (등록된 발신번호만 사용 가능)
12+
to="수신번호", # 수신번호
13+
text="안녕하세요! SOLAPI Python SDK를 사용한 SMS 발송 예제입니다.",
14+
)
15+
16+
# 메시지를 발송합니다
17+
try:
18+
response = message_service.send(message)
19+
print("메시지 발송 성공!")
20+
print(f"Group ID: {response.group_info.group_id}")
21+
print(f"요청한 메시지 개수: {response.group_info.count.total}")
22+
print(f"성공한 메시지 개수: {response.group_info.count.registered}")
23+
except Exception as e:
24+
print(f"메시지 발송 실패: {str(e)}")

examples/sms/sms.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

examples/storage/upload_file.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from solapi import SolapiMessageService
2+
from solapi.model.request.storage import FileTypeEnum
3+
4+
# API 키와 API Secret을 설정합니다
5+
message_service = SolapiMessageService(
6+
api_key="YOUR_API_KEY", api_secret="YOUR_API_SECRET"
7+
)
8+
9+
10+
def upload_file_example(file_path: str, file_type: FileTypeEnum):
11+
"""
12+
파일을 업로드하는 예제 함수입니다.
13+
14+
Args:
15+
file_path (str): 업로드할 파일의 경로
16+
file_type (FileTypeEnum): 파일 타입 (MMS, KAKAO, DOCUMENT 등)
17+
"""
18+
try:
19+
# 파일 업로드
20+
response = message_service.upload_file(
21+
file_path=file_path, upload_type=file_type
22+
)
23+
24+
print("파일 업로드 성공!")
25+
print(f"File ID: {response.file_id}")
26+
print(f"File Name: {response.name}")
27+
print(f"File Created: {response.date_created}")
28+
29+
return response.file_id
30+
31+
except Exception as e:
32+
print(f"파일 업로드 실패: {str(e)}")
33+
return None
34+
35+
36+
# 예제 실행
37+
if __name__ == "__main__":
38+
# MMS 이미지 업로드 예제
39+
# 현재 예제는 images 폴더에 있는 example.jpg 파일을 불러오지만, 실제 예제 사용시에는 이미지 파일의 경로를 변경해주세요
40+
mms_file_id = upload_file_example("../images/example.jpg", FileTypeEnum.MMS)

0 commit comments

Comments
 (0)