Spaces:
Sleeping
Sleeping
新增 upload_richmenu.py
Browse files- upload_richmenu.py +70 -0
upload_richmenu.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
# === 替換成你自己的 LINE Channel Access Token ===
|
| 5 |
+
LINE_ACCESS_TOKEN = 'V8aLCQMfD56NjZbLyPVHynCC49rE5gr4cP4PbhuIRp8AS3VbkGKlkYlnTk7XSo/ivFpE/x346fAdFG7c0tj8rLtI5sY5tmLhUY+aV3mysJ2wAndAlDpvh3wJ6FXB1ebdadJxuqXjsEzmJuU6fQszJwdB04t89/1O/w1cDnyilFU='
|
| 6 |
+
|
| 7 |
+
# === 讀取 Rich Menu JSON ===
|
| 8 |
+
richmenu_json = {
|
| 9 |
+
"size": {"width": 2500, "height": 1124},
|
| 10 |
+
"selected": True,
|
| 11 |
+
"name": "健身五功能選單",
|
| 12 |
+
"chatBarText": "打開健身選單",
|
| 13 |
+
"areas": [
|
| 14 |
+
{"bounds": {"x": 0, "y": 0, "width": 1250, "height": 281}, "action": {"type": "message", "text": "菜單 胸肌"}},
|
| 15 |
+
{"bounds": {"x": 1250, "y": 0, "width": 1250, "height": 281}, "action": {"type": "message", "text": "飲食 減脂"}},
|
| 16 |
+
{"bounds": {"x": 0, "y": 281, "width": 1250, "height": 281}, "action": {"type": "message", "text": "紀錄 深蹲 60kg"}},
|
| 17 |
+
{"bounds": {"x": 1250, "y": 281, "width": 1250, "height": 281}, "action": {"type": "message", "text": "補給品推薦"}},
|
| 18 |
+
{"bounds": {"x": 0, "y": 562, "width": 2500, "height": 562}, "action": {"type": "message", "text": "登入"}}
|
| 19 |
+
]
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# === 1. 建立 Rich Menu ===
|
| 23 |
+
headers = {
|
| 24 |
+
'Authorization': f'Bearer {LINE_ACCESS_TOKEN}',
|
| 25 |
+
'Content-Type': 'application/json'
|
| 26 |
+
}
|
| 27 |
+
response = requests.post(
|
| 28 |
+
'https://api.line.me/v2/bot/richmenu',
|
| 29 |
+
headers=headers,
|
| 30 |
+
data=json.dumps(richmenu_json)
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
if response.status_code != 200:
|
| 34 |
+
print("建立 Rich Menu 失敗:", response.text)
|
| 35 |
+
exit()
|
| 36 |
+
|
| 37 |
+
richmenu_id = response.json()['richMenuId']
|
| 38 |
+
print(f"✅ Rich Menu 建立成功,ID:{richmenu_id}")
|
| 39 |
+
|
| 40 |
+
# === 2. 上傳圖片 ===
|
| 41 |
+
with open("介面設置.png", "rb") as f:
|
| 42 |
+
image_headers = {
|
| 43 |
+
'Authorization': f'Bearer {LINE_ACCESS_TOKEN}',
|
| 44 |
+
'Content-Type': 'image/png'
|
| 45 |
+
}
|
| 46 |
+
image_response = requests.post(
|
| 47 |
+
f"https://api.line.me/v2/bot/richmenu/{richmenu_id}/content",
|
| 48 |
+
headers=image_headers,
|
| 49 |
+
data=f.read()
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
if image_response.status_code != 200:
|
| 53 |
+
print("上傳圖片失敗:", image_response.text)
|
| 54 |
+
exit()
|
| 55 |
+
|
| 56 |
+
print("✅ 圖片上傳成功")
|
| 57 |
+
|
| 58 |
+
# === 3. 指定為所有用戶預設選單 ===
|
| 59 |
+
default_headers = {
|
| 60 |
+
'Authorization': f'Bearer {LINE_ACCESS_TOKEN}'
|
| 61 |
+
}
|
| 62 |
+
default_response = requests.post(
|
| 63 |
+
f"https://api.line.me/v2/bot/user/all/richmenu/{richmenu_id}",
|
| 64 |
+
headers=default_headers
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
if default_response.status_code != 200:
|
| 68 |
+
print("設定預設 Rich Menu 失敗:", default_response.text)
|
| 69 |
+
else:
|
| 70 |
+
print("🎉 Rich Menu 已設定為預設選單!")
|