LoginSignup
10
11

More than 5 years have passed since last update.

[LINE] 最新の Flex Message を試してみた

Last updated at Posted at 2018-07-11

Flex Message

Flex Message の機能が新規追加されたので、色々試してみることにした。
下記に「curlのサンプル」と「トークルームで表示される画像」を添付しておくので参考にして欲しい。

詳しい概要説明は、LINE公式のリファレンス参照
https://developers.line.me/ja/reference/messaging-api/

PushMessage の messageObject の構成

  • 全体構成を簡単にまとめておく。

バブルコンテナの構成

- to
- message = messageObject (テンプレートメッセージ (カルーセルで使用) or Flex Message など..)
              - type (flex)
              - altText (代替テキスト)
              - contents = Flex Messageのコンテナオブジェクト (バブルコンテナ)
                              - type = bubble
                              - header = ボックスコンポーネント
                              - hero = 画像コンポーネント
                              - body = ボックスコンポーネント
                              - footer = ボックスコンポーネント
                              - direction = テキストの書字方向および水平ボックス内のコンポーネントの並び順
                              - styles = バブルスタイルオブジェクト
                                            - header = ブロックスタイル
                                            - hero = ブロックスタイル
                                            - body = ブロックスタイル
                                            - footer = ブロックスタイル
                                                        - backgroundColor
                                                        - separator 
                                                        - separatorColor

バブルコンテナのSAMPLE

百聞は一見に如かずという事で、まずサンプル (バブルコンテナとカルーセルコンテナ) をお見せする。
コンテナの構成する要素の説明については、後ほど説明していきます。

curl -X POST -H 'Content-Type:application/json' -H 'Authorization: Bearer {トークン}' -d '
{
    "messages": [
        {  
        "type": "flex",
        "altText": "this is a flex message",
        "contents": {
          "type": "bubble",
          "header": {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "text",
                "text": "Header text"
              }
            ]
          },
          "hero": {
            "type": "image",
            "url": "https://www.sample.jpg"
          },
          "body": {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "text",
                "text": "hello"
              },
              {
                "type": "text",
                "text": "world"
              }
            ]
          },
          "footer": {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "text",
                "text": "Footer text"
              }
            ]
          },
          "styles": {
             "header": {
               "backgroundColor": "#00ffff"
             },
             "hero": {
               "separator": true,
               "separatorColor": "#000000"
             },
             "footer": {
               "backgroundColor": "#00ffff",
               "separator": true,
               "separatorColor": "#000000"
             }
           }
        }
      }
    ],
    "to": "LINE UserID"
}' \https://api.line.me/v2/bot/message/push

スクリーンショット 2018-07-11 午後1.20.45.png

カルーセルコンテナの構成

  • type = carousel になっている
  • バブル数は、最大10個までとなっている
  • バブルコンテナにボディブロックがある場合は、そのバブルコンテナがカルーセルで最大の高さを持つバブルコンテナと同じ高さになるように、ボディブロックが伸長します。 (※リファレンス引用)
- to
- message = メッセージオブジェクト (テンプレートメッセージ or Flex Message etc..)
              - type (flex)
              - altText (代替テキスト)
              - contents = Flex Messageのコンテナオブジェクト (カルーセル)
                              - type = carousel
                              - contents = [バブルコンテナ1, バブルコンテナ2, バブルコンテナ3] // 最大バブル数:10

カルーセルコンテナのSAMPLE

