Created
June 18, 2017 16:30
-
-
Save gie3d/03fbecf08a837c889734068a57fc2b22 to your computer and use it in GitHub Desktop.
Sample LINE Bot using node.js express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const line = require('@line/bot-sdk'); | |
require('dotenv').config(); | |
const app = express(); | |
const config = { | |
channelAccessToken: process.env.channelAccessToken, | |
channelSecret: process.env.channelSecret | |
}; | |
const client = new line.Client(config); | |
app.post('/webhook', line.middleware(config), (req, res) => { | |
Promise | |
.all(req.body.events.map(handleEvent)) | |
.then((result) => res.json(result)); | |
}); | |
function handleEvent(event) { | |
console.log(event); | |
if (event.type === 'message' && event.message.type === 'text') { | |
handleMessageEvent(event); | |
} else { | |
return Promise.resolve(null); | |
} | |
} | |
function handleMessageEvent(event) { | |
var msg = { | |
type: 'text', | |
text: 'สวัสดีครัช' | |
}; | |
return client.replyMessage(event.replyToken, msg); | |
} | |
app.set('port', (process.env.PORT || 5000)); | |
app.listen(app.get('port'), function () { | |
console.log('run at port', app.get('port')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment