-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.py
229 lines (208 loc) · 7.36 KB
/
view.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from linebot.models import *
import requests
import json
def location():
"""第一句,請使用者傳送地點"""
message = TextSendMessage(text="你好,我是美食機器人,請告訴我你的所在位置,我將會為您推薦好吃的食物!",
quick_reply=QuickReply(items=[
QuickReplyButton(action=LocationAction(label="查詢目前所在位置"))])
)
return message
def transport():
"""第二句,請使用者選擇他使用的交通工具"""
message = TemplateSendMessage(
alt_text='交通運具選擇方式',
template=ButtonsTemplate(
title='交通工具',
text='請問您使用的交通運具種類為何呢?\n(如需重新選擇請輸入「交通方式」)',
actions=[
MessageTemplateAction(
label='步行',
text='步行',
# data=""
),
MessageTemplateAction(
label='單車',
text='單車',
# data=''
),
MessageTemplateAction(
label='機車',
text='機車',
# data=''
),
MessageTemplateAction(
label='汽車',
text='汽車',
# data=''
)
]
)
)
return message
def open_q():
"""開放式問答"""
message = TextSendMessage(text="好的,請問你想吃甚麼呢?")
return message
def google_needed():
"""資料庫查無結果的時候使用"""
message = TemplateSendMessage(
alt_text='資料庫尚無匹配到相關店家,是否需要透過google map幫您搜尋?',
template=ButtonsTemplate(
title='資料庫無資料',
text='資料庫尚無匹配到相關店家,是否需要透過google map幫您搜尋相關結果?',
actions=[
MessageTemplateAction(
label='是',
text='好的',
# data=""
),
MessageTemplateAction(
label='否',
text='不需要',
# data=''
)
]
)
)
return message
def random_recommendation():
"""食物主題,負面情緒時使用"""
message = TemplateSendMessage(
alt_text='隨機為您推薦店家',
template=ButtonsTemplate(
title='隨機推薦',
text='喜歡我們隨機推薦你的美食嗎?',
actions=[
MessageTemplateAction(
label='是',
text='喜歡',
# data=""
),
MessageTemplateAction(
label='否',
text='不喜歡,再來一個',
# data=''
)
]
)
)
return message
def recommendation_pattern(POI_name, POI_rating, POI_address, POI_open, POI_money, POI_url):
"""推薦食品的預設格式"""
message = TextSendMessage(
text=f"""===== 以下為您推薦 =====\n \
📌 {POI_name}\n \
⭐ 評分 | {POI_rating}\n \
🏠 地址 | {POI_address}\n \
🈺 營業狀況 | {POI_open} \n \
💲 價錢區間 | {POI_money} \n \
📢 點我查看更多 \n{POI_url}""")
return message
def google_api(lat, lng, r, keyword):
"""最後一句,如果在資料庫裏面找不到相對應的品項,就呼叫google places api做搜尋"""
API = "AIzaSyBXioCZJv01QRXxBedIqkjSeC2ilJY3TVU"
url = f"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={lat}%2C{lng}&radius={r}&type=restaurant&keyword={keyword}&key={API}"
payload = {}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
response = response.json()
max = 0
place_id = ""
try:
for i in response['results']:
if(float(i['rating']) > max):
max = float(i['rating'])
place_id = i['place_id']
POI_name = i['name']
POI_url, POI_address = __get_poi_url(place_id, API=API)
try:
POI_rating = i['rating']
except:
POI_rating = "無資料"
try:
POI_open = i['opening_hours']['open_now']
except:
POI_open = "無資料"
try:
POI_money = i['price_level']
except:
POI_money = "無資料"
except:
return -1
else:
# POI_open data convert
if POI_open == True:
POI_open = '營業中'
elif POI_open == "無資料":
POI_open = '無資料'
else:
POI_open = '休息中'
# POI_money data convert
if POI_money == 0:
POI_money = "免費"
elif POI_money == 1:
POI_money = "低"
elif POI_money == 2:
POI_money = "中"
elif POI_money == 3:
POI_money = "高"
elif POI_money == 4:
POI_money = '超高'
print(POI_name, POI_rating, POI_address, POI_open, POI_money, POI_url)
return (POI_name, POI_rating, POI_address, POI_open, POI_money, POI_url)
# 取得前述餐廳中評分最高的google map url
def __get_poi_url(place_id, API):
"""透過google places api搜尋店家google map網址"""
url = f"https://maps.googleapis.com/maps/api/place/details/json?place_id={place_id}&fields=url%2Cformatted_address&key={API}"
payload = {}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
response = response.json()
POI_url = response['result']['url']
POI_address = response['result']['formatted_address']
return POI_url, POI_address
def get_poi_detail(place_id):
API = "AIzaSyBXioCZJv01QRXxBedIqkjSeC2ilJY3TVU"
url = f"https://maps.googleapis.com/maps/api/place/details/json?place_id={place_id}&key={API}"
payload = {}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
response = response.json()
POI_name = response['result']['name']
POI_address = response['result']['formatted_address']
POI_url = response['result']['url']
try:
POI_rating = response['result']['rating']
except:
POI_rating = "無資料"
try:
POI_open = response['opening_hours']['open_now']
except:
POI_open = "無資料"
try:
POI_money = response['price_level']
except:
POI_money = "無資料"
# POI_open data convert
if POI_open == True:
POI_open = '營業中'
elif POI_open == "無資料":
POI_open = '無資料'
else:
POI_open = '休息中'
# POI_money data convert
if POI_money == 0:
POI_money = "免費"
elif POI_money == 1:
POI_money = "低"
elif POI_money == 2:
POI_money = "中"
elif POI_money == 3:
POI_money = "高"
elif POI_money == 4:
POI_money = '超高'
return (POI_name, POI_rating, POI_address, POI_open, POI_money, POI_url)
if(__name__ == "__main__"):
data = google_api(23.00071, 120.215205, 1500, '咖哩')
print(data)