조아혜

add chatbot.js

1 +var express = require('express');
2 +const request = require('request');
3 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
4 +const TOKEN = 'w5i8sURqF5bof6DWeB87n+oCeWrYaFf7a5YZzfzN1jeITIlZ3PcOmZRcdGCo/djTuHhNxybfJ69y7Jex+7tipBNRynngfyWX9CK1L3EupuhnX8rubeCmJda7HvsQWXVo8ZDcwl2aLwXsE3kiYF2qEwdB04t89/1O/w1cDnyilFU='
5 +const fs = require('fs');
6 +const path = require('path');
7 +const HTTPS = require('https');
8 +const domain = "www.osstest237.ml"
9 +const sslport = 23023;
10 +
11 +const bodyParser = require('body-parser');
12 +var app = express();
13 +app.use(bodyParser.json());
14 +
15 +var usingMessage = ''
16 +
17 +app.post('/hook', function (req, res) {
18 +
19 + var eventObj = req.body.events[0];
20 + // var source = eventObj.source;
21 + // var message = eventObj.message;
22 + var text = eventObj.message.text;
23 +
24 + // request log
25 + console.log('======================', new Date() ,'======================');
26 + console.log('[request]', req.body);
27 + console.log('[request source] ', eventObj.source);
28 + console.log('[request message]', eventObj.message);
29 +
30 + if (text == 'Cfr:Yes') {
31 + QuickReplyCfrYes(eventObj.replyToken);
32 + res.sendStatus(200);
33 + } else if (text == 'Cfr:No') {
34 + QuickReplyCfrNo(eventObj.replyToken);
35 + res.sendStatus(200);
36 + } else if (eventObj.message.type == 'image') {
37 + console.log('image url: ', eventObj.message.contentProvider);
38 + console.log('image url: ', eventObj.message.originalContentUrl);
39 + console.log('image url: ', eventObj.message.previewImageUrl);
40 + console.log('id:', eventObj.message.id);
41 + res.sendStatus(200);
42 + }
43 + else {
44 + usingMessage = text;
45 + request.post(
46 + {
47 + url: TARGET_URL,
48 + headers: {
49 + 'Authorization': `Bearer ${TOKEN}`
50 + },
51 + json: {
52 + "replyToken":eventObj.replyToken,
53 + "messages":[
54 + {
55 + "type": "sticker",
56 + "packageId": "11537",
57 + "stickerId": "52002738"
58 + },
59 + {
60 + "type": "text",
61 + "text": "안녕하세요.\nCFR을 이용한 경기도 맛집 추천봇입니다."
62 + },
63 + {
64 + "type": "template",
65 + "altText": "얼굴 인식을 위해 사진을 가져오시겠습니까?",
66 + "template": {
67 + "type": "confirm",
68 + "text": "얼굴 인식을 위해 사진을 가져오시겠습니까?",
69 + "actions": [
70 + {
71 + "type": "message",
72 + "label": "Cfr:Yes",
73 + "text": "Cfr:Yes"
74 + },
75 + {
76 + "type": "message",
77 + "label": "Cfr:No",
78 + "text": "Cfr:No"
79 + }
80 + ]
81 + }
82 + },
83 + ]
84 + }
85 + },(error, response, body) => {
86 + console.log(body)
87 + });
88 + res.sendStatus(200);
89 + }
90 +
91 +});
92 +
93 +const quickReplyYes = {
94 + "items": [
95 + {
96 + "type": "action",
97 + "action": {
98 + "type": "cameraRoll",
99 + "label": "사진 가져오기"
100 + }
101 + },
102 + {
103 + "type": "action",
104 + "action": {
105 + "type": "camera",
106 + "label": "사진 찍기"
107 + }
108 + },
109 + {
110 + "type": "action",
111 + "action": {
112 + "type": "location",
113 + "label": "위치"
114 + }
115 + }
116 + ]
117 + };
118 +
119 +function QuickReplyCfrYes(replyToken) {
120 + request.post(
121 + {
122 + url: TARGET_URL,
123 + headers: {
124 + 'Authorization': `Bearer ${TOKEN}`
125 + },
126 + json: {
127 + "replyToken": replyToken,
128 + "messages": [
129 + {
130 + "type": "text",
131 + "text": "사진 입력",
132 + "quickReply": quickReplyYes
133 + }
134 + ]
135 + }
136 + },(error, response, body) => {
137 + console.log(body)
138 + });
139 +}
140 +
141 +const quickReplyNo = {
142 + items: [
143 + {
144 + "type": "action",
145 + "action": {
146 + "type": "message",
147 + "label": "weather",
148 + "text": "날씨"
149 + }
150 + },
151 + {
152 + "type": "action",
153 + "action": {
154 + "type": "message",
155 + "label": "menu",
156 + "text": "메뉴"
157 + }
158 + },
159 + {
160 + "type": "action",
161 + "action": {
162 + "type": "message",
163 + "label": "random",
164 + "text": "랜덤"
165 + }
166 + }
167 + ]
168 + };
169 +
170 +function QuickReplyCfrNo (replyToken) {
171 + request.post(
172 + {
173 + url: TARGET_URL,
174 + headers: {
175 + 'Authorization': `Bearer ${TOKEN}`
176 + },
177 + json: {
178 + "replyToken": replyToken,
179 + "messages": [
180 + {
181 + "type": "text",
182 + "text": "맛집 추천 옵션 선택",
183 + "quickReply": quickReplyNo
184 + }
185 + ]
186 + }
187 + },(error, response, body) => {
188 + console.log(body)
189 + });
190 +}
191 +
192 +try {
193 + const option = {
194 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
195 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
196 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
197 + };
198 + if (usingMessage == '') {
199 + HTTPS.createServer(option, app).listen(sslport, () => {
200 + console.log(`[HTTPS] Server is started on port ${sslport}`);
201 + });
202 + }
203 + } catch (error) {
204 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
205 + console.log(error);
206 + }
...\ No newline at end of file ...\ No newline at end of file
This diff is collapsed. Click to expand it.
1 +{
2 + "name": "reply",
3 + "version": "1.0.0",
4 + "description": "",
5 + "main": "chatbot.js",
6 + "scripts": {
7 + "test": "echo \"Error: no test specified\" && exit 1"
8 + },
9 + "author": "",
10 + "license": "ISC",
11 + "dependencies": {
12 + "body-parser": "^1.19.0",
13 + "express": "^4.17.1",
14 + "request": "^2.88.2"
15 + }
16 +}