前提・実現したいこと
PythonでFlex Messageで取得した外部のJsonを読み込んで実装したいです。
main.pyにGithubのFlexSendMessageの項目2つ目のコードをmain.pyに貼り付けると出来ましたが、Jsonデータを外部において読み込んだものをFlexSendMessage()で使うとエラーが出てしまいます。
発生している問題・エラーメッセージ
raise LineBotApiError(
2022-03-25T08:08:37.136565+00:00 app[web.1]: linebot.exceptions.LineBotApiError: LineBotApiError: status_code=400, request_id=b2ac85b9-d2ca-4448-929a-3cd1d98d3006, error_response={"details": [{"message": "May not be empty", "property": "messages[0].contents"}], "message": "The request body has 1 error(s)"}, headers={'Content-Type': 'application/json', 'Server': 'envoy', 'x-content-type-options': 'nosniff', 'x-frame-options': 'DENY', 'x-line-request-id': 'b2ac85b9-d2ca-4448-929a-3cd1d98d3006', 'x-xss-protection': '1; mode=block', 'Content-Length': '122', 'Expires': 'Fri, 25
Mar 2022 08:08:37 GMT', 'Cache-Control': 'max-age=0, no-cache, no-store', 'Pragma': 'no-cache', 'Date': 'Fri, 25 Mar 2022 08:08:37 GMT', 'Connection': 'close'}
該当のソースコード
main.py(一部抜粋)
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
print(event)
f = open('bus.json', 'r')
flex_message_json_dict = json.load(f)
print(type(flex_message_json_dict))
line_bot_api.reply_message(
event.reply_token,
FlexSendMessage(
alt_text='alt_text',
#contentsパラメタに, dict型の値を渡す
contents=flex_message_json_dict
)
)
print("完了")
bus.json
{
"type": "flex",
"altText": "Flex Message",
"contents": {
"type": "bubble",
"hero": {
"type": "image",
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_1_cafe.png",
"size": "full",
"aspectRatio": "20:13",
"aspectMode": "cover",
"action": {
"type": "uri",
"label": "Line",
"uri": "https://linecorp.com/"
}
},
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "Brown Cafe",
"size": "xl",
"weight": "bold"
},
{
"type": "box",
"layout": "baseline",
"margin": "md",
"contents": [
{
"type": "icon",
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png",
"size": "sm"
},
{
"type": "icon",
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png",
"size": "sm"
},
{
"type": "icon",
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png",
"size": "sm"
},
{
"type": "icon",
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png",
"size": "sm"
},
{
"type": "icon",
"url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gray_star_28.png",
"size": "sm"
},
{
"type": "text",
"text": "4.0",
"flex": 0,
"margin": "md",
"size": "sm",
"color": "#999999"
}
]
},
{
"type": "box",
"layout": "vertical",
"spacing": "sm",
"margin": "lg",
"contents": [
{
"type": "box",
"layout": "baseline",
"spacing": "sm",
"contents": [
{
"type": "text",
"text": "Place",
"flex": 1,
"size": "sm",
"color": "#AAAAAA"
},
{
"type": "text",
"text": "Miraina Tower, 4-1-6 Shinjuku, Tokyo",
"flex": 5,
"size": "sm",
"color": "#666666",
"wrap":true
}
]
},
{
"type": "box",
"layout": "baseline",
"spacing": "sm",
"contents": [
{
"type": "text",
"text": "Time",
"flex": 1,
"size": "sm",
"color": "#AAAAAA"
},
{
"type": "text",
"text": "10:00 - 23:00",
"flex": 5,
"size": "sm",
"color": "#666666",
"wrap": true
}
]
}
]
}
]
},
"footer": {
"type": "box",
"layout": "vertical",
"flex": 0,
"spacing": "sm",
"contents": [
{
"type": "button",
"action": {
"type": "uri",
"label": "CALL",
"uri": "https://linecorp.com"
},
"height": "sm",
"style": "link"
},
{
"type": "button",
"action": {
"type": "uri",
"label": "WEBSITE",
"uri": "https://linecorp.com"
},
"height": "sm",
"style": "link"
},
{
"type": "spacer",
"size": "sm"
}
]
}
}
}
試したこと
以下のサイトを参考にしました。
LINE BotでFlex Messageを使ってみる(Python編)
Line Bot Designer で作成した Flex Message を linebot で送信したい
LINE Message API - Push Message -
補足情報(FW/ツールのバージョンなど)
python 3.6.8
line-bot-sdk 2.1.0