Skip to content

Commit

Permalink
Implement flex message feature (#107)
Browse files Browse the repository at this point in the history
* Support flex message. Close #102 (#106)

* Support flex message. Close #102

Ref. line/line-bot-sdk-java#198

* fixed test case

* Fixed typo. s/filter/filler/

* follow flake8 rules

* Add docstring of flex_message
  • Loading branch information
be-hase authored and okdtsk committed Jun 21, 2018
1 parent d6d5215 commit ccb97de
Show file tree
Hide file tree
Showing 7 changed files with 608 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ nosetests.xml
coverage.xml
*,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
Expand Down
118 changes: 115 additions & 3 deletions examples/flask-kitchensink/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
import sys
import tempfile
from argparse import ArgumentParser

from flask import Flask, request, abort

from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
LineBotApiError, InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
Expand All @@ -37,7 +36,10 @@
CarouselTemplate, CarouselColumn, PostbackEvent,
StickerMessage, StickerSendMessage, LocationMessage, LocationSendMessage,
ImageMessage, VideoMessage, AudioMessage, FileMessage,
UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent
UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent,
FlexSendMessage, BubbleContainer, ImageComponent, BoxComponent,
TextComponent, SpacerComponent, IconComponent, ButtonComponent,
SeparatorComponent,
)

app = Flask(__name__)
Expand Down Expand Up @@ -81,6 +83,11 @@ def callback():
# handle webhook body
try:
handler.handle(body, signature)
except LineBotApiError as e:
print("Got exception from LINE Messaging API: %s\n" % e.message)
for m in e.error.details:
print(" %s: %s" % (m.property, m.message))
print("\n")
except InvalidSignatureError:
abort(400)

Expand Down Expand Up @@ -166,6 +173,111 @@ def handle_text_message(event):
line_bot_api.reply_message(event.reply_token, template_message)
elif text == 'imagemap':
pass
elif text == 'flex':
bubble = BubbleContainer(
direction='ltr',
hero=ImageComponent(
url='https://example.com/cafe.jpg',
size='full',
aspect_ratio='20:13',
aspect_mode='cover',
action=URIAction(uri='http://example.com', label='label')
),
body=BoxComponent(
layout='vertical',
contents=[
# title
TextComponent(text='Brown Cafe', weight='bold', size='xl'),
# review
BoxComponent(
layout='baseline',
margin='md',
contents=[
IconComponent(size='sm', url='https://example.com/gold_star.png'),
IconComponent(size='sm', url='https://example.com/grey_star.png'),
IconComponent(size='sm', url='https://example.com/gold_star.png'),
IconComponent(size='sm', url='https://example.com/gold_star.png'),
IconComponent(size='sm', url='https://example.com/grey_star.png'),
TextComponent(text='4.0', size='sm', color='#999999', margin='md',
flex=0)
]
),
# info
BoxComponent(
layout='vertical',
margin='lg',
spacing='sm',
contents=[
BoxComponent(
layout='baseline',
spacing='sm',
contents=[
TextComponent(
text='Place',
color='#aaaaaa',
size='sm',
flex=1
),
TextComponent(
text='Shinjuku, Tokyo',
wrap=True,
color='#666666',
size='sm',
flex=5
)
],
),
BoxComponent(
layout='baseline',
spacing='sm',
contents=[
TextComponent(
text='Time',
color='#aaaaaa',
size='sm',
flex=1
),
TextComponent(
text="10:00 - 23:00",
wrap=True,
color='#666666',
size='sm',
flex=5,
),
],
),
],
)
],
),
footer=BoxComponent(
layout='vertical',
spacing='sm',
contents=[
# callAction, separator, websiteAction
SpacerComponent(size='sm'),
# callAction
ButtonComponent(
style='link',
height='sm',
action=URIAction(label='CALL', uri='tel:000000'),
),
# separator
SeparatorComponent(),
# websiteAction
ButtonComponent(
style='link',
height='sm',
action=URIAction(label='WEBSITE', uri="https://example.com")
)
]
),
)
message = FlexSendMessage(alt_text="hello", contents=bubble)
line_bot_api.reply_message(
event.reply_token,
message
)
else:
line_bot_api.reply_message(
event.reply_token, TextSendMessage(text=event.message.text))
Expand Down
17 changes: 17 additions & 0 deletions linebot/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,20 @@
ImageCarouselTemplate,
ImageCarouselColumn,
)
from .flex_message import ( # noqa
FlexSendMessage,
FlexContainer,
BubbleContainer,
BubbleStyle,
BlockStyle,
CarouselContainer,
FlexComponent,
BoxComponent,
ButtonComponent,
FillerComponent,
IconComponent,
ImageComponent,
SeparatorComponent,
SpacerComponent,
TextComponent
)
3 changes: 1 addition & 2 deletions linebot/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ def as_json_dict(self):

elif hasattr(value, 'as_json_dict'):
data[camel_key] = value.as_json_dict()

else:
elif value is not None:
data[camel_key] = value

return data
Expand Down
Loading

0 comments on commit ccb97de

Please sign in to comment.