Created
June 18, 2017 17:08
-
-
Save gie3d/d0530525fdf670c556023a98ea7f8c8f to your computer and use it in GitHub Desktop.
Sample LINE Bot SDK node.js with more text events
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: 'สวัสดีครัช' | |
}; | |
var eventText = event.message.text.toLowerCase(); | |
if (eventText === 'image') { | |
msg = { | |
'type': 'image', | |
'originalContentUrl': 'https://www.thesun.co.uk/wp-content/uploads/2017/03/fifa-17-2.jpg?strip=all&w=742&quality=100', | |
'previewImageUrl': 'https://images.performgroup.com/di/library/GOAL/a6/bb/fifa-18-ronaldo_lx3r88bpjpk91re36ukdgomrj.jpg?t=2027563652&w=620&h=430' | |
} | |
} else if (eventText === 'location') { | |
msg = { | |
"type": "location", | |
"title": "my location", | |
"address": "〒150-0002 東京都渋谷区渋谷2丁目21−1", | |
"latitude": 35.65910807942215, | |
"longitude": 139.70372892916203 | |
} | |
} else if (eventText === 'template button') { | |
msg = { | |
"type": "template", | |
"altText": "this is a buttons template", | |
"template": { | |
"type": "buttons", | |
"thumbnailImageUrl": "https://www.thesun.co.uk/wp-content/uploads/2017/03/fifa-17-2.jpg?strip=all&w=742&quality=100", | |
"title": "Menu", | |
"text": "Please select", | |
"actions": [{ | |
"type": "postback", | |
"label": "Buy", | |
"data": "action=buy&itemid=123" | |
}, { | |
"type": "postback", | |
"label": "Add to cart", | |
"data": "action=add&itemid=123" | |
}, { | |
"type": "uri", | |
"label": "View detail", | |
"uri": "http://example.com/page/123" | |
}] | |
} | |
} | |
} else if (eventText === 'template confirm') { | |
msg = { | |
"type": "template", | |
"altText": "this is a confirm template", | |
"template": { | |
"type": "confirm", | |
"text": "Are you sure?", | |
"actions": [{ | |
"type": "message", | |
"label": "Yes", | |
"text": "yes" | |
}, { | |
"type": "message", | |
"label": "No", | |
"text": "no" | |
}] | |
} | |
} | |
} else if (eventText === 'carousel') { | |
msg = { | |
"type": "template", | |
"altText": "this is a carousel template", | |
"template": { | |
"type": "carousel", | |
"columns": [ | |
{ | |
"thumbnailImageUrl": "https://www.thesun.co.uk/wp-content/uploads/2017/03/fifa-17-2.jpg?strip=all&w=742&quality=100", | |
"title": "this is menu", | |
"text": "description", | |
"actions": [ | |
{ | |
"type": "postback", | |
"label": "Buy", | |
"data": "action=buy&itemid=111" | |
}, | |
{ | |
"type": "postback", | |
"label": "Add to cart", | |
"data": "action=add&itemid=111" | |
}, | |
{ | |
"type": "uri", | |
"label": "View detail", | |
"uri": "http://example.com/page/111" | |
} | |
] | |
}, | |
{ | |
"thumbnailImageUrl": "https://www.thesun.co.uk/wp-content/uploads/2017/03/fifa-17-2.jpg?strip=all&w=742&quality=100", | |
"title": "this is menu", | |
"text": "description", | |
"actions": [ | |
{ | |
"type": "postback", | |
"label": "Buy", | |
"data": "action=buy&itemid=222" | |
}, | |
{ | |
"type": "postback", | |
"label": "Add to cart", | |
"data": "action=add&itemid=222" | |
}, | |
{ | |
"type": "uri", | |
"label": "View detail", | |
"uri": "http://example.com/page/222" | |
} | |
] | |
} | |
] | |
} | |
} | |
} | |
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