|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +from linebot import ( |
| 7 | + LineBotApi, |
| 8 | +) |
| 9 | + |
| 10 | +from linebot.models import ( |
| 11 | + RichMenu, |
| 12 | + RichMenuArea, |
| 13 | + RichMenuSize, |
| 14 | + RichMenuBounds, |
| 15 | + URIAction |
| 16 | +) |
| 17 | +from linebot.models.actions import RichMenuSwitchAction |
| 18 | +from linebot.models.rich_menu import RichMenuAlias |
| 19 | + |
| 20 | +channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', None) |
| 21 | +if channel_access_token is None: |
| 22 | + print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.') |
| 23 | + sys.exit(1) |
| 24 | + |
| 25 | +line_bot_api = LineBotApi(channel_access_token) |
| 26 | + |
| 27 | + |
| 28 | +def rich_menu_object_a_json(): |
| 29 | + return { |
| 30 | + "size": { |
| 31 | + "width": 2500, |
| 32 | + "height": 1686 |
| 33 | + }, |
| 34 | + "selected": False, |
| 35 | + "name": "richmenu-a", |
| 36 | + "chatBarText": "Tap to open", |
| 37 | + "areas": [ |
| 38 | + { |
| 39 | + "bounds": { |
| 40 | + "x": 0, |
| 41 | + "y": 0, |
| 42 | + "width": 1250, |
| 43 | + "height": 1686 |
| 44 | + }, |
| 45 | + "action": { |
| 46 | + "type": "uri", |
| 47 | + "uri": "https://www.line-community.me/" |
| 48 | + } |
| 49 | + }, |
| 50 | + { |
| 51 | + "bounds": { |
| 52 | + "x": 1251, |
| 53 | + "y": 0, |
| 54 | + "width": 1250, |
| 55 | + "height": 1686 |
| 56 | + }, |
| 57 | + "action": { |
| 58 | + "type": "richmenuswitch", |
| 59 | + "richMenuAliasId": "richmenu-alias-b", |
| 60 | + "data": "richmenu-changed-to-b" |
| 61 | + } |
| 62 | + } |
| 63 | + ] |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +def rich_menu_object_b_json(): |
| 68 | + return { |
| 69 | + "size": { |
| 70 | + "width": 2500, |
| 71 | + "height": 1686 |
| 72 | + }, |
| 73 | + "selected": False, |
| 74 | + "name": "richmenu-b", |
| 75 | + "chatBarText": "Tap to open", |
| 76 | + "areas": [ |
| 77 | + { |
| 78 | + "bounds": { |
| 79 | + "x": 0, |
| 80 | + "y": 0, |
| 81 | + "width": 1250, |
| 82 | + "height": 1686 |
| 83 | + }, |
| 84 | + "action": { |
| 85 | + "type": "richmenuswitch", |
| 86 | + "richMenuAliasId": "richmenu-alias-a", |
| 87 | + "data": "richmenu-changed-to-a" |
| 88 | + } |
| 89 | + }, |
| 90 | + { |
| 91 | + "bounds": { |
| 92 | + "x": 1251, |
| 93 | + "y": 0, |
| 94 | + "width": 1250, |
| 95 | + "height": 1686 |
| 96 | + }, |
| 97 | + "action": { |
| 98 | + "type": "uri", |
| 99 | + "uri": "https://www.line-community.me/" |
| 100 | + } |
| 101 | + } |
| 102 | + ] |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | +def create_action(action): |
| 107 | + if action['type'] == 'uri': |
| 108 | + return URIAction(type=action['type'], uri=action.get('uri')) |
| 109 | + else: |
| 110 | + return RichMenuSwitchAction( |
| 111 | + type=action['type'], |
| 112 | + rich_menu_alias_id=action.get('richMenuAliasId'), |
| 113 | + data=action.get('data') |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +def main(): |
| 118 | + # 2. Create rich menu A (richmenu-a) |
| 119 | + rich_menu_object_a = rich_menu_object_a_json() |
| 120 | + areas = [ |
| 121 | + RichMenuArea( |
| 122 | + bounds=RichMenuBounds( |
| 123 | + x=info['bounds']['x'], |
| 124 | + y=info['bounds']['y'], |
| 125 | + width=info['bounds']['width'], |
| 126 | + height=info['bounds']['height'] |
| 127 | + ), |
| 128 | + action=create_action(info['action']) |
| 129 | + ) for info in rich_menu_object_a['areas'] |
| 130 | + ] |
| 131 | + |
| 132 | + rich_menu_to_a_create = RichMenu( |
| 133 | + size=RichMenuSize(width=rich_menu_object_a['size']['width'], height=rich_menu_object_a['size']['height']), |
| 134 | + selected=rich_menu_object_a['selected'], |
| 135 | + name=rich_menu_object_a['name'], |
| 136 | + chat_bar_text=rich_menu_object_a['name'], |
| 137 | + areas=areas |
| 138 | + ) |
| 139 | + |
| 140 | + rich_menu_a_id = line_bot_api.create_rich_menu(rich_menu=rich_menu_to_a_create) |
| 141 | + |
| 142 | + # 3. Upload image to rich menu A |
| 143 | + with open('./public/richmenu-a.png', 'rb') as f: |
| 144 | + line_bot_api.set_rich_menu_image(rich_menu_a_id, 'image/png', f) |
| 145 | + |
| 146 | + # 4. Create rich menu B (richmenu-b) |
| 147 | + rich_menu_object_b = rich_menu_object_b_json() |
| 148 | + areas = [ |
| 149 | + RichMenuArea( |
| 150 | + bounds=RichMenuBounds( |
| 151 | + x=info['bounds']['x'], |
| 152 | + y=info['bounds']['y'], |
| 153 | + width=info['bounds']['width'], |
| 154 | + height=info['bounds']['height'] |
| 155 | + ), |
| 156 | + action=create_action(info['action']) |
| 157 | + ) for info in rich_menu_object_b['areas'] |
| 158 | + ] |
| 159 | + |
| 160 | + rich_menu_to_b_create = RichMenu( |
| 161 | + size=RichMenuSize(width=rich_menu_object_b['size']['width'], height=rich_menu_object_b['size']['height']), |
| 162 | + selected=rich_menu_object_b['selected'], |
| 163 | + name=rich_menu_object_b['name'], |
| 164 | + chat_bar_text=rich_menu_object_b['name'], |
| 165 | + areas=areas |
| 166 | + ) |
| 167 | + |
| 168 | + rich_menu_b_id = line_bot_api.create_rich_menu(rich_menu=rich_menu_to_b_create) |
| 169 | + |
| 170 | + # 5. Upload image to rich menu B |
| 171 | + with open('./public/richmenu-b.png', 'rb') as f: |
| 172 | + line_bot_api.set_rich_menu_image(rich_menu_b_id, 'image/png', f) |
| 173 | + |
| 174 | + # 6. Set rich menu A as the default rich menu |
| 175 | + line_bot_api.set_default_rich_menu(rich_menu_b_id) |
| 176 | + |
| 177 | + # 7. Create rich menu alias A |
| 178 | + alias_a = RichMenuAlias( |
| 179 | + rich_menu_alias_id='richmenu-alias-a', |
| 180 | + rich_menu_id=rich_menu_a_id |
| 181 | + ) |
| 182 | + line_bot_api.create_rich_menu_alias(alias_a) |
| 183 | + |
| 184 | + # 8. Create rich menu alias B |
| 185 | + alias_b = RichMenuAlias( |
| 186 | + rich_menu_alias_id='richmenu-alias-b', |
| 187 | + rich_menu_id=rich_menu_b_id |
| 188 | + ) |
| 189 | + line_bot_api.create_rich_menu_alias(alias_b) |
| 190 | + print('success') |
| 191 | + |
| 192 | + |
| 193 | +main() |
0 commit comments