EC2 Default User

Add app.js and package.json

1 +var express = require('express');
2 +const request = require('request');
3 +const PUSH_TARGET_URL = 'https://api.line.me/v2/bot/message/push'
4 +const REPLY_TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
5 +const TOKEN = 'Zd+BLpi6wLHMngB3EK74S1W7ApnAXuYZ86xGIi60JKrSW0xI0JyXlCzpunYxk9fxtOkH4y2/CNrb6K7WYldpXBwUkCKNIyEQ04AUpQKQ1EzS6C3qm6y5sBm0zs/Gmzn6n1v1jLfmSpxyLir7VqHk5wdB04t89/1O/w1cDnyilFU='
6 +const USER_ID = 'Uaa6ee8ae309532533aead588d062180d'
7 +const fs = require('fs');
8 +const path = require('path');
9 +const HTTPS = require('https');
10 +const domain = "osschatbot.tk"
11 +const sslport = 23023;
12 +
13 +const bodyParser = require('body-parser');
14 +var app = express();
15 +app.use(bodyParser.json());
16 +
17 +request.post(
18 + {
19 + url: PUSH_TARGET_URL,
20 + headers: {
21 + 'Authorization': `Bearer ${TOKEN}`
22 + },
23 + json: {
24 + "to": `${USER_ID}`,
25 + "messages":[
26 + {
27 + "type":"sticker",
28 + "packageId":"11538",
29 + "stickerId":"51626517"
30 + },
31 + {
32 + "type":"text",
33 + "text":"안녕하세요\n"+
34 + "음식추천 챗봇 쿠밥봇입니다\n\n"+
35 + "원하시는 메뉴를 골라주세요\n\n"+
36 + "1.한식2.중식3.양식\n"+
37 + "4.일식.5.분식6.아시안\n"+
38 + "7.패스트푸드8.학식"
39 + },
40 + {
41 + "type":"text",
42 + "text":"Welcome!!!\n" +
43 + "I'm a food recommendation chatbot, KHUBABBOT\n"+
44 + "Please choose the menu you want\n"+
45 + "1. Korean food 2. Chinese food 3. Western food\n"+
46 + "4. Japanese food 5. Snack food 6. Asian food\n"+
47 + "7. Fast food 8. School food\n"
48 + }
49 + ]
50 + }
51 + },(error, response, body) => {
52 + console.log(body)
53 + });
54 +
55 +app.post('/hook', function (req, res) {
56 +
57 + var eventObj = req.body.events[0];
58 + var source = eventObj.source;
59 + var message = eventObj.message;
60 +
61 + const foodArr = [
62 + {index : 1, name: "한식"},
63 + {index : 2, name: "중식"},
64 + {index : 3, name: "양식"},
65 + {index : 4, name: "일식"},
66 + {index : 5, name: "분식"},
67 + {index : 6, name: "아시안"},
68 + {index: 7, name: "패스트푸드"},
69 + {index: 8, name: "학식"}
70 + ];
71 +
72 + // request log
73 + console.log('======================', new Date() ,'======================');
74 + console.log('[request]', req.body);
75 + console.log('[request source] ', eventObj.source);
76 + console.log('[request message]', eventObj.message);
77 +
78 + var food = foodArr.find(element => element.index == message.text || element.name == message.text);
79 +
80 + console.log(food);
81 +
82 + request.post(
83 + {
84 + url: REPLY_TARGET_URL,
85 + headers: {
86 + 'Authorization': `Bearer ${TOKEN}`
87 + },
88 + json: {
89 + "replyToken":eventObj.replyToken,
90 + "messages":[
91 + {
92 + "type": "location",
93 + "title": "my location",
94 + "address": "1-6-1 Yotsuya, Shinjuku-ku, Tokyo, 160-0004, Japan",
95 + "latitude": 35.687574,
96 + "longitude": 139.72922
97 + }
98 + ]
99 + }
100 + },(error, response, body) => {
101 + console.log(body)
102 + });
103 +
104 + res.sendStatus(200);
105 +});
106 +
107 +try {
108 + const option = {
109 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
110 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
111 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
112 + };
113 +
114 + HTTPS.createServer(option, app).listen(sslport, () => {
115 + console.log(`[HTTPS] Server is started on port ${sslport}`);
116 + });
117 + } catch (error) {
118 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
119 + console.log(error);
120 + }
...\ No newline at end of file ...\ No newline at end of file
1 +{
2 + "dependencies": {
3 + "express": "^4.18.2",
4 + "request": "^2.88.2"
5 + }
6 +}