mylinebotnew / upload_richmenu.py
wsy07's picture
新增 upload_richmenu.py
7b43f6b
import requests
import json
# === 替換成你自己的 LINE Channel Access Token ===
LINE_ACCESS_TOKEN = 'V8aLCQMfD56NjZbLyPVHynCC49rE5gr4cP4PbhuIRp8AS3VbkGKlkYlnTk7XSo/ivFpE/x346fAdFG7c0tj8rLtI5sY5tmLhUY+aV3mysJ2wAndAlDpvh3wJ6FXB1ebdadJxuqXjsEzmJuU6fQszJwdB04t89/1O/w1cDnyilFU='
# === 讀取 Rich Menu JSON ===
richmenu_json = {
"size": {"width": 2500, "height": 1124},
"selected": True,
"name": "健身五功能選單",
"chatBarText": "打開健身選單",
"areas": [
{"bounds": {"x": 0, "y": 0, "width": 1250, "height": 281}, "action": {"type": "message", "text": "菜單 胸肌"}},
{"bounds": {"x": 1250, "y": 0, "width": 1250, "height": 281}, "action": {"type": "message", "text": "飲食 減脂"}},
{"bounds": {"x": 0, "y": 281, "width": 1250, "height": 281}, "action": {"type": "message", "text": "紀錄 深蹲 60kg"}},
{"bounds": {"x": 1250, "y": 281, "width": 1250, "height": 281}, "action": {"type": "message", "text": "補給品推薦"}},
{"bounds": {"x": 0, "y": 562, "width": 2500, "height": 562}, "action": {"type": "message", "text": "登入"}}
]
}
# === 1. 建立 Rich Menu ===
headers = {
'Authorization': f'Bearer {LINE_ACCESS_TOKEN}',
'Content-Type': 'application/json'
}
response = requests.post(
'https://api.line.me/v2/bot/richmenu',
headers=headers,
data=json.dumps(richmenu_json)
)
if response.status_code != 200:
print("建立 Rich Menu 失敗:", response.text)
exit()
richmenu_id = response.json()['richMenuId']
print(f"✅ Rich Menu 建立成功,ID:{richmenu_id}")
# === 2. 上傳圖片 ===
with open("介面設置.png", "rb") as f:
image_headers = {
'Authorization': f'Bearer {LINE_ACCESS_TOKEN}',
'Content-Type': 'image/png'
}
image_response = requests.post(
f"https://api.line.me/v2/bot/richmenu/{richmenu_id}/content",
headers=image_headers,
data=f.read()
)
if image_response.status_code != 200:
print("上傳圖片失敗:", image_response.text)
exit()
print("✅ 圖片上傳成功")
# === 3. 指定為所有用戶預設選單 ===
default_headers = {
'Authorization': f'Bearer {LINE_ACCESS_TOKEN}'
}
default_response = requests.post(
f"https://api.line.me/v2/bot/user/all/richmenu/{richmenu_id}",
headers=default_headers
)
if default_response.status_code != 200:
print("設定預設 Rich Menu 失敗:", default_response.text)
else:
print("🎉 Rich Menu 已設定為預設選單!")