### Configure Start Script in package.json Source: https://developers.line.biz/ja/docs/messaging-api/nodejs-sample Define the `start` script in `package.json` to specify the entry point for the server, typically `index.js`. ```json { "name": "sample-app", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" } ``` -------------------------------- ### Install Express.js Source: https://developers.line.biz/ja/docs/messaging-api/nodejs-sample/index.html.md Install the Express.js framework, a lightweight Node.js web server framework, using npm. ```bash npm install express ``` -------------------------------- ### Configure Quick Reply Buttons in a Text Message Source: https://developers.line.biz/ja/docs/messaging-api/using-quick-reply This example demonstrates how to set up a text message with Quick Reply buttons. It includes buttons for selecting food categories with message actions and a button to send the user's location using a location action. Use this when you want to guide user input with predefined options. ```json { "type": "text", "text": "Select your favorite food category or send me your location!", "quickReply": { "items": [ { "type": "action", "imageUrl": "https://example.com/sushi.png", "action": { "type": "message", "label": "Sushi", "text": "Sushi" } }, { "type": "action", "imageUrl": "https://example.com/tempura.png", "action": { "type": "message", "label": "Tempura", "text": "Tempura" } }, { "type": "action", "action": { "type": "location", "label": "Send location" } } ] } } ``` -------------------------------- ### URI Action Example Source: https://developers.line.biz/ja/docs/messaging-api/actions This example demonstrates how to use URI actions within quick reply buttons to open a menu, initiate a phone call, or share a LINE official account. ```APIDOC ## URI Action Opens a specified URL in the LINE in-app browser. Can also be used with LINE URL schemes to open the dialer or share a LINE official account. ### Request Example ```json { "messages": [ { "type": "text", "text": "ご注文はお決まりですか?", "quickReply": { "items": [ { "type": "action", "action": { "type": "uri", "label": "メニューを見る", "uri": "https://example.com/menu" } }, { "type": "action", "action": { "type": "uri", "label": "電話注文", "uri": "tel:09001234567" } }, { "type": "action", "action": { "type": "uri", "label": "友だちに勧める", "uri": "https://line.me/R/nv/recommendOA/%40linedevelopers" } } ] } } ] } ``` ``` -------------------------------- ### URI Action Example for Quick Reply Buttons Source: https://developers.line.biz/ja/docs/messaging-api/actions This JSON snippet demonstrates how to set up a URI action for quick reply buttons. It includes examples for opening a menu, initiating a phone call, and sharing a LINE official account. ```json { "messages": [ { "type": "text", "text": "ご注文はお決まりですか?", "quickReply": { "items": [ { "type": "action", "action": { "type": "uri", "label": "メニューを見る", "uri": "https://example.com/menu" } }, { "type": "action", "action": { "type": "uri", "label": "電話注文", "uri": "tel:09001234567" } }, { "type": "action", "action": { "type": "uri", "label": "友だちに勧める", "uri": "https://line.me/R/nv/recommendOA/%40linedevelopers" } } ] } } ] } ``` -------------------------------- ### Configure npm Start Script Source: https://developers.line.biz/ja/docs/messaging-api/nodejs-sample/index.html.md Update the `package.json` file to define the start script for your application. This tells Heroku which file to use to start the server. ```json { "name": "sample-app", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" } ``` -------------------------------- ### Example Private Key (jwx) Source: https://developers.line.biz/ja/docs/messaging-api/generate-json-web-token/index.html.md This is an example of a generated private key in JSON format, including algorithm, key type, and usage. ```json { "alg": "RS256", "d": "JeSJWnvZ......", "dp": "gBDRXGg7......", "dq": "MjFJ4xM9......", "e": "AQ......", "kty": "RSA", "n": "pTS2jGso......", "p": "xQibzkW6......", "q": "1qWtyQ9s......", "qi": "sdVGblc......", "use": "sig" } ``` -------------------------------- ### Initialize Node.js Project Source: https://developers.line.biz/ja/docs/messaging-api/nodejs-sample Use `npm init -y` to create a `package.json` file, skipping all setup questions. ```sh npm init -y ``` -------------------------------- ### Install JWCrypto Library (Python) Source: https://developers.line.biz/ja/docs/messaging-api/generate-json-web-token Install the JWCrypto library, an open-source Python library for JWT implementation. This requires Python 3 and pip to be installed. ```python $ pip install jwcrypto ``` -------------------------------- ### Install jwx Command-Line Tool (Go) Source: https://developers.line.biz/ja/docs/messaging-api/generate-json-web-token Install the jwx command-line tool, a part of the jwx Go library for JWT implementation. Ensure you have a Go development environment set up. ```sh $ git clone https://github.com/lestrrat-go/jwx.git $ cd jwx $ make jwx ``` -------------------------------- ### Start Server Listener Source: https://developers.line.biz/ja/docs/messaging-api/nodejs-sample Configure the server to listen on the specified `PORT` using `app.listen()`, defaulting to 3000. ```javascript app.listen(PORT, () => { console.log(`Example app listening at http://localhost:${PORT}`); }); ``` -------------------------------- ### Clipboard Action Example Source: https://developers.line.biz/ja/docs/messaging-api/actions This example shows how to implement a clipboard action in a message template, allowing users to copy text, such as a coupon code, to their clipboard with a single tap. ```APIDOC ## Clipboard Action Copies specified text to the device's clipboard when the user taps the associated control. ### Request Example ```json { "messages": [ { "type": "template", "altText": "クーポンコードをお送りします。", "template": { "type": "buttons", "thumbnailImageUrl": "{your coupon image}", "imageAspectRatio": "rectangle", "imageSize": "cover", "imageBackgroundColor": "#FFFFFF", "title": "限定クーポン配布中!", "text": "有効期限:2024年2月末日\nクーポンコード(3B48740B)を下記のボタンからコピーしてお使いください。", "actions": [ { "type": "clipboard", "label": "コピー", "clipboardText": "3B48740B" } ] } } ] } ``` ``` -------------------------------- ### Example Public Key (jwx) Source: https://developers.line.biz/ja/docs/messaging-api/generate-json-web-token/index.html.md This is an example of a generated public key in JSON format, including algorithm, key type, and usage. ```json { "alg": "RS256", "e": "AQ......", "kty": "RSA", "n": "pTS2jGso......", "use": "sig" } ``` -------------------------------- ### Example Private Key (JWK Format) Source: https://developers.line.biz/ja/docs/messaging-api/generate-json-web-token This is an example of the private key generated in JWK format. It contains all necessary components for signing operations. ```json { "alg": "RS256", "d": "GaDzOmc4......", "dp": "WAByrYmh......", "dq": "WLwjYun0......", "e": "AQ......", "ext": true, "key_ops": [ "sign" ], "kty": "RSA", "n": "vsbOUoFA......", "p": "5QJitCu9......", "q": "1ULfGui5......", "qi": "2cK4apee......" } ``` -------------------------------- ### Example Public Key (JWK Format) Source: https://developers.line.biz/ja/docs/messaging-api/generate-json-web-token This is an example of the public key generated in JWK format. It contains the necessary components for verification operations. ```json { "alg": "RS256", "e": "AQ......", "ext": true, "key_ops": [ "verify" ], "kty": "RSA", "n": "vsbOUoFA......" } ``` -------------------------------- ### Set up Root Route Source: https://developers.line.biz/ja/docs/messaging-api/nodejs-sample Implement a GET route for the root path (`/`) to return a status of 200, useful for health checks. ```javascript app.get("/", (req, res) => { res.sendStatus(200); }); ``` -------------------------------- ### Linear Gradient with Intermediate Color (10% position) Source: https://developers.line.biz/ja/docs/messaging-api/flex-message-layout Adds an intermediate color to a linear gradient background. The `centerColor` property defines the intermediate color, and `centerPosition` sets its location within the gradient. This example places the intermediate color at 10% from the start. ```json { "type": "bubble", "body": { "type": "box", "layout": "vertical", "contents": [], "background": { "type": "linearGradient", "angle": "0deg", "startColor": "#ff0000", "centerColor": "#0000ff", "endColor": "#00ff00", "centerPosition": "10%" }, "height": "200px" } } ``` -------------------------------- ### Create and Initialize Heroku App Source: https://developers.line.biz/ja/docs/messaging-api/nodejs-sample/index.html.md Create a new directory for your project, navigate into it, initialize Git, and create a new app on Heroku. Replace `{Name of your app}` with a unique name. ```bash mkdir sample-app cd sample-app git init heroku create {Name of your app} ``` -------------------------------- ### Install jwcrypto library Source: https://developers.line.biz/ja/docs/messaging-api/generate-json-web-token/index.html.md Install the jwcrypto library using pip. This library is required for cryptographic operations in Python. ```bash $ pip install jwcrypto ``` -------------------------------- ### Initialize npm Project Source: https://developers.line.biz/ja/docs/messaging-api/nodejs-sample/index.html.md Initialize a new npm project with default settings. This creates a `package.json` file to manage project metadata and dependencies. ```bash npm init -y ``` -------------------------------- ### Example JWT Structure Source: https://developers.line.biz/ja/docs/messaging-api/generate-json-web-token This is an example of a generated JWT. It consists of a Base64-encoded header, claims set, and a signature, separated by dots. ```text eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ijk4NjllNDQ2LTM0ODktNDUxNi1hODNmLWVjOTIxNGFkOTRkMCJ9.eyJpc3MiOiIxMjM0NTY3ODkwIiwic3ViIjoiMTIzNDU2Nzg5MCIsImF1ZCI6Imh0dHBzOi8vYXBpLmxpbmUubWUvIiwiZXhwIjoxNjIzOTk1NTk5LCJ0b2tlbl9leHAiOjI1OTIwMDB9.Zf32xTqgUHSYw2C2Mlmunqz_AtkaqvGh0msx9XJMX6QYLPT4m4QYF3PsER-zfbhbByNT4rH09JEMRP7bzcNMQ8l4n_WXwTyLkNciZYzF-sTiVHiZu4ucJm4_l8ni5NaqOVEntsCp1wQi8-VLjaMpQlQ7crCdouEMFFeyVwgERfH8ui6UZaJeIlJKRZTnO6iYvKYuLyUsqzowfwZo0hcnnZIXKnjZ81ukjH3_78EHXOD5ivovAT7CtmBoglm3Bvsi0N6PlEONLhHqpCleaYTXRmCykxDLP600JRvi5TYApaN-8n2Bo3FskXJLuxquWLP-LTfMDlkakmfEfcQCiz7daQ ``` -------------------------------- ### Create index.js File Source: https://developers.line.biz/ja/docs/messaging-api/nodejs-sample/index.html.md Create the main server file `index.js` for your Node.js application. ```bash touch index.js ``` -------------------------------- ### JWT Header Example Source: https://developers.line.biz/ja/docs/messaging-api/generate-json-web-token The header contains information about the JWT, including the algorithm used for signing, the token type, and a key ID. This is a decoded example of the header. ```json { "alg": "RS256", "typ": "JWT", "kid": "536e453c-aa93-4449-8e90-add2608783c6" } ``` -------------------------------- ### Example JWT Structure Source: https://developers.line.biz/ja/docs/messaging-api/generate-json-web-token/index.html.md This is an example of a JWT generated using the RS256 algorithm. It consists of a Base64 encoded header, claims set, and a signature, separated by dots. ```sh eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjJjNjU4NWYzLThkZGQtNDZjNC05YmUyLWI1NGE3MGFhOTRlYSJ9.eyJpc3MiOiIxNjUzOTQ3MTcyIiwic3ViIjoiMTY1Mzk0NzE3MiIsImF1ZCI6Imh0dHBzOi8vYXBpLmxpbmUubWUvIiwiZXhwIjoiMTU4NTIwMDA2MiIsInRva2VuX2V4cCI6IjI1OTIwMDAifQ.UVG6PAEub-OPbZ3nJuVxRRPjY6Sz_eIHJV9DTTAHCR79YsG4yWvoa9AeIctibb6IJQKgTEV7mF7LsUDmXldEDqYwyEmKs38zj_995Ntc9SYBFphHpr09NqfMoqMphwKqms2NOnqgcHreFs27d9Q0Qv8Rtv2t7SB2cVO__KrsjzYNs3miTvDdkqYLXFo5fXwuzNtHOCAJomd6bhMR8Yd1-vJmtMCBPK4hmA98w8fG_NhcyLbw-B9AuxQ6z92zXiRhNyPlK_3ce2T7HtgUluJ4xJl4xdLJ_C6hvTAqtQxmSiJKzbjUiANF6hVBTomU8vkaIjEKjnlT1uPMihfrsA3pzQ ``` -------------------------------- ### JSONでFlex Messageを定義する Source: https://developers.line.biz/ja/docs/messaging-api/using-flex-messages 「Hello, World!」メッセージを作成するための基本的なFlex MessageのJSON構造です。単一のメッセージバブルコンテナを使用し、水平レイアウトのボックス内に2つのテキストコンポーネントを配置します。 ```json { "type": "bubble", "body": { "type": "box", "layout": "horizontal", "contents": [ { "type": "text", "text": "Hello," }, { "type": "text", "text": "World!" } ] } } ``` -------------------------------- ### Get a List of Created Coupons Source: https://developers.line.biz/ja/docs/messaging-api/send-coupons-to-users Retrieve a list of all created coupons, including their IDs and titles. This list can be filtered by status to get only active or closed coupons. ```curl curl -v -X GET https://api.line.me/v2/bot/coupon \ -H 'Authorization: Bearer {channel access token}' ``` ```json { "items": [ { "couponId": "01JZMWQ9HMDW9ENJP4C167CXP8", "title": "年末年始クーポン" }, { "couponId": "01JZA9NPPFDJ3RFG8NA9DJ0NQT", "title": "友だち限定クーポン" } ] } ``` -------------------------------- ### Example JWT Source: https://developers.line.biz/ja/docs/messaging-api/generate-json-web-token This is an example of a fully formed JWT, which is a Base64 encoded string representing the signed header and claims. It is generated using a private key and a specified algorithm. ```sh eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjJjNjU4NWYzLThkZGQtNDZjNC05YmUyLWI1NGE3MGFhOTRlYSJ9.eyJpc3MiOiIxNjUzOTQ3MTcyIiwic3ViIjoiMTY1Mzk0NzE3MiIsImF1ZCI6Imh0dHBzOi8vYXBpLmxpbmUubWUvIiwiZXhwIjoiMTU4NTIwMDA2MiIsInRva2VuX2V4cCI6IjI1OTIwMDAifQ.UVG6PAEub-OPbZ3nJuVxRRPjY6Sz_eIHJV9DTTAHCR79YsG4yWvoa9AeIctibb6IJQKgTEV7mF7LsUDmXldEDqYwyEmKs38zj_995Ntc9SYBFphHpr09NqfMoqMphwKqms2NOnqgcHreFs27d9Q0Qv8Rtv2t7SB2cVO__KrsjzYNs3miTvDdkqYLXFo5fXwuzNtHOCAJomd6bhMR8Yd1-vJmtMCBPK4hmA98w8fG_NhcyLbw-B9AuxQ6z92zXiRhNyPlK_3ce2T7HtgUluJ4xJl4xdLJ_C6hvTAqtQxmSiJKzbjUiANF6hVBTomU8vkaIjEKjnlT1uPMihfrsA3pzQ ``` -------------------------------- ### Create Rich Menu B with Switching and URI Actions Source: https://developers.line.biz/ja/docs/messaging-api/switch-rich-menus/index.html.md Use this to create a rich menu with specific tap areas. The left area switches to another rich menu alias, and the right area opens a URI. Ensure the 'richmenu-alias-a' exists before using this. ```sh curl -v -X POST https://api.line.me/v2/bot/richmenu \ -H 'Authorization: Bearer {channel access token}' \ -H 'Content-Type: application/json' \ -d \ '{ \ "size": { \ "width": 2500, \ "height": 1686 \ }, \ "selected": false, \ "name": "richmenu-b", \ "chatBarText": "Tap to open", \ "areas": [ \ { \ "bounds": { \ "x": 0, \ "y": 0, \ "width": 1250, \ "height": 1686 \ }, \ "action": { \ "type": "richmenuswitch", \ "richMenuAliasId": "richmenu-alias-a", \ "data": "richmenu-changed-to-a" \ } \ }, \ { \ "bounds": { \ "x": 1251, \ "y": 0, \ "width": 1250, \ "height": 1686 \ }, \ "action": { \ "type": "uri", \ "uri": "https://techblog.lycorp.co.jp/" \ } \ } \ ] \ }' ``` -------------------------------- ### Configure Port and Token Environment Variables Source: https://developers.line.biz/ja/docs/messaging-api/nodejs-sample Set up environment variables for the server port (`PORT`) and the LINE channel access token (`LINE_ACCESS_TOKEN`) in `index.js`. ```javascript const PORT = process.env.PORT || 3000; const TOKEN = process.env.LINE_ACCESS_TOKEN; ``` -------------------------------- ### Create Rich Menu B Source: https://developers.line.biz/ja/docs/messaging-api/switch-rich-menus/index.html.md Creates a new rich menu with specified size, name, and interactive areas. This example demonstrates setting up areas for switching to another rich menu and opening a URI. ```APIDOC ## POST /v2/bot/richmenu ### Description Creates a rich menu. ### Method POST ### Endpoint https://api.line.me/v2/bot/richmenu ### Request Body - **size** (object) - Required - Dimensions of the rich menu. - **width** (integer) - Required - Width of the rich menu. - **height** (integer) - Required - Height of the rich menu. - **selected** (boolean) - Required - Whether the rich menu is selected by default. - **name** (string) - Required - Name of the rich menu. - **chatBarText** (string) - Required - Text displayed on the chat bar. - **areas** (array) - Required - Tappable areas within the rich menu. - **bounds** (object) - Required - Defines the boundaries of a tappable area. - **x** (integer) - Required - X coordinate of the top-left corner. - **y** (integer) - Required - Y coordinate of the top-left corner. - **width** (integer) - Required - Width of the area. - **height** (integer) - Required - Height of the area. - **action** (object) - Required - Action to perform when the area is tapped. - **type** (string) - Required - Type of action (e.g., `richmenuswitch`, `uri`). - **richMenuAliasId** (string) - Optional - Alias ID of the rich menu to switch to (for `richmenuswitch` action). - **data** (string) - Optional - Data to send with the action (for `richmenuswitch` action). - **uri** (string) - Optional - URI to open (for `uri` action). ### Request Example ```json { "size": { "width": 2500, "height": 1686 }, "selected": false, "name": "richmenu-b", "chatBarText": "Tap to open", "areas": [ { "bounds": { "x": 0, "y": 0, "width": 1250, "height": 1686 }, "action": { "type": "richmenuswitch", "richMenuAliasId": "richmenu-alias-a", "data": "richmenu-changed-to-a" } }, { "bounds": { "x": 1251, "y": 0, "width": 1250, "height": 1686 }, "action": { "type": "uri", "uri": "https://techblog.lycorp.co.jp/" } } ] } ``` ### Response #### Success Response (200) - **richMenuId** (string) - The ID of the created rich menu. ``` -------------------------------- ### Postback Action to Open Keyboard with FillInText Source: https://developers.line.biz/ja/docs/messaging-api/try-rich-menu/index.html.md Use this to open the keyboard and pre-fill it with specified text, useful for forms or structured input. ```json {"type":"postback","label":"キーボードを開くフィルインテキストあり","data":"actionId=","inputOption":"openKeyboard","fillInText":"---\予約番号: \予約メニュー番号: \n予約日時: \n---"} ```