curl -X POST -H 'Content-Type:application/json' -H 'Authorization: Bearer {トークン}' -d '{
    "messages": [
        {  
        "type": "flex",
        "altText": "this is a flex message",
        "contents": {
          "type": "carousel",
          "contents": [
          {
            "type": "bubble",
            "header": {
              "type": "box",
              "layout": "vertical",
              "contents": [
                {
                  "type": "text",
                  "text": "Header text"
                }
              ]
            },
            "hero": {
              "type": "image",
              "url": "https://example.com/flex/images/image.jpg"
            },
            "body": {
              "type": "box",
              "layout": "vertical",
              "contents": [
                {
                  "type": "text",
                  "text": "hello"
                },
                {
                  "type": "text",
                  "text": "world"
                }
              ]
            },
            "footer": {
              "type": "box",
              "layout": "vertical",
              "contents": [
                {
                  "type": "text",
                  "text": "Footer text"
                }
              ]
            },
            "styles": {
               "header": {
                 "backgroundColor": "#00ffff"
               },
               "hero": {
                 "separator": true,
                 "separatorColor": "#000000"
               },
               "footer": {
                 "backgroundColor": "#00ffff",
                 "separator": true,
                 "separatorColor": "#000000"
               }
             }
          },
            {
              "type": "bubble",
              "body": {
                "type": "box",
                "layout": "vertical",
                "contents": [
                  {
                    "type": "text",
                    "text": "Second bubble"
                  }
                ]
              }
            }
          ]
        }
      }
    ],
    "to": "LINE UserID"
}' \https://api.line.me/v2/bot/message/push

スクリーンショット 2018-07-11 午後1.21.22.png

バブルコンテナ を構成する要素の説明

バブルコンテナ = 
      - type = bubble
      - direction = テキストの書字方向および水平ボックス内のコンポーネントの並び順
      - header = ボックスコンポーネント
      - hero = 画像コンポーネント
      - body = ボックスコンポーネント
      - footer = ボックスコンポーネント
      - styles = バブルスタイルオブジェクト

directionについて

  • 指定可能な値
    • 1. ltr
    • 2. rtl

direction = ltr

  • left to right に配置する
curl -X POST -H 'Content-Type:application/json' -H 'Authorization: Bearer {トークン}' -d '
{
    "messages": [
        {  
        "type": "flex",
        "altText": "this is a flex message",
        "contents": {
          "type": "bubble",
          "direction": "ltr",
          "hero": {
                    "type": "image",
                    "url": "https://www.sample.jpg",
                    "size": "full"
                  },
          "body": {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "text",
                "text": "hello"
              },
              {
                "type": "text",
                "text": "world"
              }
            ]
          }
        }
      }
    ],
    "to": "LINE UserID"
}' \https://api.line.me/v2/bot/message/push

スクリーンショット 2018-07-11 午後1.26.12.png

direction = rtl

  • right to left に配置する
curl -X POST -H 'Content-Type:application/json' -H 'Authorization: Bearer {トークン}' -d '
{
    "messages": [
        {  
        "type": "flex",
        "altText": "this is a flex message",
        "contents": {
          "type": "bubble",
          "direction": "ltr",
          "hero": {
                    "type": "image",
                    "url": "https://www.sample.jpg",
                    "size": "full"
                  },
          "body": {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "text",
                "text": "hello"
              },
              {
                "type": "text",
                "text": "world"
              }
            ]
          }
        }
      }
    ],
    "to": "LINE UserID"
}' \https://api.line.me/v2/bot/message/push

スクリーンショット 2018-07-11 午後1.23.30.png

コンポーネントについて

バブルコンテナを構成するオブジェクト(要素) のこと

コンポーネントの種類

  1. ボックス
  2. ボタン
  3. フィラー
  4. アイコン
  5. 画像
  6. セパレータ
  7. スペーサー
  8. テキスト

ボックスコンポーネントとは

子要素のレイアウトを定義するコンポーネントです。ボックスにボックスを含めることもできます。

ボックスコンポーネント = 
  - type =  box (固定)
  - layout =  vertical / horizon / baseline
  - contents = [コンポーネント1, コンポーネント2]
  - mergin
  - spacing
  - flex

layout の種類

  1. horizontal (水平配置)
  2. vertical (垂直配置)
  3. baseline (水平配置)

layout の仕様

  • horizontal / vertical の場合:ボックス、ボタン、フィラー、画像、セパレータ、およびテキストコンポーネント
  • baselineの場合:フィラー、アイコン、およびテキストコンポーネント

