-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrichmenu.py
93 lines (72 loc) · 3.13 KB
/
richmenu.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
## 只須設定圖片格式及檔名 (搜尋!)
## ========== 設定line_bot ==========
import os
CHANNEL_ACCESS_TOKEN = os.environ["LINE_CHANNEL_ACCESS_TOKEN"]
CHANNEL_SECRET = os.environ["LINE_CHANNEL_SECRET"]
## ========== 新增 Rich menus,並透過line API 取得 rich_menu_link ==========
## (! 配合圖片格式修改參數)
import requests, json
headers = {"Authorization":"Bearer "+CHANNEL_ACCESS_TOKEN,"Content-Type":"application/json"}
body = {
"size": {"width": 2500, "height": 599},
"selected": "true",
"name": "圖文選單richmenu",
"chatBarText": "⇤左側開⌨🎤 點這開選單",
"areas":[
{
"bounds": {"x": 0, "y": 0, "width": 625, "height": 635},
"action": {"type": "uri", "uri": "https://line.me/R/nv/camera/"}
},
{
"bounds": {"x": 500, "y": 0, "width": 625, "height": 635},
"action": {"type": "uri", "uri": "https://line.me/R/nv/cameraRoll/multi"},
},
# TODO: 先傳文字開啟收藏的食譜,之後改用Postback試試看藏網址直接開網頁
{
"bounds": {"x": 1000, "y": 0, "width": 625, "height": 635},
"action": {"type": "message", "text": "收藏的食譜"},
},
{
"bounds": {"x": 1500, "y": 0, "width": 625, "height": 635},
"action": {"type": "uri", "uri": "https://line.me/R/nv/recommendOA/@096oeofl"}
},
{
"bounds": {"x": 2000, "y": 0, "width": 625, "height": 635},
"action": {"type": "uri", "uri": "https://liff.line.me/1656700369-VlnBxlo4"}
}
]
}
req = requests.request('POST', 'https://api.line.me/v2/bot/richmenu',
headers=headers, data=json.dumps(body).encode('utf-8'))
richmenuId = json.loads(req.text).get("richMenuId")
print(richmenuId)
# ========== 在line_bot上設定 Rich menus 的圖片,透過 line-bot-sdk-python 來將圖片掛上該圖文選單 ==========
from linebot import (LineBotApi)
line_bot_api = LineBotApi(CHANNEL_ACCESS_TOKEN)
## (! 配合圖片修改檔名"richmenu.jpg"及圖片形式"image/jpeg")
with open("pic/richmenu_5.jpg",'rb') as f:
line_bot_api.set_rich_menu_image(richmenuId, "image/jpeg", f)
## ========== 啟用 Rich menus,透過發送 request ==========
import requests
headers = {"Authorization":"Bearer "+CHANNEL_ACCESS_TOKEN,"Content-Type":"application/json"}
req = requests.request('POST', 'https://api.line.me/v2/bot/user/all/richmenu/'+richmenuId,
headers=headers)
# print(req.text)
# # ========== 查看所有 Rich menus(最多100組) ==========
#
# from linebot import (LineBotApi, WebhookHandler)
#
# line_bot_api = LineBotApi(CHANNEL_ACCESS_TOKEN)
#
# rich_menu_list = line_bot_api.get_rich_menu_list()
#
# for rich_menu in rich_menu_list:
# print(rich_menu.rich_menu_id)
# # ========== 刪除Rich menus==========
# from linebot import (LineBotApi, WebhookHandler)
# line_bot_api = LineBotApi(CHANNEL_ACCESS_TOKEN)
# line_bot_api.delete_rich_menu('richmenu-6479255f4a62971310f0dec0fb352c54')
#
# rich_menu_list = line_bot_api.get_rich_menu_list()
# for rich_menu in rich_menu_list:
# print(rich_menu.rich_menu_id)