Showing
1 changed file
with
46 additions
and
0 deletions
app.js
0 → 100644
1 | +const line = require('@line/bot-sdk'); | ||
2 | +const express = require('express'); | ||
3 | + | ||
4 | +// create LINE SDK config from env variables | ||
5 | +const config = { | ||
6 | + channelAccessToken: 'mnny0MJSezgBXzR9C3Ddcc1Csdb7Y9jkvy2nqV5saOmvR2YOJ1/kj/2M0CNsLA+57B2qDpdUQ7WbCTtIKx/LAJ6Kwfop4tX3up7EM8H9EZK1td6GMbhhCb6wvUFVdb1PcTO4joCv8mspd3ubo8a+gAdB04t89/1O/w1cDnyilFU=', | ||
7 | + channelSecret: 'bde77633a16fc5bfbd532d5990c6170e', | ||
8 | +}; | ||
9 | + | ||
10 | +// create LINE SDK client | ||
11 | +const client = new line.Client(config); | ||
12 | + | ||
13 | +// create Express app | ||
14 | +// about Express itself: https://expressjs.com/ | ||
15 | +const app = express(); | ||
16 | + | ||
17 | +// register a webhook handler with middleware | ||
18 | +// about the middleware, please refer to doc | ||
19 | +app.post('/webhook', line.middleware(config), (req, res) => { | ||
20 | + Promise | ||
21 | + .all(req.body.events.map(handleEvent)) | ||
22 | + .then((result) => res.json(result)) | ||
23 | + .catch((err) => { | ||
24 | + console.error(err); | ||
25 | + res.status(500).end(); | ||
26 | + }); | ||
27 | +}); | ||
28 | + | ||
29 | +// event handler | ||
30 | +function handleEvent(event) { | ||
31 | + if (event.type !== 'message' || event.message.type !== 'text') { | ||
32 | + // ignore non-text-message event | ||
33 | + return Promise.resolve(null); | ||
34 | + } | ||
35 | + | ||
36 | + // create a echoing text message | ||
37 | + const echo = { type: 'text', text: event.message.text }; | ||
38 | + | ||
39 | + // use reply API | ||
40 | + return client.replyMessage(event.replyToken, echo); | ||
41 | +} | ||
42 | + | ||
43 | + | ||
44 | +app.listen(3000, function () { | ||
45 | + console.log('Linebot listening on port 3000!'); | ||
46 | +}); |
-
Please register or login to post a comment