vertical

curl -X POST -H 'Content-Type:application/json' -H 'Authorization: Bearer {トークン}' -d '
{
    "messages": [
        {  
        "type": "flex",
        "altText": "this is a flex message",
        "contents": {
          "type": "bubble",
          "direction": "rtl",
          "hero": {
                    "type": "image",
                    "url": "https://www.sample.jpg",
                    "size": "full"
                  },
          "body": {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "text",
                "text": "hello"
              },
              {
                "type": "text",
                "text": "world"
              }
            ]
          }
        }
      }
    ],
    "to": "LINE UserID"
}' \https://api.line.me/v2/bot/message/push

スクリーンショット 2018-07-11 午後1.26.12.png

horizon

curl -X POST -H 'Content-Type:application/json' -H 'Authorization: Bearer {トークン}' -d '
{
    "messages": [
        {
        "type": "flex",
        "altText": "this is a flex message",
        "contents": {
          "type": "bubble",
          "direction": "ltr",
          "hero": {
                    "type": "image",
                    "url": "https://www.sample.jpg",
                    "size": "full"
                  },
          "body": {
            "type": "box",
            "layout": "horizontal",
            "contents": [
              {
                "type": "text",
                "text": "hello"
              },
              {
                "type": "text",
                "text": "world"
              }
            ]
          }
        }
      }
    ],
    "to": "LINE UserID"
}' \https://api.line.me/v2/bot/message/push

スクリーンショット 2018-07-11 午後1.26.50.png

ボタンコンポーネントとは

ボタンを描画するコンポーネントです。ユーザーがタップすると、指定したアクションが実行されます。

ボタンコンポーネント = 
  - type =  button (固定)
  - action =  アクションオブジェクト (uri / postback / message/ 日時選択)
  - color 
  - height
  - mergin

SAMPLE

curl -X POST -H 'Content-Type:application/json' -H 'Authorization: Bearer {トークン}' -d '
{
    "messages": [
        {  
        "type": "flex",
        "altText": "this is a flex message",
        "contents": {
          "type": "bubble",
          "direction": "ltr",
          "hero": {
                    "type": "image",
                    "url": "https://rs-j.adtdp.com/line-dev/10/carousel_image/pixta_29373431_M.jpg",
                    "size": "full"
                  },
          "body": {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "text",
                "text": "hello"
              },
              {
                "type": "text",
                "text": "world"
              },
              {
              "type": "button",
              "action": {
                "type": "uri",
                "label": "Tap me",
                "uri": "https://example.com"
              },
              "style": "primary",
              "color": "#0000ff"
            }
            ]
          }
        }
      }
    ],
    "to": "LINE UserID"
}' \https://api.line.me/v2/bot/message/push

スクリーンショット 2018-07-11 午後1.27.26.png

アイコンコンポーネントとは

アイコンを描画するコンポーネントです。

アイコンコンポーネント = 
  - type =  icon (固定)
  - url =  画像url (最大画像サイズ:240×240px)
  - size 
  - mergin
curl -X POST -H 'Content-Type:application/json' -H 'Authorization: Bearer {トークン}' -d '
{
    "messages": [
        {  
        "type": "flex",
        "altText": "this is a flex message",
        "contents": {
          "type": "bubble",
          "body": {
            "type": "box",
            "layout": "baseline",
            "contents": [
            {
              "type": "icon",
              "url": "https://sample.jpg",
              "size": "lg"
            },
            {
              "type": "icon",
              "url": "https://sample.jpg",
              "size": "lg"
            }
            ]
          }
        }
      }
    ],
    "to": "LINE UserID"
}' \https://api.line.me/v2/bot/message/push

スクリーンショット 2018-07-11 午後1.27.48.png

セパレータコンポーネントとは

親ボックス内のコンポーネントの間に境界線を描画するコンポーネントです。

セパレータコンポーネント = 
  - type =  separator (固定)
  - margin
  - color

SAMPLE

