Q&A

PythonでFlex Messageで取得した外部ファイルのJsonを読み込んで実装したい

前提・実現したいこと

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

  • 0
  • 2
  • 2003
  • twitter facebook

回答ありがとうございます。 アドバイスを頂いたところと、そもそものjsonの記述が間違っていたようで、サンプルコードに変えると動きました!

  • 0

参考にされたサイトのうちの一つと同じ状態だと思います。(https://www.line-community.me/ja/question/5d405945851f743fd7cd97c6/line-bot-designer-%E3%81%A7%E4%BD%9C%E6%88%90%E3%81%97%E3%81%9F-flex-message-%E3%82%92-linebot-%E3%81%A7%E9%80%81%E4%BF%A1%E3%81%97%E3%81%9F%E3%81%84

bus.jsoncontentsの中身だけを残すと多分正しいリクエストになると思います。 つまり、bus.jsonの先頭は

"type": "bubble",
    "hero": {
      "type": "image",
			...

としてください。

main.pyを直しても動きそうで、contents=flex_message_json_dictcontents=flex_message_json_dict["contents]としてもよさそうです。

というのも、FlexSendMessageに本来渡すべきなのはcontentsであり、alttextを含んだjsonではないからです(これを渡してしまっています!)。

  • 3

関連する質問

    関連する質問はありません

本当によろしいですか? question.vm