LoginSignup
4
1

More than 1 year has passed since last update.

LINE WORKS Bot メッセージの送信 JavaScript サンプルコード

Posted at

Bot - メッセージの送信

Bot からのメッセージ送信方法は、ユーザー指定とトークルーム指定の2つがあります。

グループや組織のトークルームにメッセージを送信するにはトークルーム指定を使用します。

JavaScript Sample Code

request モジュールを使用したサンプルコードです。
事前に npm install request を行って request モジュールをインストールしてください。
下記はユーザー指定でのメッセージ送信です。

ApiRequest.js
var request = require("request");
var token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==";
var botId = "1234567890";
var userId = "xxxxx-xxxxxxxxx-xxxxxx";
var options = {
  "method": "POST",
  "url": `https://www.worksapis.com/v1.0/bots/${botId}/users/${userId}/messages`,
  "headers": {
    "Content-Type": "application/json",
    "Authorization": `Bearer ${token}`
  },
  "body": JSON.stringify({
    "content": {
      "type": "text",
      "text": "test"
    }
  })
};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(`statusCode: ${response.statusCode}\nstatusMessage: ${response.statusMessage}`);
});

Request Body Sample

サンプルコードの body を変更することにより、様々なタイプのメッセージを送信することができます。

type: text

"content": {
  "type": "text",
  "text": "test"
}

1657265765.png

type: image

"content": {
    "type": "image",
    "previewUrl": "https://developers.worksmobile.com/favicon.png",
    "resourceUrl": "https://developers.worksmobile.com/favicon.png"
}

1657265963.png

type: link

"content": {
    "type": "link",
    "contentText": "contentText",
    "linkText": "worksmobile homepage",
    "link": "http://www.worksmobile.com"
}

1657267842.png

type: sticker

"content": {
    "type": "sticker",
    "packageId": "1",
    "stickerId": "3"
}

1657504707.png

type: button_template

"content": {
    "type": "button_template",
    "contentText": "button template",
    "actions": [
        {
            "type": "message",
            "label": "Message lable",
            "text": "Message text"
        },
        {
            "type": "message",
            "label": "Postback lable",
            "text": "Postback text",
            "postback": "postback data"
        },
        {
            "type": "uri",
            "label": "LINE WORKS",
            "uri": "https://line.worksmobile.com/jp/"
        }
    ]
}

1657504882.png

type: list_template

"content": {
    "type": "list_template",
    "coverData": {
        "backgroundImage": "https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png",
        "title": "title",
        "subtitle": "subtitle"
    },
    "elements": [{
        "title": "WorksMobile's Homepage",
        "subtitle": "Link action subtitle",
        "image": "https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png",
        "action": {
            "type": "uri",
            "label": "Visit",
            "uri": "https://line.worksmobile.com"
        }
    }, {
        "title": "List Temaplate postback",
        "subtitle": "postback subtitle",
        "image": "https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png",
        "action": {
            "type": "message",
            "label": "Postback lable",
            "text": "Postback text",
            "postback": "postback data"
        }
    }],
    "actions": [[{
        "type": "message",
        "label": "下部ボタン Actions",
        "text": "下部ボタン Actions",
        "postback": "bottom button actions postback"
    }]]
}

1657524595.png

type: carousel

"content": {
    "type": "carousel",
    "imageAspectRatio": "square",
    "columns": [{
        "thumbnailImageUrl": "https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png",
        "title": "carousel title 1",
        "text": "roomId carousel text",
        "defaultAction": {
            "type":"uri",
            "label":"Visit",
            "uri":"https://www.naver.com"
        },
        "actions": [{
            "type": "postback",
            "label": "carousel Postback",
            "data": "carousel postback data",
            "displayText": "Postback"
        },{
            "type": "message",
            "label": "carousel message",
            "text": "carousel message"
        },{
            "type": "uri",
            "label": "LINE WORKS",
            "uri": "https://line.worksmobile.com/jp/"
        }]
    },{
        "thumbnailImageUrl": "https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png",
        "title": "carousel title 2",
        "text": "carousel text 2",
        "defaultAction": {
            "type":"uri",
            "label":"Visit",
            "uri":"https://www.naver.com"
        },
        "actions": [{
            "type": "message",
            "label": "carousel Actions",
            "text": "carousel Actions",
            "postback": "carousel actions postback"
        }]
    }]
}

1657525135.png

type: image_carousel

"content": {
    "type": "image_carousel",
    "columns": [{
        "imageUrl": "https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png",
        "action": {
            "type": "message",
            "label": "imgCarousel1",
            "text": "image carousel message"
        }
    },{
        "imageUrl": "https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png",
        "action": {
            "type": "message",
            "label": "imgCarousel2",
            "text": "image carousel message"
        }
    }]
}

1657525453.png

type: file

"content": {
    "type": "file",
    "resourceUrl": "https://line.worksmobile.com/kr/wp-content/uploads/2020/05/user-start-guide-v2.0.pdf"
}

1657525994.png

type: flex

{
  "type": "carousel",
  "contents": [
    {
      "type": "bubble",
      "size": "mega",
      "header": {
        "type": "box",
        "layout": "horizontal",
        "contents": [
          {
            "type": "text",
            "text": "WORKS MOBILE",
            "size": "lg",
            "color": "#00c73c",
            "weight": "bold"
          }
        ],
        "backgroundColor": "#ffffff"
      },
      "body": {
        "type": "box",
        "layout": "vertical",
        "contents": [
          {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "image",
                "url": "https://i.ibb.co/tXyrDKh/11-matthew-operation-quality-manager.png",
                "size": "full"
              }
            ],
            "width": "120px",
            "height": "120px",
            "cornerRadius": "180px",
            "borderWidth": "1px",
            "borderColor": "#22222210",
            "offsetStart": "75px",
            "offsetEnd": "75px"
          },
          {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "text",
                "text": "John Smith",
                "weight": "bold",
                "size": "xl",
                "align": "center"
              },
              {
                "type": "text",
                "text": "Graphic Designer",
                "size": "sm",
                "align": "center"
              }
            ],
            "margin": "xl"
          },
          {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "box",
                "layout": "baseline",
                "contents": [
                  {
                    "type": "text",
                    "text": "Department",
                    "size": "sm",
                    "color": "#858f89",
                    "flex": 3,
                    "margin": "md"
                  },
                  {
                    "type": "text",
                    "text": "Product Design 1",
                    "size": "sm",
                    "margin": "md",
                    "flex": 5
                  }
                ]
              },
              {
                "type": "box",
                "layout": "baseline",
                "contents": [
                  {
                    "type": "text",
                    "text": "Phone",
                    "size": "sm",
                    "color": "#858f89",
                    "flex": 3,
                    "margin": "md"
                  },
                  {
                    "type": "text",
                    "text": "000-111-2222",
                    "size": "sm",
                    "margin": "md",
                    "flex": 5
                  }
                ],
                "margin": "sm"
              },
              {
                "type": "box",
                "layout": "baseline",
                "contents": [
                  {
                    "type": "text",
                    "text": "E-mail",
                    "size": "sm",
                    "color": "#858f89",
                    "flex": 3,
                    "margin": "md"
                  },
                  {
                    "type": "text",
                    "text": "email@works.com",
                    "size": "sm",
                    "margin": "md",
                    "flex": 5
                  }
                ],
                "margin": "sm"
              }
            ],
            "margin": "xxl",
            "width": "260px",
            "alignItems": "center",
            "paddingAll": "6px"
          }
        ]
      },
      "footer": {
        "type": "box",
        "layout": "vertical",
        "contents": [
          {
            "type": "button",
            "action": {
              "type": "uri",
              "label": "Send a message",
              "uri": "https://worksmobile.com"
            },
            "style": "primary"
          },
          {
            "type": "button",
            "action": {
              "type": "uri",
              "label": "Call",
              "uri": "https://worksmobile.com"
            },
            "margin": "md",
            "style": "secondary"
          }
        ]
      }
    },
    {
      "type": "bubble",
      "size": "mega",
      "header": {
        "type": "box",
        "layout": "horizontal",
        "contents": [
          {
            "type": "text",
            "text": "WORKS MOBILE",
            "size": "lg",
            "color": "#00c73c",
            "weight": "bold"
          }
        ],
        "backgroundColor": "#ffffff"
      },
      "body": {
        "type": "box",
        "layout": "vertical",
        "contents": [
          {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "image",
                "url": "https://i.ibb.co/jrD8083/05-emma-business-maketing-manager.png",
                "size": "full"
              }
            ],
            "width": "120px",
            "height": "120px",
            "cornerRadius": "180px",
            "borderColor": "#22222210",
            "borderWidth": "1px",
            "offsetStart": "75px",
            "offsetEnd": "75px"
          },
          {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "text",
                "text": "Sarah Harding",
                "weight": "bold",
                "size": "xl",
                "align": "center"
              },
              {
                "type": "text",
                "text": "UX Designer",
                "size": "sm",
                "align": "center"
              }
            ],
            "margin": "xl"
          },
          {
            "type": "box",
            "layout": "vertical",
            "contents": [
              {
                "type": "box",
                "layout": "baseline",
                "contents": [
                  {
                    "type": "text",
                    "text": "Department",
                    "size": "sm",
                    "color": "#858f89",
                    "flex": 3,
                    "margin": "md"
                  },
                  {
                    "type": "text",
                    "text": "Interface Design 1",
                    "size": "sm",
                    "margin": "md",
                    "flex": 5
                  }
                ]
              },
              {
                "type": "box",
                "layout": "baseline",
                "contents": [
                  {
                    "type": "text",
                    "text": "Phone",
                    "size": "sm",
                    "color": "#858f89",
                    "flex": 3,
                    "margin": "md"
                  },
                  {
                    "type": "text",
                    "text": "333-444-5555",
                    "size": "sm",
                    "margin": "md",
                    "flex": 5
                  }
                ],
                "margin": "sm"
              },
              {
                "type": "box",
                "layout": "baseline",
                "contents": [
                  {
                    "type": "text",
                    "text": "E-mail",
                    "size": "sm",
                    "color": "#858f89",
                    "flex": 3,
                    "margin": "md"
                  },
                  {
                    "type": "text",
                    "text": "email@works.com",
                    "size": "sm",
                    "margin": "md",
                    "flex": 5
                  }
                ],
                "margin": "sm"
              }
            ],
            "margin": "xxl",
            "width": "260px",
            "alignItems": "center",
            "paddingAll": "6px"
          }
        ]
      },
      "footer": {
        "type": "box",
        "layout": "vertical",
        "contents": [
          {
            "type": "button",
            "action": {
              "type": "uri",
              "label": "Send a message",
              "uri": "https://worksmobile.com"
            },
            "style": "primary"
          },
          {
            "type": "button",
            "action": {
              "type": "uri",
              "label": "Call",
              "uri": "https://worksmobile.com"
            },
            "margin": "md",
            "style": "secondary"
          }
        ]
      }
    }
  ]
}

1657526200.png

Flexible Template Simulator を使用すると、実際にメッセージを送信しなくてもレイアウトを確認し、メッセージ送信APIで利用することができます。

quickReply

クイックリプライ (quick reply) は今まで紹介したすべてのメッセージタイプで使用可能です。
使用するには、content パラメータ内に quickReply パラメータを設定します。

quickReply with type: text
"content": {
    "type": "text",
    "text": "quickReply",
    "quickReply": {
        "items": [{
            "action": {
                "type": "postback",
                "label": "Postback",
                "data": "postback data",
                "displayText": "Postback"
            }
        },{
            "action": {
                "type": "message",
                "label": "メッセージ",
                "text": "メッセージ"
            }
        },{
            "imageUrl": "https://developers.worksmobile.com/favicon.png",
            "action": {
                "type": "uri",
                "label": "LINE WORKS",
                "uri": "https://line.worksmobile.com/jp/"
            }
        },{
            "action": {
                "type": "camera",
                "label": "カメラ"
            }
        },{
            "action": {
                "type": "cameraRoll",
                "label": "カメラロール"
            }
        },{
            "action": {
                "type": "location",
                "label": "場所"
            }
        }]
    }
}

1657608061.png

以上です。

4
1
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
4
1