|
19 | 19 | import sys
|
20 | 20 | import tempfile
|
21 | 21 | from argparse import ArgumentParser
|
22 |
| - |
23 | 22 | from flask import Flask, request, abort
|
24 | 23 |
|
25 | 24 | from linebot import (
|
26 | 25 | LineBotApi, WebhookHandler
|
27 | 26 | )
|
28 | 27 | from linebot.exceptions import (
|
29 |
| - InvalidSignatureError |
| 28 | + LineBotApiError, InvalidSignatureError |
30 | 29 | )
|
31 | 30 | from linebot.models import (
|
32 | 31 | MessageEvent, TextMessage, TextSendMessage,
|
|
37 | 36 | CarouselTemplate, CarouselColumn, PostbackEvent,
|
38 | 37 | StickerMessage, StickerSendMessage, LocationMessage, LocationSendMessage,
|
39 | 38 | ImageMessage, VideoMessage, AudioMessage, FileMessage,
|
40 |
| - UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent |
| 39 | + UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent, |
| 40 | + FlexSendMessage, BubbleContainer, ImageComponent, BoxComponent, |
| 41 | + TextComponent, SpacerComponent, IconComponent, ButtonComponent, |
| 42 | + SeparatorComponent, |
41 | 43 | )
|
42 | 44 |
|
43 | 45 | app = Flask(__name__)
|
@@ -81,6 +83,11 @@ def callback():
|
81 | 83 | # handle webhook body
|
82 | 84 | try:
|
83 | 85 | handler.handle(body, signature)
|
| 86 | + except LineBotApiError as e: |
| 87 | + print("Got exception from LINE Messaging API: %s\n" % e.message) |
| 88 | + for m in e.error.details: |
| 89 | + print(" %s: %s" % (m.property, m.message)) |
| 90 | + print("\n") |
84 | 91 | except InvalidSignatureError:
|
85 | 92 | abort(400)
|
86 | 93 |
|
@@ -166,6 +173,111 @@ def handle_text_message(event):
|
166 | 173 | line_bot_api.reply_message(event.reply_token, template_message)
|
167 | 174 | elif text == 'imagemap':
|
168 | 175 | pass
|
| 176 | + elif text == 'flex': |
| 177 | + bubble = BubbleContainer( |
| 178 | + direction='ltr', |
| 179 | + hero=ImageComponent( |
| 180 | + url='https://example.com/cafe.jpg', |
| 181 | + size='full', |
| 182 | + aspect_ratio='20:13', |
| 183 | + aspect_mode='cover', |
| 184 | + action=URIAction(uri='http://example.com', label='label') |
| 185 | + ), |
| 186 | + body=BoxComponent( |
| 187 | + layout='vertical', |
| 188 | + contents=[ |
| 189 | + # title |
| 190 | + TextComponent(text='Brown Cafe', weight='bold', size='xl'), |
| 191 | + # review |
| 192 | + BoxComponent( |
| 193 | + layout='baseline', |
| 194 | + margin='md', |
| 195 | + contents=[ |
| 196 | + IconComponent(size='sm', url='https://example.com/gold_star.png'), |
| 197 | + IconComponent(size='sm', url='https://example.com/grey_star.png'), |
| 198 | + IconComponent(size='sm', url='https://example.com/gold_star.png'), |
| 199 | + IconComponent(size='sm', url='https://example.com/gold_star.png'), |
| 200 | + IconComponent(size='sm', url='https://example.com/grey_star.png'), |
| 201 | + TextComponent(text='4.0', size='sm', color='#999999', margin='md', |
| 202 | + flex=0) |
| 203 | + ] |
| 204 | + ), |
| 205 | + # info |
| 206 | + BoxComponent( |
| 207 | + layout='vertical', |
| 208 | + margin='lg', |
| 209 | + spacing='sm', |
| 210 | + contents=[ |
| 211 | + BoxComponent( |
| 212 | + layout='baseline', |
| 213 | + spacing='sm', |
| 214 | + contents=[ |
| 215 | + TextComponent( |
| 216 | + text='Place', |
| 217 | + color='#aaaaaa', |
| 218 | + size='sm', |
| 219 | + flex=1 |
| 220 | + ), |
| 221 | + TextComponent( |
| 222 | + text='Shinjuku, Tokyo', |
| 223 | + wrap=True, |
| 224 | + color='#666666', |
| 225 | + size='sm', |
| 226 | + flex=5 |
| 227 | + ) |
| 228 | + ], |
| 229 | + ), |
| 230 | + BoxComponent( |
| 231 | + layout='baseline', |
| 232 | + spacing='sm', |
| 233 | + contents=[ |
| 234 | + TextComponent( |
| 235 | + text='Time', |
| 236 | + color='#aaaaaa', |
| 237 | + size='sm', |
| 238 | + flex=1 |
| 239 | + ), |
| 240 | + TextComponent( |
| 241 | + text="10:00 - 23:00", |
| 242 | + wrap=True, |
| 243 | + color='#666666', |
| 244 | + size='sm', |
| 245 | + flex=5, |
| 246 | + ), |
| 247 | + ], |
| 248 | + ), |
| 249 | + ], |
| 250 | + ) |
| 251 | + ], |
| 252 | + ), |
| 253 | + footer=BoxComponent( |
| 254 | + layout='vertical', |
| 255 | + spacing='sm', |
| 256 | + contents=[ |
| 257 | + # callAction, separator, websiteAction |
| 258 | + SpacerComponent(size='sm'), |
| 259 | + # callAction |
| 260 | + ButtonComponent( |
| 261 | + style='link', |
| 262 | + height='sm', |
| 263 | + action=URIAction(label='CALL', uri='tel:000000'), |
| 264 | + ), |
| 265 | + # separator |
| 266 | + SeparatorComponent(), |
| 267 | + # websiteAction |
| 268 | + ButtonComponent( |
| 269 | + style='link', |
| 270 | + height='sm', |
| 271 | + action=URIAction(label='WEBSITE', uri="https://example.com") |
| 272 | + ) |
| 273 | + ] |
| 274 | + ), |
| 275 | + ) |
| 276 | + message = FlexSendMessage(alt_text="hello", contents=bubble) |
| 277 | + line_bot_api.reply_message( |
| 278 | + event.reply_token, |
| 279 | + message |
| 280 | + ) |
169 | 281 | else:
|
170 | 282 | line_bot_api.reply_message(
|
171 | 283 | event.reply_token, TextSendMessage(text=event.message.text))
|
|
0 commit comments