-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathapp.py
212 lines (187 loc) · 6.19 KB
/
app.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# -*- coding: utf-8 -*-
import os
import sys
from linebot.v3.messaging import (
Configuration,
ApiClient,
MessagingApi,
MessagingApiBlob,
RichMenuRequest,
RichMenuArea,
RichMenuSize,
RichMenuBounds,
URIAction,
RichMenuSwitchAction,
CreateRichMenuAliasRequest
)
channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', None)
if channel_access_token is None:
print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.')
sys.exit(1)
configuration = Configuration(
access_token=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(uri=action.get('uri'))
else:
return RichMenuSwitchAction(
rich_menu_alias_id=action.get('richMenuAliasId'),
data=action.get('data')
)
def main():
with ApiClient(configuration) as api_client:
line_bot_api = MessagingApi(api_client)
line_bot_blob_api = MessagingApiBlob(api_client)
# 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 = RichMenuRequest(
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_request=rich_menu_to_a_create
).rich_menu_id
# 3. Upload image to rich menu A
with open('./public/richmenu-a.png', 'rb') as image:
line_bot_blob_api.set_rich_menu_image(
rich_menu_id=rich_menu_a_id,
body=bytearray(image.read()),
_headers={'Content-Type': 'image/png'}
)
# 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 = RichMenuRequest(
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_request=rich_menu_to_b_create
).rich_menu_id
# 5. Upload image to rich menu B
with open('./public/richmenu-b.png', 'rb') as image:
line_bot_blob_api.set_rich_menu_image(
rich_menu_id=rich_menu_b_id,
body=bytearray(image.read()),
_headers={'Content-Type': 'image/png'}
)
# 6. Set rich menu A as the default rich menu
line_bot_api.set_default_rich_menu(rich_menu_id=rich_menu_a_id)
# 7. Create rich menu alias A
alias_a = CreateRichMenuAliasRequest(
rich_menu_alias_id='richmenu-alias-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 = CreateRichMenuAliasRequest(
rich_menu_alias_id='richmenu-alias-b',
rich_menu_id=rich_menu_b_id
)
line_bot_api.create_rich_menu_alias(alias_b)
print('success')
main()