-
Notifications
You must be signed in to change notification settings - Fork 0
/
RichMenu.py
189 lines (167 loc) · 5.32 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
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
import os
import sys
from linebot import (
LineBotApi,
)
from linebot.models import (
RichMenu,
RichMenuArea,
RichMenuSize,
RichMenuBounds,
URIAction
)
from linebot.models.actions import RichMenuSwitchAction
from linebot.models.rich_menu import RichMenuAlias
from config import *
channel_access_token = CHANNEL_ACCESS_TOKEN
if channel_access_token is None:
print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.')
sys.exit(1)
line_bot_api = LineBotApi(channel_access_token)
def rich_menu_object_a_json():
return {
"size": {
"width": 2500,
"height": 1686
},
"selected": False,
"name": "richmenu-a",
"chatBarText": "Tap to open",
"areas": [
{
"bounds": {
"x": 0,
"y": 0,
"width": 1250,
"height": 1686
},
"action": {
"type": "uri",
"uri": "https://www.line-community.me/"
}
},
{
"bounds": {
"x": 1251,
"y": 0,
"width": 1250,
"height": 1686
},
"action": {
"type": "richmenuswitch",
"richMenuAliasId": "richmenu-alias-b",
"data": "richmenu-changed-to-b"
}
}
]
}
def rich_menu_object_b_json():
return {
"size": {
"width": 2500,
"height": 1686
},
"selected": False,
"name": "richmenu-b",
"chatBarText": "Tap to open",
"areas": [
{
"bounds": {
"x": 0,
"y": 0,
"width": 1250,
"height": 1686
},
"action": {
"type": "richmenuswitch",
"richMenuAliasId": "richmenu-alias-a",
"data": "richmenu-changed-to-a"
}
},
{
"bounds": {
"x": 1251,
"y": 0,
"width": 1250,
"height": 1686
},
"action": {
"type": "uri",
"uri": "https://www.line-community.me/"
}
}
]
}
def create_action(action):
if action['type'] == 'uri':
return URIAction(type=action['type'], uri=action.get('uri'))
else:
return RichMenuSwitchAction(
type=action['type'],
rich_menu_alias_id=action.get('richMenuAliasId'),
data=action.get('data')
)
def richmenu_start():
# 2. Create rich menu A (richmenu-a)
rich_menu_object_a = rich_menu_object_a_json()
areas = [
RichMenuArea(
bounds=RichMenuBounds(
x=info['bounds']['x'],
y=info['bounds']['y'],
width=info['bounds']['width'],
height=info['bounds']['height']
),
action=create_action(info['action'])
) for info in rich_menu_object_a['areas']
]
rich_menu_to_a_create = RichMenu(
size=RichMenuSize(width=rich_menu_object_a['size']['width'], height=rich_menu_object_a['size']['height']),
selected=rich_menu_object_a['selected'],
name=rich_menu_object_a['name'],
chat_bar_text=rich_menu_object_a['name'],
areas=areas
)
rich_menu_a_id = line_bot_api.create_rich_menu(rich_menu=rich_menu_to_a_create)
# 3. Upload image to rich menu A
with open('./public/richmenu-a.png', 'rb') as f:
line_bot_api.set_rich_menu_image(rich_menu_a_id, 'image/png', f)
# 4. Create rich menu B (richmenu-b)
rich_menu_object_b = rich_menu_object_b_json()
areas = [
RichMenuArea(
bounds=RichMenuBounds(
x=info['bounds']['x'],
y=info['bounds']['y'],
width=info['bounds']['width'],
height=info['bounds']['height']
),
action=create_action(info['action'])
) for info in rich_menu_object_b['areas']
]
rich_menu_to_b_create = RichMenu(
size=RichMenuSize(width=rich_menu_object_b['size']['width'], height=rich_menu_object_b['size']['height']),
selected=rich_menu_object_b['selected'],
name=rich_menu_object_b['name'],
chat_bar_text=rich_menu_object_b['name'],
areas=areas
)
rich_menu_b_id = line_bot_api.create_rich_menu(rich_menu=rich_menu_to_b_create)
# 5. Upload image to rich menu B
with open('./public/richmenu-b.png', 'rb') as f:
line_bot_api.set_rich_menu_image(rich_menu_b_id, 'image/png', f)
# 6. Set rich menu A as the default rich menu
line_bot_api.set_default_rich_menu(rich_menu_a_id)
# 7. Create rich menu alias A
alias_a = RichMenuAlias(
rich_menu_alias_id='a',
rich_menu_id=rich_menu_a_id
)
line_bot_api.create_rich_menu_alias(alias_a)
# 8. Create rich menu alias B
alias_b = RichMenuAlias(
rich_menu_alias_id='b',
rich_menu_id=rich_menu_b_id
)
line_bot_api.create_rich_menu_alias(alias_b)
print('RichMenu Started!')