Skip to content

Commit 602e57f

Browse files
committed
하나의 파일에 라이브러리 코드가 함께 들어가 있는 발송 예제 추가
1 parent ceefca0 commit 602e57f

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ message_list.py 메시지 목록 조회
2121
group 그룹발송 예제
2222
storage 파일(이미지) 관리
2323
send_one 단건 발송 예제
24+
all_in_one 하나의 파일에 라이브러리 코드가 함께 들어있는 발송 예제
2425
```

examples/all_in_one.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import json
2+
import time
3+
import datetime
4+
import uuid
5+
import hmac
6+
import hashlib
7+
import requests
8+
9+
# apiKey, apiSecret 입력 필수
10+
apiKey = 'INPUT YOUR API KEY'
11+
apiSecret = 'INPUT YOUR SECRET KEY'
12+
13+
# 아래 값은 필요시 수정
14+
protocol = 'https'
15+
domain = 'api.solapi.com'
16+
prefix = ''
17+
18+
def unique_id():
19+
return str(uuid.uuid1().hex)
20+
21+
def get_iso_datetime():
22+
utc_offset_sec = time.altzone if time.localtime().tm_isdst else time.timezone
23+
utc_offset = datetime.timedelta(seconds=-utc_offset_sec)
24+
return datetime.datetime.now().replace(tzinfo=datetime.timezone(offset=utc_offset)).isoformat()
25+
26+
def get_signature(key, msg):
27+
return hmac.new(key.encode(), msg.encode(), hashlib.sha256).hexdigest()
28+
29+
def get_headers(apiKey, apiSecret):
30+
date = get_iso_datetime()
31+
salt = unique_id()
32+
data = date + salt
33+
return {
34+
'Authorization': 'HMAC-SHA256 ApiKey=' + apiKey + ', Date=' + date + ', salt=' + salt + ', signature=' +
35+
get_signature(apiSecret, data),
36+
'Content-Type': 'application/json; charset=utf-8'
37+
}
38+
39+
def getUrl(path):
40+
url = '%s://%s' % (protocol, domain)
41+
if prefix != '':
42+
url = url + prefix
43+
url = url + path
44+
return url
45+
46+
def sendMany(data):
47+
return requests.post(getUrl('/messages/v4/send-many'), headers=get_headers(apiKey, apiSecret), json=data)
48+
49+
# 한번 요청으로 1만건의 메시지 발송이 가능합니다.
50+
if __name__ == '__main__':
51+
data = {
52+
'messages': [
53+
{
54+
'to': '01000000001',
55+
'from': '029302266',
56+
'text': '한글 45자, 영자 90자 이하 입력되면 자동으로 SMS타입의 메시지가 추가됩니다.'
57+
},
58+
{
59+
'to': '01000000002',
60+
'from': '029302266',
61+
'text': '한글 45자, 영자 90자 이상 입력되면 자동으로 LMS타입의 문자메시자가 발송됩니다. 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ'
62+
},
63+
{
64+
'type': 'SMS',
65+
'to': '01000000003',
66+
'from': '029302266',
67+
'text': 'SMS 타입에 한글 45자, 영자 90자 이상 입력되면 오류가 발생합니다. 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ'
68+
},
69+
{
70+
'to': [ '01000000004', '01000000005' ], # 수신번호를 array로 입력하면 같은 내용을 여러명에게 보낼 수 있습니다.
71+
'from': '029302266',
72+
'text': '한글 45자, 영자 90자 이하 입력되면 자동으로 SMS타입의 메시지가 발송됩니다.'
73+
},
74+
# 해외발송
75+
{
76+
'country': '1', # 미국(1), 일본(81), 중국(86) 등 국가번호 입력
77+
'to': '01000000006', # 수신번호를 array로 입력하면 같은 내용을 여러명에게 보낼 수 있습니다.
78+
'from': '029302266',
79+
'text': '한글 45자, 영자 90자 이하 입력되면 자동으로 SMS타입의 메시지가 발송됩니다.'
80+
},
81+
# 알림톡 발송
82+
{
83+
'to': '01000000004',
84+
'from': '029302266',
85+
'text': '버튼은 최대 5개까지 추가 가능하며 템플릿 내용과 마찬가지로 버튼 내용도 등록 및 검수 받은 내용 그대로 입력되어야 합니다.',
86+
'kakaoOptions': {
87+
'pfId': 'KA01PF200323182344986oTFz9CIabcx',
88+
'templateId': 'KA01TP200323182345741y9yF20aabcx',
89+
'buttons': [
90+
{
91+
'buttonType': 'WL', # 웹링크
92+
'buttonName': '버튼 이름',
93+
'linkMo': 'https://m.example.com',
94+
'linkPc': 'https://example.com' # 템플릿 등록 시 모바일링크만 입력하였다면 linkPc 값은 입력하시면 안됩니다.
95+
},
96+
{
97+
'buttonType': 'AL', # 앱링크
98+
'buttonName': '실행 버튼',
99+
'linkAnd': 'examplescheme://', # 안드로이드
100+
'linkIos': 'examplescheme://' # iOS
101+
},
102+
{
103+
'buttonType': 'DS', # 배송조회
104+
'buttonName': '배송 조회'
105+
},
106+
{
107+
'buttonType': 'BK', # 봇키워드(챗봇에게 키워드를 전달합니다. 버튼이름의 키워드가 그대로 전달됩니다.)
108+
'buttonName': '봇키워드'
109+
},
110+
{
111+
'buttonType': 'MD', # 상담요청하기 (상담요청하기 버튼을 누르면 메시지 내용이 상담원에게 그대로 전달됩니다.)
112+
'buttonName': '상담요청하기'
113+
}
114+
]
115+
}
116+
}
117+
# ...
118+
# 1만건까지 추가 가능
119+
]
120+
}
121+
res = sendMany(data)
122+
print(json.dumps(res.json(), indent=2, ensure_ascii=False))

0 commit comments

Comments
 (0)