iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤖

Local Development Environment for Node.js and LINE Bot | Node.js + LINE Messaging API

に公開

Overview

A memo on setting up a development environment for LINE Bots using Node.js on a local PC.

Prerequisites

  • Node -v: v20.12.2
  • Windows 11
  • Use VSCode to start the server
  • LINE Channel has already been created
  • Using linebot instead of the official LINE Messaging API SDK

Setup

Installing Dependencies

Initialize npm in the development directory

npm init -y

Install the linebot module

npm i linebot # ^1.6.1

Install the localtunnel module
Allows receiving Webhooks etc. locally for development

npm i localtunnel -D # ^2.0.2

Configuring Startup Commands and More

npm scripts configuration

Register expose in the scripts section of package.json as follows:
Change any-domain to a domain of your choice. It can be used if it is not already taken by someone else.
This allows localhost:3000 to be exposed as https://any-domain.loca.lt.

package.json
"scripts": {
    "expose": "lt --port 3000 --subdomain any-domain --print-requests",
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
},

VSCode Tasks Configuration

Create a directory named .vscode in the project root and create a file named tasks.json within it. The content is as follows:
With this setup, running the task npm: start and expose will execute both the server startup and its public exposure together.

Content of tasks.json
tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "npm: start",
            "type": "shell",
            "command": "npm start",
            "group": "none",
            "presentation": {
                "reveal": "always",
                "panel": "new"
            }
        },
        {
            "label": "npm: expose",
            "type": "shell",
            "command": "npm run expose",
            "group": "none",
            "presentation": {
                "reveal": "always",
                "panel": "new"
            }
        },
        {
            "label": "npm: start and expose",
            "type": "shell",
            "dependsOn": [
                "npm: expose",
                "npm: start"
            ],
            "runOptions": {
                "runOn": "folderOpen"
            }
        }
    ]
}

VSCode Keyboard Shortcut Settings

  1. Press Ctrl + Shift + P in VSCode and type Open Keyboard Shortcuts in the input field that appears. Select Preferences: Open Keyboard Shortcuts (JSON).
  2. Add the following content to the opened keybindings.json:
    keybindings.json
     [
         {
           "key": "Ctrl+Q",
           "command": "workbench.action.tasks.runTask",
           "args": "npm: start and expose",
         }
     ]
    
  3. Save keybindings.json.

Template Creation and Startup

  1. Create index.js in the project root and enter the following content.
    Change CHANNEL_ID, CHANNEL_SECRET, and CHANNEL_ACCESS_TOKEN accordingly, for example, by using environment variables.

    index.js
    // Template from https://github.com/boybundit/linebot
    const linebot = require('linebot');
    
    const bot = linebot({
        channelId: CHANNEL_ID,
        channelSecret: CHANNEL_SECRET,
        channelAccessToken: CHANNEL_ACCESS_TOKEN
    });
    
    bot.on('message', function (event) {
        event.reply(event.message.text).then(function (data) {
            // success
        }).catch(function (error) {
            // error
        });
    });
    
    bot.listen('/linewebhook', 3000);
    
  2. Press Ctrl + Q in VSCode.
    npm start and npm expose will start.
    A URL will be issued, such as your url is: https://any-domain.loca.lt.

  3. Register the previously mentioned URL and path in the Webhook URL settings on LINE Developers.
    For example, if https://any-domain.loca.lt is issued, register https://any-domain.loca.lt/linewebhook as the Webhook URL.

  4. Done.

References

https://github.com/boybundit/linebot
https://github.com/localtunnel/localtunnel
https://developers.line.biz/ja/reference/messaging-api/

Discussion