curl -X POST -H 'Content-Type:application/json' -H 'Authorization: Bearer {トークン}' -d '{
    "messages": [
        {  
        "type": "flex",
        "altText": "this is a flex message",
        "contents": {
          "type": "carousel",
          "contents": [
          {
            "type": "bubble",
            "header": {
              "type": "box",
              "layout": "vertical",
              "contents": [
                {
                  "type": "text",
                  "text": "Header text"
                }
              ]
            },
            "hero": {
              "type": "image",
              "url": "https://example.com/flex/images/image.jpg"
            },
            "body": {
              "type": "box",
              "layout": "vertical",
              "contents": [
                {
                  "type": "text",
                  "text": "hello"
                },
                {
                  "type": "separator",
                  "color": "#000000"
                },
                {
                  "type": "text",
                  "text": "world"
                }
              ]
            },
            "footer": {
              "type": "box",
              "layout": "vertical",
              "contents": [
                {
                  "type": "text",
                  "text": "Footer text"
                }
              ]
            },
            "styles": {
               "header": {
                 "backgroundColor": "#00ffff"
               },
               "hero": {
                 "separator": true,
                 "separatorColor": "#000000"
               },
               "footer": {
                 "backgroundColor": "#00ffff",
                 "separator": true,
                 "separatorColor": "#000000"
               }
             }
          },
            {
              "type": "bubble",
              "body": {
                "type": "box",
                "layout": "vertical",
                "contents": [
                  {
                    "type": "text",
                    "text": "Second bubble"
                  }
                ]
              }
            }
          ]
        }
      }
    ],
    "to": "LINE UserID"
}' \https://api.line.me/v2/bot/message/push

スクリーンショット 2018-07-11 午後1.28.20.png

スペーサーコンポーネントとは

ボックス内の先頭または末尾に固定サイズのスペースを配置する不可視のコンポーネントです

スペーサーコンポーネント = 
  - type =  spacer (固定)
  - size = xs / sm / md / lg / xx / xxl

SAMPLE

curl -X POST -H 'Content-Type:application/json' -H 'Authorization: Bearer {トークン' -d '{
    "messages": [
        {  
        "type": "flex",
        "altText": "this is a flex message",
        "contents": {
          "type": "carousel",
          "contents": [
          {
            "type": "bubble",
            "header": {
              "type": "box",
              "layout": "vertical",
              "contents": [
                {
                  "type": "text",
                  "text": "Header text"
                }
              ]
            },
            "hero": {
              "type": "image",
              "url": "https://example.com/flex/images/image.jpg"
            },
            "body": {
              "type": "box",
              "layout": "vertical",
              "contents": [
                {
                  "type": "text",
                  "text": "hello"
                },
                {
                  "type": "separator",
                  "color": "#000000"
                },
                {
                  "type": "text",
                  "text": "world"
                },
                {
                  "type": "spacer",
                  "size": "xxl"
                }
              ]
            },
            "footer": {
              "type": "box",
              "layout": "vertical",
              "contents": [
                {
                  "type": "text",
                  "text": "Footer text"
                }
              ]
            },
            "styles": {
               "header": {
                 "backgroundColor": "#00ffff"
               },
               "hero": {
                 "separator": true,
                 "separatorColor": "#000000"
               },
               "footer": {
                 "backgroundColor": "#00ffff",
                 "separator": true,
                 "separatorColor": "#000000"
               }
             }
          },
            {
              "type": "bubble",
              "body": {
                "type": "box",
                "layout": "vertical",
                "contents": [
                  {
                    "type": "text",
                    "text": "Second bubble"
                  }
                ]
              }
            }
          ]
        }
      }
    ],
    "to": "LINE UserID"
}' \https://api.line.me/v2/bot/message/push

スペーサー無

スクリーンショット 2018-07-11 午後1.28.59.png

スペーサー有

スクリーンショット 2018-07-11 午後1.29.23.png

注意事項

  • multi castで Flex Messageは送信できません。
10
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11