유명현

Set line-bot sdk, .env

1 -var express = require("express"); 1 +const express = require("express");
2 -var app = express(); 2 +const line = require("@line/bot-sdk");
3 +require("dotenv").config();
4 +console.log(process.env.channelAccessToken, process.env.channelSecret);
5 +const config = {
6 + channelAccessToken: process.env.channelAccessToken,
7 + channelSecret: process.env.channelSecret,
8 +};
3 9
4 -app.get("/", function (req, res) { 10 +const app = express();
5 - res.send("<h1>hello express!</h1>"); 11 +app.post("/webhook", line.middleware(config), (req, res) => {
12 + Promise.all(req.body.events.map(handleEvent)).then((result) =>
13 + res.json(result)
14 + );
6 }); 15 });
7 16
17 +const client = new line.Client(config);
18 +function handleEvent(event) {
19 + if (event.type !== "message" || event.message.type !== "text") {
20 + return Promise.resolve(null);
21 + }
22 + console.log(event);
23 +
24 + // push
25 + client.pushMessage(event.source.userId, {
26 + type: "text",
27 + text: "hihihi",
28 + });
29 + return client.replyMessage(event.replyToken, {
30 + type: "text",
31 + text: event.message.text,
32 + });
33 +}
34 +
8 app.listen(3000); 35 app.listen(3000);
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
7 "": { 7 "": {
8 "name": "secondhand-noti-bot", 8 "name": "secondhand-noti-bot",
9 "version": "1.0.0", 9 "version": "1.0.0",
10 - "license": "ISC", 10 + "license": "MIT",
11 "dependencies": { 11 "dependencies": {
12 "express": "^4.18.1", 12 "express": "^4.18.1",
13 "nodemon": "^2.0.16" 13 "nodemon": "^2.0.16"
......