Skip to content

Commit cb1bccc

Browse files
committed
update
1 parent 5487952 commit cb1bccc

File tree

5 files changed

+980
-143
lines changed

5 files changed

+980
-143
lines changed

PyKakao/api.py

Lines changed: 39 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def __init__(self, service_key=None, redirect_uri="https://localhost:5000", scop
2121
self.service_key = service_key
2222
self.redirect_uri = redirect_uri
2323
self.scope = scope
24-
self.url = "https://kapi.kakao.com/v2/api/talk/memo/default/send"
24+
self.url_message_me = "https://kapi.kakao.com/v2/api/talk/memo/default/send"
25+
self.url_message_friend = "https://kapi.kakao.com/v1/api/talk/friends/message/default/send"
2526

2627
def get_url_for_generating_code(self):
2728
"""
@@ -110,164 +111,69 @@ def set_access_token(self, access_token):
110111
access_token : str
111112
액세스 토큰
112113
"""
113-
self.headers = {"Authorization": f"Bearer {access_token}"}
114+
self.headers = {
115+
"Authorization": f"Bearer {access_token}",
116+
"Content-Type": "application/x-www-form-urlencoded",
117+
}
114118
print("액세스 토큰 설정 완료")
115119

116-
def send_text(self, text, link, **kwargs):
117-
"""
118-
텍스트 메시지 전송
119-
120-
Parameters
121-
----------
122-
text : str
123-
전송할 메시지 (최대 200자)
124-
"""
125-
params = {
126-
"object_type": "text",
127-
"text": text,
128-
"link": link,
129-
}
130-
params.update(kwargs)
131-
data = {"template_object": json.dumps(params)}
132-
try:
133-
r = requests.post(self.url, headers=self.headers, data=data)
134-
r.raise_for_status()
135-
except Exception as e:
136-
print(f"메시지 전송 실패")
137-
print(e)
138-
return
139-
if r.json()['result_code'] == 0:
140-
print("메시지 전송 성공")
141-
else:
142-
print(f"메시지 전송 실패")
143-
print(r.json())
144-
145-
def send_feed(self, content, **kwargs):
146-
"""
147-
피드 메시지 전송
120+
def send_message_to_me(self, message_type, **kwargs):
121+
"""나에게 보내기 API
148122
149123
Parameters
150124
----------
151-
content : dict
152-
피드 내용
125+
message_type : str
126+
메시지 유형 ('text', 'feed', 'list', 'location', 'commerce' 등)
127+
kwargs : dict
128+
메시지 유형에 따른 추가 파라미터
153129
"""
154-
params = {
155-
"object_type": "feed",
156-
"content": content,
157-
}
130+
params = {"object_type": message_type}
158131
params.update(kwargs)
159132
data = {"template_object": json.dumps(params)}
160133
try:
161-
r = requests.post(self.url, headers=self.headers, data=data)
134+
r = requests.post(self.url_message_me, headers=self.headers, data=data)
162135
r.raise_for_status()
163136
except Exception as e:
164-
print(f"메시지 전송 실패")
137+
print("메시지 전송 실패")
165138
print(e)
166139
return
167140
if r.json()['result_code'] == 0:
168141
print("메시지 전송 성공")
169142
else:
170-
print(f"메시지 전송 실패")
143+
print("메시지 전송 실패")
171144
print(r.json())
172145

173-
def send_list(self, header_title, header_link, contents, **kwargs):
174-
"""
175-
리스트 메시지 전송
146+
def send_message_to_friend(self, message_type, receiver_uuids, **kwargs):
147+
"""친구에게 보내기 API
176148
177149
Parameters
178150
----------
179-
header_title : str
180-
리스트 제목
181-
header_link : dict
182-
리스트 링크
183-
contents : list
184-
리스트 내용
185-
"""
186-
params = {
187-
"object_type": "list",
188-
"header_title": header_title,
189-
"header_link": header_link,
190-
"contents": contents,
191-
}
151+
message_type : str
152+
메시지 유형 ('text', 'feed', 'list', 'location', 'commerce' 등)
153+
receiver_uuids : list
154+
받는 사람의 고유 ID 목록
155+
kwargs : dict
156+
메시지 유형에 따른 추가 파라미터
157+
"""
158+
params = {"object_type": message_type}
192159
params.update(kwargs)
193-
data = {"template_object": json.dumps(params)}
194-
try:
195-
r = requests.post(self.url, headers=self.headers, data=data)
196-
r.raise_for_status()
197-
except Exception as e:
198-
print(f"메시지 전송 실패")
199-
print(e)
200-
return
201-
if r.json()['result_code'] == 0:
202-
print("메시지 전송 성공")
203-
else:
204-
print(f"메시지 전송 실패")
205-
print(r.json())
206-
207-
def send_location(self, address, address_title, content, **kwargs):
208-
"""
209-
위치 메시지 전송
210-
211-
Parameters
212-
----------
213-
address : str
214-
주소
215-
address_title : str
216-
주소 제목
217-
content : dict
218-
위치 정보
219-
"""
220-
params = {
221-
"object_type": "location",
222-
"address": address,
223-
"address_title": address_title,
224-
"content": content,
225-
}
226-
params.update(kwargs)
227-
data = {"template_object": json.dumps(params)}
228-
try:
229-
r = requests.post(self.url, headers=self.headers, data=data)
230-
r.raise_for_status()
231-
except Exception as e:
232-
print(f"메시지 전송 실패")
233-
print(e)
234-
return
235-
if r.json()['result_code'] == 0:
236-
print("메시지 전송 성공")
237-
else:
238-
print(f"메시지 전송 실패")
239-
print(r.json())
240-
241-
def send_commerce(self, content, commerce, **kwargs):
242-
"""
243-
커머스 메시지 전송
244-
245-
Parameters
246-
----------
247-
content : dict
248-
커머스 정보
249-
commerce : dict
250-
상품 정보
251-
"""
252-
params = {
253-
"object_type": "commerce",
254-
"content": content,
255-
"commerce": commerce,
256-
}
257-
params.update(kwargs)
258-
data = {"template_object": json.dumps(params)}
160+
data = {
161+
"receiver_uuids": json.dumps(receiver_uuids),
162+
"template_object": json.dumps(params),
163+
}
259164
try:
260-
r = requests.post(self.url, headers=self.headers, data=data)
165+
r = requests.post(self.url_message_friend, headers=self.headers, data=data)
261166
r.raise_for_status()
167+
response = r.json()
168+
# 성공한 UUID 목록 확인
169+
if "successful_receiver_uuids" in response:
170+
print("메시지 전송 성공:", response["successful_receiver_uuids"])
171+
# 실패 정보 확인
172+
if "failure_info" in response:
173+
print("일부 메시지 전송 실패:", response["failure_info"])
262174
except Exception as e:
263-
print(f"메시지 전송 실패")
175+
print("메시지 전송 실패")
264176
print(e)
265-
return
266-
if r.json()['result_code'] == 0:
267-
print("메시지 전송 성공")
268-
else:
269-
print(f"메시지 전송 실패")
270-
print(r.json())
271177

272178

273179
class KoGPT:

PyKakao/config/info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__project__ = "PyKakao"
2-
__version__ = "0.0.6"
2+
__version__ = "0.0.7"
33
__author__ = "정우일(Wooil Jeong)"
44
__contact__ = "wooil@kakao.com"
55
__github__ = "https://github.com/WooilJeong/PyKakao"

README.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ nsfw_response = api.check_nsfw(encoded_images_for_nsfw_check)
233233

234234
### 메시지 API
235235

236-
메시지 API는 사용자가 카카오톡 친구에게 카카오톡 메시지를 보내는 기능을 제공합니다. PyKakao의 최신 버전에서는 '나에게 보내기' 기능만 이용할 수 있습니다.
236+
메시지 API는 사용자가 카카오톡 친구에게 카카오톡 메시지를 보내는 기능을 제공합니다. PyKakao의 최신 버전에서는 '나에게 보내기'와 '친구에게 보내기' 기능을 이용할 수 있습니다.
237237

238238
메시지 API의 경우 아래 '카카오 로그인 관련 설정하기'와 같이 설정 후 정상적으로 이용할 수 있습니다.
239239

@@ -269,15 +269,40 @@ access_token = MSG.get_access_token_by_redirected_url(url)
269269
# 액세스 토큰 설정
270270
MSG.set_access_token(access_token)
271271

272-
# 텍스트 메시지 전송
273-
text = "텍스트 영역입니다. 최대 200자 표시 가능합니다."
272+
# 1. 나에게 보내기 API - 텍스트 메시지 보내기 예시
273+
message_type = "text" # 메시지 유형 - 텍스트
274+
text = "텍스트 영역입니다. 최대 200자 표시 가능합니다." # 전송할 텍스트 메시지 내용
274275
link = {
275-
"web_url": "https://developers.kakao.com",
276-
"mobile_web_url": "https://developers.kakao.com"
277-
}
278-
button_title = "바로 확인"
276+
"web_url": "https://developers.kakao.com",
277+
"mobile_web_url": "https://developers.kakao.com",
278+
}
279+
button_title = "바로 확인" # 버튼 타이틀
280+
281+
MSG.send_message_to_me(
282+
message_type=message_type,
283+
text=text,
284+
link=link,
285+
button_title=button_title,
286+
)
287+
288+
# 2. 친구에게 보내기 API - 텍스트 메시지 보내기 예시 (친구의 UUID 필요)
289+
message_type = "text" # 메시지 유형 - 텍스트
290+
receiver_uuids = [] # 메시지 수신자 UUID 목록
291+
text = "텍스트 영역입니다. 최대 200자 표시 가능합니다." # 전송할 텍스트 메시지 내용
292+
link = {
293+
"web_url": "https://developers.kakao.com",
294+
"mobile_web_url": "https://developers.kakao.com",
295+
}
296+
button_title = "바로 확인" # 버튼 타이틀
297+
298+
MSG.send_message_to_friend(
299+
message_type=message_type,
300+
receiver_uuids=receiver_uuids,
301+
text=text,
302+
link=link,
303+
button_title=button_title,
304+
)
279305

280-
MSG.send_text(text=text, link={}, button_title=button_title)
281306
```
282307

283308
<br>

test/test-code.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
"outputs": [],
210210
"source": [
211211
"# 카카오 인증코드 발급 URL 접속 후 리다이렉트된 URL\n",
212-
"url = \"https://localhost:5000/?code=q4uqoy53MvJ_ZFZOfoOynbd55rLYecKKfEo9rtTiwk_wXp1Dm3LWlf_bdLd_eVB4eunSmgopcJ8AAAGFGsKe4A\"\n",
212+
"url = \"\"\n",
213213
"# 위 URL로 액세스 토큰 추출\n",
214214
"access_token = API.get_access_token_by_redirected_url(url)"
215215
]

0 commit comments

Comments
 (0)