wlstp8473

search_restaurant1 with telegram

...@@ -5,6 +5,10 @@ import requests ...@@ -5,6 +5,10 @@ import requests
5 from pprint import pprint 5 from pprint import pprint
6 from io import StringIO #return_print 하기 위해 6 from io import StringIO #return_print 하기 위해
7 7
8 +import telegram
9 +from telegram.ext import Updater
10 +from telegram.ext import MessageHandler, Filters
11 +
8 class KakaoLocalAPI: 12 class KakaoLocalAPI:
9 """ 13 """
10 Kakao Local API 컨트롤러 14 Kakao Local API 컨트롤러
...@@ -65,23 +69,15 @@ x = 126.98083352703802 #내 위치 위도와 경도 ...@@ -65,23 +69,15 @@ x = 126.98083352703802 #내 위치 위도와 경도
65 y = 37.39403042340368 69 y = 37.39403042340368
66 radius = 1 #반경 km 검색 70 radius = 1 #반경 km 검색
67 71
68 -
69 ## Request 72 ## Request
70 result_5 = kakao.search_keyword(query,x,y,radius) 73 result_5 = kakao.search_keyword(query,x,y,radius)
71 74
72 75
73 -
74 -
75 -
76 -
77 -
78 # + json파일 확인해보기 이후 데이터를 추출해야함 76 # + json파일 확인해보기 이후 데이터를 추출해야함
79 #json_string = json.dumps(result_5, indent=2) 77 #json_string = json.dumps(result_5, indent=2)
80 #print(json_string) 78 #print(json_string)
81 79
82 80
83 -
84 -
85 #json파일 특정 키워드 추출 81 #json파일 특정 키워드 추출
86 #추출할 리스트 place_name, place_url, x, y (x y는 위치임), 검색키워드, 선택 지역(위치) 82 #추출할 리스트 place_name, place_url, x, y (x y는 위치임), 검색키워드, 선택 지역(위치)
87 #from pprint import pprint 83 #from pprint import pprint
...@@ -110,6 +106,7 @@ phone_list = [ x_['phone'] for x_ in result_5['documents']] ...@@ -110,6 +106,7 @@ phone_list = [ x_['phone'] for x_ in result_5['documents']]
110 #print('------') 106 #print('------')
111 107
112 place_url_list = [ x_['place_url'] for x_ in result_5['documents']] 108 place_url_list = [ x_['place_url'] for x_ in result_5['documents']]
109 +
113 #print(place_url_list) 110 #print(place_url_list)
114 111
115 #print('------') 112 #print('------')
...@@ -117,21 +114,24 @@ place_url_list = [ x_['place_url'] for x_ in result_5['documents']] ...@@ -117,21 +114,24 @@ place_url_list = [ x_['place_url'] for x_ in result_5['documents']]
117 #print(x_list) 위치 시각화 할때 사용 114 #print(x_list) 위치 시각화 할때 사용
118 x_list = [ x_['x'] for x_ in result_5['documents']] 115 x_list = [ x_['x'] for x_ in result_5['documents']]
119 y_list = [ x_['y'] for x_ in result_5['documents']] 116 y_list = [ x_['y'] for x_ in result_5['documents']]
117 +
120 #print(y_list) 118 #print(y_list)
121 #print(x_list) 119 #print(x_list)
122 120
123 #print('------') 121 #print('------')
124 122
125 road_address_name_list = [ x_['road_address_name'] for x_ in result_5['documents']] 123 road_address_name_list = [ x_['road_address_name'] for x_ in result_5['documents']]
124 +
125 +
126 #print(road_address_name_list) 126 #print(road_address_name_list)
127 127
128 #print('------') 128 #print('------')
129 129
130 -
131 resturant_info_list = [] 130 resturant_info_list = []
132 total_count_num = str(total_count_list[0]) 131 total_count_num = str(total_count_list[0])
133 total_count_num = int(total_count_num) 132 total_count_num = int(total_count_num)
134 -resturant_info = "[ 맛집 정보 ]" 133 +resturant_info = ""
134 +resturant_string = "관련 음식점 정보: " + '\n'
135 135
136 #프린트 결과를 리턴받기 위한 함수 136 #프린트 결과를 리턴받기 위한 함수
137 #from io import StringIO 137 #from io import StringIO
...@@ -140,14 +140,52 @@ def return_print(*message): ...@@ -140,14 +140,52 @@ def return_print(*message):
140 print(*message, file=io, end="") 140 print(*message, file=io, end="")
141 return io.getvalue() 141 return io.getvalue()
142 142
143 -print(resturant_info) # 봇 메세지 ㄱㄱ 143 +for i in range(0,10): #total_count_num가 너무 크면 인덱스 범위가 벗어나서 실행이 안됨, 임시로 10개까지만
144 + resturant_info = [place_name_list[i] + " " +phone_list[i] + " " + road_address_name_list[i] + " " + place_url_list[i]]
145 + resturant_string += str(resturant_info) + "\n" + "\n"
146 +
147 +
144 148
145 -for i in range(0,10): #total_count_num가 너무 크면 인덱스 범위가 벗어나서 실행이 안됨, 임시로 5개까지만
146 - resturant_info_print = print([place_name_list[i] + " " +phone_list[i] + " " + road_address_name_list[i] + " " + place_url_list[i]])
147 #resturant_info_list = [place_name_list[i] + " " +phone_list[i] + " " + road_address_name_list[i] + " " + place_url_list[i]] 149 #resturant_info_list = [place_name_list[i] + " " +phone_list[i] + " " + road_address_name_list[i] + " " + place_url_list[i]]
148 #resturant_info += return_print(resturant_info_list) 150 #resturant_info += return_print(resturant_info_list)
151 +# print(resturant_string)
152 +
153 +
154 +
155 +
156 +
157 +
158 +#################################################################3
159 +#텔레그램 관련 코드
160 +token = "1721885449:AAHDGMbjSJfhXxML6nfSCpfiU7SghpL_vOE"
161 +id = "1657858421"
162 +bot = telegram.Bot(token)
163 +
164 +
165 +info_message = '''맛집 검색 기능: (#맛집)''' ########기능 리스트 ##############33
166 +bot.sendMessage(chat_id=id, text=info_message)
167 +
168 +react_weather = '''오늘 날씨 봐봐'''
169 +
170 +
171 +updater = Updater(token=token, use_context=True)
172 +dispatcher = updater.dispatcher
173 +updater.start_polling()
174 +
149 175
176 +# 챗봇 응답
177 +def handler(update, context):
178 + user_text = update.message.text # 사용자가 보낸 메세지를 user_text 변수에 저장합니다.
150 179
180 + # 오늘 확진자 수 답장
181 + if ("맛집" in user_text):
182 + bot.sendMessage(chat_id=id, text=resturant_string)
183 + elif ("고마워" in user_text):
184 + bot.send_message(chat_id=id, text="웅 ㅎㅎ")
185 + else:
186 + update.message.reply_text("ㅎㅎ")
151 187
188 +echo_handler = MessageHandler(Filters.text, handler)
189 +dispatcher.add_handler(echo_handler)
152 190
153 191
......