Update app.py
Browse files
app.py
CHANGED
|
@@ -104,72 +104,74 @@ def handle_image(event):
|
|
| 104 |
# --- 第 4 步:將資料塞進橫向圖卡 ---
|
| 105 |
# --- 第 4 步:將資料塞進 Toyota 風格的旗艦版圖卡 ---
|
| 106 |
# --- 第 4 步:將資料塞進 Toyota 風格的旗艦版圖卡 ---
|
|
|
|
| 107 |
bubbles = []
|
| 108 |
for item in data_list:
|
| 109 |
|
| 110 |
-
# 如果
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
#
|
| 114 |
specs_box = {
|
| 115 |
"type": "box",
|
| 116 |
"layout": "vertical",
|
| 117 |
"spacing": "sm",
|
| 118 |
"margin": "lg",
|
| 119 |
"contents": [
|
| 120 |
-
# 第一列:通路
|
| 121 |
{
|
| 122 |
"type": "box",
|
| 123 |
"layout": "horizontal",
|
| 124 |
"contents": [
|
| 125 |
{"type": "text", "text": "販售通路", "size": "md", "color": "#8c8c8c", "flex": 1},
|
| 126 |
-
{"type": "text", "text":
|
| 127 |
]
|
| 128 |
},
|
| 129 |
-
# 第二列:價格
|
| 130 |
{
|
| 131 |
"type": "box",
|
| 132 |
"layout": "horizontal",
|
| 133 |
"contents": [
|
| 134 |
{"type": "text", "text": price_tag, "size": "md", "color": "#8c8c8c", "flex": 1},
|
| 135 |
-
{"type": "text", "text": f"${
|
| 136 |
]
|
| 137 |
}
|
| 138 |
]
|
| 139 |
}
|
| 140 |
|
| 141 |
-
#
|
| 142 |
-
if
|
| 143 |
specs_box["contents"].append(
|
| 144 |
{
|
| 145 |
"type": "box",
|
| 146 |
"layout": "horizontal",
|
| 147 |
"contents": [
|
| 148 |
{"type": "text", "text": "獨家優惠", "size": "md", "color": "#8c8c8c", "flex": 1},
|
| 149 |
-
{"type": "text", "text":
|
| 150 |
]
|
| 151 |
}
|
| 152 |
)
|
| 153 |
|
| 154 |
-
# 組裝最終的
|
| 155 |
card = {
|
| 156 |
"type": "bubble",
|
| 157 |
-
"size": "mega",
|
| 158 |
"hero": {
|
| 159 |
"type": "image",
|
| 160 |
-
"url":
|
| 161 |
"size": "full",
|
| 162 |
-
"aspectRatio": "4:3",
|
| 163 |
-
"aspectMode": "fit",
|
| 164 |
"backgroundColor": "#FFFFFF"
|
| 165 |
},
|
| 166 |
"body": {
|
| 167 |
"type": "box",
|
| 168 |
"layout": "vertical",
|
| 169 |
"contents": [
|
| 170 |
-
|
| 171 |
-
{"type": "text", "text": item["name"], "weight": "bold", "size": "xl", "wrap": True},
|
| 172 |
-
# 塞入剛剛做好的規格表
|
| 173 |
specs_box
|
| 174 |
]
|
| 175 |
},
|
|
@@ -177,16 +179,15 @@ def handle_image(event):
|
|
| 177 |
"type": "box",
|
| 178 |
"layout": "vertical",
|
| 179 |
"contents": [
|
| 180 |
-
# 底部黑底白字的大按鈕
|
| 181 |
{
|
| 182 |
"type": "button",
|
| 183 |
"style": "primary",
|
| 184 |
-
"color": "#000000",
|
| 185 |
"height": "md",
|
| 186 |
"action": {
|
| 187 |
"type": "uri",
|
| 188 |
"label": "前往商家購買",
|
| 189 |
-
"uri":
|
| 190 |
}
|
| 191 |
}
|
| 192 |
]
|
|
@@ -194,7 +195,6 @@ def handle_image(event):
|
|
| 194 |
}
|
| 195 |
bubbles.append(card)
|
| 196 |
|
| 197 |
-
|
| 198 |
# --- 第 5 步:打包發送圖卡 ---
|
| 199 |
flex_message = FlexSendMessage.new_from_json_dict({
|
| 200 |
"type": "flex",
|
|
@@ -206,6 +206,7 @@ def handle_image(event):
|
|
| 206 |
})
|
| 207 |
line_bot_api.push_message(user_id, flex_message)
|
| 208 |
del user_states[user_id]
|
|
|
|
| 209 |
except Exception as e:
|
| 210 |
line_bot_api.push_message(
|
| 211 |
user_id,
|
|
@@ -213,8 +214,6 @@ def handle_image(event):
|
|
| 213 |
)
|
| 214 |
|
| 215 |
|
| 216 |
-
|
| 217 |
-
|
| 218 |
# ================= 3. 多輪對話核心邏輯 =================
|
| 219 |
@handler.add(MessageEvent, message=TextMessage)
|
| 220 |
def handle_message(event):
|
|
|
|
| 104 |
# --- 第 4 步:將資料塞進橫向圖卡 ---
|
| 105 |
# --- 第 4 步:將資料塞進 Toyota 風格的旗艦版圖卡 ---
|
| 106 |
# --- 第 4 步:將資料塞進 Toyota 風格的旗艦版圖卡 ---
|
| 107 |
+
# --- 第 4 步:將資料塞進 Toyota 風格的旗艦版圖卡 (終極防彈版) ---
|
| 108 |
bubbles = []
|
| 109 |
for item in data_list:
|
| 110 |
|
| 111 |
+
# 🛡️ 第一道防線:先把所有資料拿出來,如果 AI 漏給,就塞預設值!
|
| 112 |
+
safe_store = item.get("store", "推薦藥妝店")
|
| 113 |
+
safe_name = item.get("name", "未知名稱商品")
|
| 114 |
+
safe_price = item.get("price", "請洽門市")
|
| 115 |
+
safe_image = item.get("image", "https://via.placeholder.com/600x400/eeeeee/999999?text=Image+Not+Found")
|
| 116 |
+
safe_discount = item.get("discount", "")
|
| 117 |
+
safe_link = item.get("link", f"https://www.google.com/search?q={safe_store}+{safe_name}")
|
| 118 |
|
| 119 |
+
# 🛡️ 第二道防線:畫圖卡時,『絕對不用』中括號,全部使用上面的 safe_ 變數!
|
| 120 |
specs_box = {
|
| 121 |
"type": "box",
|
| 122 |
"layout": "vertical",
|
| 123 |
"spacing": "sm",
|
| 124 |
"margin": "lg",
|
| 125 |
"contents": [
|
|
|
|
| 126 |
{
|
| 127 |
"type": "box",
|
| 128 |
"layout": "horizontal",
|
| 129 |
"contents": [
|
| 130 |
{"type": "text", "text": "販售通路", "size": "md", "color": "#8c8c8c", "flex": 1},
|
| 131 |
+
{"type": "text", "text": safe_store, "size": "md", "color": "#111111", "weight": "bold", "flex": 2}
|
| 132 |
]
|
| 133 |
},
|
|
|
|
| 134 |
{
|
| 135 |
"type": "box",
|
| 136 |
"layout": "horizontal",
|
| 137 |
"contents": [
|
| 138 |
{"type": "text", "text": price_tag, "size": "md", "color": "#8c8c8c", "flex": 1},
|
| 139 |
+
{"type": "text", "text": f"${safe_price}", "size": "md", "color": "#111111", "weight": "bold", "flex": 2}
|
| 140 |
]
|
| 141 |
}
|
| 142 |
]
|
| 143 |
}
|
| 144 |
|
| 145 |
+
# 處理折扣標籤
|
| 146 |
+
if safe_discount:
|
| 147 |
specs_box["contents"].append(
|
| 148 |
{
|
| 149 |
"type": "box",
|
| 150 |
"layout": "horizontal",
|
| 151 |
"contents": [
|
| 152 |
{"type": "text", "text": "獨家優惠", "size": "md", "color": "#8c8c8c", "flex": 1},
|
| 153 |
+
{"type": "text", "text": safe_discount, "size": "md", "color": "#ff3b30", "weight": "bold", "flex": 2}
|
| 154 |
]
|
| 155 |
}
|
| 156 |
)
|
| 157 |
|
| 158 |
+
# 組裝最終的卡片
|
| 159 |
card = {
|
| 160 |
"type": "bubble",
|
| 161 |
+
"size": "mega",
|
| 162 |
"hero": {
|
| 163 |
"type": "image",
|
| 164 |
+
"url": safe_image,
|
| 165 |
"size": "full",
|
| 166 |
+
"aspectRatio": "4:3",
|
| 167 |
+
"aspectMode": "fit",
|
| 168 |
"backgroundColor": "#FFFFFF"
|
| 169 |
},
|
| 170 |
"body": {
|
| 171 |
"type": "box",
|
| 172 |
"layout": "vertical",
|
| 173 |
"contents": [
|
| 174 |
+
{"type": "text", "text": safe_name, "weight": "bold", "size": "xl", "wrap": True},
|
|
|
|
|
|
|
| 175 |
specs_box
|
| 176 |
]
|
| 177 |
},
|
|
|
|
| 179 |
"type": "box",
|
| 180 |
"layout": "vertical",
|
| 181 |
"contents": [
|
|
|
|
| 182 |
{
|
| 183 |
"type": "button",
|
| 184 |
"style": "primary",
|
| 185 |
+
"color": "#000000",
|
| 186 |
"height": "md",
|
| 187 |
"action": {
|
| 188 |
"type": "uri",
|
| 189 |
"label": "前往商家購買",
|
| 190 |
+
"uri": safe_link
|
| 191 |
}
|
| 192 |
}
|
| 193 |
]
|
|
|
|
| 195 |
}
|
| 196 |
bubbles.append(card)
|
| 197 |
|
|
|
|
| 198 |
# --- 第 5 步:打包發送圖卡 ---
|
| 199 |
flex_message = FlexSendMessage.new_from_json_dict({
|
| 200 |
"type": "flex",
|
|
|
|
| 206 |
})
|
| 207 |
line_bot_api.push_message(user_id, flex_message)
|
| 208 |
del user_states[user_id]
|
| 209 |
+
|
| 210 |
except Exception as e:
|
| 211 |
line_bot_api.push_message(
|
| 212 |
user_id,
|
|
|
|
| 214 |
)
|
| 215 |
|
| 216 |
|
|
|
|
|
|
|
| 217 |
# ================= 3. 多輪對話核心邏輯 =================
|
| 218 |
@handler.add(MessageEvent, message=TextMessage)
|
| 219 |
def handle_message(event):
|