node-line-bot-api is a node.js SDK for LINE Bot API.
LINE Bot API Docs https://devdocs.line.me/
npm install node-line-bot-api --save
You can setup auth data with the following samples
- Use Server Key
const line = require('node-line-bot-api')
line.init({
accessToken: '{YOUR_ACCESS_TOKEN}',
// (Optional) for webhook signature validation
channelSecret: '{YOUR_CHANNEL_SECRET}'
})
'use strict'
const line = require('node-line-bot-api')
// init with auth
line.init({
accessToken: '{YOUR_ACCESS_TOKEN}',
// (Optional) for webhook signature validation
channelSecret: '{YOUR_CHANNEL_SECRET}'
})
// simple push message with user MID
line.client
.pushMessage({
to: '{YOUR_MID}',
messages:[
{
"type":"text",
"text":"Hello, world1"
},
{
"type":"text",
"text":"Hello, world2"
}
]
})
'use strict'
const line = require('node-line-bot-api')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
// need raw buffer for signature validation
app.use(bodyParser.json({
verify (req, res, buf) {
req.rawBody = buf
}
}))
// init with auth
line.init({
accessToken: 'YOUR_ACCESS_TOKEN',
// (Optional) for webhook signature validation
channelSecret: 'YOUR_CHANNEL_SECRET'
})
app.post('/webhook/', line.validator.validateSignature(), (req, res, next) => {
// get content from request body
const promises = req.body.events.map(event => {
// reply message
return line.client
.replyMessage({
replyToken: event.replyToken,
messages: [
{
type: 'text',
text: event.message.text
}
]
})
})
Promise
.all(promises)
.then(() => res.json({success: true}))
})
app.listen(process.env.PORT || 3000, () => {
console.log('Example app listening on port 3000!')
})
app.post('/webhook/', line.validator.validateSignature(), (req, res, next) => {
const promises = req.body.events.map(event => {
if (event.type === 'follow') {
// do something when your bot account is added as a friend
return Promise.resolve()
} else if (event.type === 'message') {
// do somthing when you get a message from followers
return Promise.resolve()
} else {
// other handling see the details in official docs
// https://devdocs.line.me/ja/#webhook-event-object
return Promise.resolve()
}
})
Promise
.all(promises)
.then(() => res.json({success: true}))
})
line.client
.getMessageContent('xxxxxxxxxx' /* messageId */)
.then((content) => {
// handle content
})
line.client
.getProfile('xxxxxxxxxx' /* userId */)
.then((profile) => {
// handle profile
/**
* {
* "displayName": "hoge hoge",
* "userId": "xxxxxxxxxx",
* "pictureUrl": "http://dl.profile.line-cdn.net/xxxxxxxxxx",
* "statusMessage": "fuga"
* }
*/
})
line.client.leaveGroup('xxxxxxxxxx' /* groupId */)
line.client.leaveRoom('xxxxxxxxxx' /* roomId */)
Recommended node version is above v4.0.0 because this module is implemented with ES6.
Fork the repository and create a PR to 'develop' branch.