-
Notifications
You must be signed in to change notification settings - Fork 0
/
alim_wrapper.py
190 lines (152 loc) · 4.82 KB
/
alim_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import requests
import json
import inspect
basicSetting = {}
host = "https://kakaoapi.aligo.in"
debug = True
printFunc = lambda: print("======== [", inspect.stack()[1][3], "] ========")
printLog = lambda log: print(json.dumps(log, indent=2, ensure_ascii=False))
class ResponseError(Exception):
def __init__(self, code, msg):
self.code = code
self.msg = msg
def __str__(self):
return str(self.code) + " : " + self.msg
def initSetting(apiKey, senderKey, userId):
"""Initialize basic settings; this settings are used for all request
Args :
apiKey(string) : apiKey for using kakao alim talk service (need to get from smart aligo homepage)
senderKey(string) : senderKey for check sender Info (need to get from smart aligo homepage)
userId(string) : smart aligo user id
"""
basicSetting["apikey"] = apiKey
basicSetting["senderkey"] = senderKey
basicSetting["userid"] = userId
def tokenSetting(token):
"""Initialize token value; token is used for all request
Args :
token(string) : token string, get from getToken function
"""
basicSetting["token"] = token
def sendRequest(url, data={}):
# send data as json fomat to url, and return result as dict
res = requests.post(url, data={**basicSetting, **data})
response = res.json()
if response["code"] != 0:
raise ResponseError(response["code"], response["message"])
return response
def getToken(time):
# get token from API server
# time : token validation duration (minutes)
url = host + "/akv10/token/create/" + str(time) + "/i/"
result = sendRequest(url)
if debug:
printFunc()
printLog(result)
return result["token"]
def authChannel(plusid, phonenumber):
data = locals()
url = host + "/akv10/profile/auth/"
result = sendRequest(url, data)
if debug:
printFunc()
printLog(result)
def getCategory():
url = host + "/akv10/category/"
result = sendRequest(url)
if debug:
printFunc()
printLog(result)
return result["data"]
def addFriendChannel(plusid, authnum, phonenumber, categorycode):
data = locals()
url = host + "/akv10/profile/add/"
result = sendRequest(url, data)
if debug:
printFunc()
printLog(result)
return result["data"]
def listChannel(plusid=None, senderkey=None):
data = locals()
for i in list(data):
if data[i] is None:
del data[i]
url = host + "/akv10/profile/list/"
result = sendRequest(url, data)
if debug:
printFunc()
printLog(result)
return result["list"]
def listTemplates(tpl_code=None):
""" get Lists of all template
Args :
tpl_code(string) : if not None, print a template which has this tpl_code
Returns :
list : return list of dict for template info
"""
data = locals()
for i in list(data):
if data[i] is None:
del data[i]
url = host + "/akv10/template/list/"
result = sendRequest(url, data)
if debug:
printFunc()
printLog(result)
return result["list"]
def listHistory(page=1, limit=50, startdate=None, enddate=None):
data = locals()
for i in list(data):
if data[i] is None:
del data[i]
url = host + "/akv10/history/list/"
result = sendRequest(url, data)
if debug:
printFunc()
printLog(result)
return (
result["list"],
result["currentPage"],
result["totalPage"],
result["totalCount"],
)
def getSendInfo(sender, receiver_1, senddate=None, recvname_1=None):
data = locals()
for i in list(data):
if data[i] is None:
del data[i]
return data
def getTplContent(template):
data = {}
data["tpl_code"] = template["templtCode"]
data["subject_1"] = template["templtName"]
data["message_1"] = template["templtContent"]
data["button_1"] = json.dumps({"button": template["buttons"]}, ensure_ascii=False)
return data
def getFailContent(template, failover="N"):
data = {}
data["failover"] = failover
if failover == "N":
return data
data["fsubject_1"] = template["templtName"]
data["fmessage_1"] = template["templtContent"]
return data
def sendAlimTalk(sendInfo, renderedMsg, failover="N"):
tplContent = getTplContent(renderedMsg)
failContent = getFailContent(renderedMsg, failover)
data = {**sendInfo, **tplContent, **failContent}
url = host + "/akv10/alimtalk/send/"
result = sendRequest(url, data)
if debug:
printFunc()
printLog(result)
return result["info"]
def getDetailHistory(mid):
data = {}
data["mid"] = mid
url = host + "/akv10/history/detail/"
result = sendRequest(url, data)
if debug:
printFunc()
printLog(result)
return result["list"]