iTranslated by AI
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.
"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
{
"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
- Press
Ctrl + Shift + Pin VSCode and typeOpen Keyboard Shortcutsin the input field that appears. SelectPreferences: Open Keyboard Shortcuts (JSON). - Add the following content to the opened
keybindings.json:keybindings.json[ { "key": "Ctrl+Q", "command": "workbench.action.tasks.runTask", "args": "npm: start and expose", } ] - Save
keybindings.json.
Template Creation and Startup
-
Create
index.jsin the project root and enter the following content.
ChangeCHANNEL_ID,CHANNEL_SECRET, andCHANNEL_ACCESS_TOKENaccordingly, 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); -
Press
Ctrl + Qin VSCode.
npm startandnpm exposewill start.
A URL will be issued, such asyour url is: https://any-domain.loca.lt. -
Register the previously mentioned URL and path in the Webhook URL settings on LINE Developers.
For example, ifhttps://any-domain.loca.ltis issued, registerhttps://any-domain.loca.lt/linewebhookas the Webhook URL. -
Done.
References
Discussion