Bob Cheng
Tips

Line Bot

version: line-bot-python-sdk v3.14.2 https://github.com/line/line-bot-sdk-python

if we have a flex message like this:

test_flex = {
    "type": "bubble",
    "body": {
        "type": "box",
        "layout": "horizontal",
        "contents": [
            {"type": "text", "text": "Hello,"},
            {"type": "text", "text": "World!"},
        ],
    },
}

using below method will got an error: (line-bot-python-sdk doc didn’t provide a usage on this, so I get this from the internet)

flex_message = FlexMessage(altText="flex message", contents=test_flex)
print(flex_message.to_dict())
# output
# {'type': 'flex', 'altText': 'flex message', 'contents': {'type': 'bubble'}}
# the body part disappear!
# and receive error
# HTTP response body: {"message":"A message (messages[0]) in the request body is invalid","details":[{"message":"At least one block must be specified","property":"/"}]}

this will be ok:

flex_message = FlexMessage(altText="flex message", contents=FlexContainer.from_dict(test_flex))
print(flex_message.to_dict())
# output
# {'type': 'flex', 'altText': 'flex message', 'contents': {'type': 'bubble', 'body': {'type': 'box', 'layout': 'horizontal', 'contents': [{'type': 'text', 'text': 'Hello,'}, {'type': 'text', 'text': 'World!'}]}}}

but I don’t know why