フリーランス 技術調査ブログ

フリーランス/エンジニア Ruby Python Nodejs Vuejs React Dockerなどの調査技術調査の備忘録

LineBotAPIで画像カルーセル&確認&ユーザー情報取得(Ruby版)

はじめに

画像カルーセル

            @client.reply_message(event['replyToken'], {
              "type": "template",
              "altText": "this is a image carousel template",
              "template": {
                  "type": "image_carousel",
                  "columns": [
                      {
                        "imageUrl": "<画像のURL>",
                        "action": {
                          "type": "message",
                          "label": "選択1",
                          "text": "選択1"
                        }
                      },
                      {
                        "imageUrl": "<画像のURL>",
                        "action": {
                          "type": "message",
                          "label": "選択2",
                          "text": "選択2"
                        }
                      },
                      {
                        "imageUrl": "<画像のURL>",
                        "action": {
                          "type": "uri",
                          "label": "Yahoo",
                          "uri": "http://www.yahoo.co.jp"
                        }
                      }
                  ]
              }
            })

f:id:PX-WING:20200903082040p:plain

確認メッセージ

            @client.reply_message(event['replyToken'], {
              "type": "template",
              "altText": "確認",
              "template": {
                  "type": "confirm",
                  "text": "あなたの性別は?",
                  "actions": [
                      {
                        "type": "message",
                        "label": "男性",
                        "text": "男性"
                      },
                      {
                        "type": "message",
                        "label": "女性",
                        "text": "女性"
                      }
                  ]
              }
            }) 

f:id:PX-WING:20200903082159p:plain

Flex Message

            @client.reply_message(event['replyToken'], {
              "type": "flex",
              "altText": "this is a flex message",
              "contents": {
                "type": "bubble",
                "body": {
                  "type": "box",
                  "layout": "vertical",
                  "contents": [
                    {
                      "type": "text",
                      "text": "ああああああああああああああああああああああああ"
                    },
                    {
                      "type": "text",
                      "text": "いいいいいいいいいいいいいいいいいいいいいいい"
                    }
                  ]
                }
              }
            })

f:id:PX-WING:20200903082347p:plain

ユーザー情報取得

    events = @client.parse_events_from(body)
    events.each do |event|
      user_id = event["source"]["userId"]
      response = @client.get_profile(user_id)
      case response
      when Net::HTTPSuccess then
        contact = JSON.parse(response.body)
        logger.debug("これだよね")
        logger.debug(contact)
      else
        logger.debug("#{response.code} #{response.body}")
      end
   end
  • 取得内容は下記のデータです。電話番号までは取得できないのが残念
{"userId"=>"<取得したUserId>", "displayName"=>"<ユーザーの名前>", "language"=>"ja"}