김연우

read_csv to main

Showing 1 changed file with 175 additions and 14 deletions
...@@ -7,10 +7,10 @@ const path = require('path'); ...@@ -7,10 +7,10 @@ const path = require('path');
7 const HTTPS = require('https'); 7 const HTTPS = require('https');
8 const bodyParser = require('body-parser'); 8 const bodyParser = require('body-parser');
9 9
10 -var latitude = 37.2429832; 10 +// var latitude = 37.2429832;
11 -var longitude = 127.0749535; 11 +// var longitude = 127.0749535;
12 -var locationAdd = "경기 용인시 기흥구 서그내로49번길 13" 12 +// var locationAdd = "경기 용인시 기흥구 서그내로49번길 13"
13 -var location_name = "카페 서천" 13 +// var location_name = "카페 서천"
14 14
15 15
16 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 16 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
...@@ -21,6 +21,11 @@ const TOKEN = tokens.channel ...@@ -21,6 +21,11 @@ const TOKEN = tokens.channel
21 const id = tokens.id 21 const id = tokens.id
22 const pw = tokens.pw 22 const pw = tokens.pw
23 23
24 +var first = false; //첫 시도인지
25 +var second = false; //첫번째 분류 선택했는지
26 +var destCar = "";
27 +var destination = [];
28 +
24 function sendText(replyToken, messages) { 29 function sendText(replyToken, messages) {
25 request.post( 30 request.post(
26 { 31 {
...@@ -75,8 +80,8 @@ app.use(bodyParser.json()); ...@@ -75,8 +80,8 @@ app.use(bodyParser.json());
75 app.post('/hook', function (req, res) { 80 app.post('/hook', function (req, res) {
76 81
77 var eventObj = req.body.events[0]; 82 var eventObj = req.body.events[0];
78 - // var source = eventObj.source; 83 + var source = eventObj.source;
79 - // var message = eventObj.message; 84 + var message = eventObj.message;
80 85
81 // request log 86 // request log
82 console.log('======================', new Date(), '======================'); 87 console.log('======================', new Date(), '======================');
...@@ -84,20 +89,118 @@ app.post('/hook', function (req, res) { ...@@ -84,20 +89,118 @@ app.post('/hook', function (req, res) {
84 console.log('[request source] ', eventObj.source); 89 console.log('[request source] ', eventObj.source);
85 console.log('[request message]', eventObj.message); 90 console.log('[request message]', eventObj.message);
86 91
87 - sendText(eventObj.replyToken, talk.start(id, pw)) // Test only 92 + if (first == false && eventObj.message.text == "처음") {
88 - //sendImage(eventObj.replyToken) 93 + request.post(
94 + {
95 + url: TARGET_URL,
96 + headers: {
97 + 'Authorization': `Bearer ${TOKEN}`
98 + },
99 + json: {
100 + "replyToken": eventObj.replyToken,
101 + "messages": [
102 + {
103 + "type": "text",
104 + "text": "카테고리를 선택해주세요.\n1. 식사\n2. 카페\n3. 술\n4. 놀거리\n(숫자만 입력해주세요)"
105 + }
106 + ]
107 + },
108 + }, (error, response, body) => {
109 + console.log(body)
110 + });
111 + first = true;
112 + }
113 +
114 + else if (first == true && second == false) {
89 115
116 + if (eventObj.message.text == 1) { //식사 선택
117 + request.post(
118 + {
119 + url: TARGET_URL, headers: { 'Authorization': `Bearer ${TOKEN}` }, json: {
120 + "replyToken": eventObj.replyToken,
121 + "messages": [{ "type": "text", "text": "[식사] 키워드를 선택해주세요\n1. 양식\n2. 한식\n3. 중식\n4. 일식\n5. 기타\n(숫자만 입력해주세요)" }]
122 + }
123 + }, (error, response, body) => {
124 + console.log(body)
125 + });
126 + destCar = "meal";
127 + } else if (eventObj.message.text == 2) { //카페 선택
128 + request.post(
129 + {
130 + url: TARGET_URL, headers: { 'Authorization': `Bearer ${TOKEN}` }, json: {
131 + "replyToken": eventObj.replyToken,
132 + "messages": [{ "type": "text", "text": "[카페] 키워드를 선택해주세요\n1. 감성\n2. 카공\n3. 디저트\n(숫자만 입력해주세요)" }]
133 + }
134 + }, (error, response, body) => {
135 + console.log(body)
136 + });
137 + destCar = "cafe";
138 + } else if (eventObj.message.text == 3) { //술 선택
139 + request.post(
140 + {
141 + url: TARGET_URL, headers: { 'Authorization': `Bearer ${TOKEN}` }, json: {
142 + "replyToken": eventObj.replyToken,
143 + "messages": [{ "type": "text", "text": "[술] 키워드를 선택해주세요\n1. 소주\n2. 이자카야\n3. 막걸리\n4. 맥주\n(숫자만 입력해주세요)" }]
144 + }
145 + }, (error, response, body) => {
146 + console.log(body)
147 + });
148 + destCar = "bar"
149 + } else if (eventObj.message.text == 4) { //놀거리 선택
150 + request.post(
151 + {
152 + url: TARGET_URL, headers: { 'Authorization': `Bearer ${TOKEN}` }, json: {
153 + "replyToken": eventObj.replyToken,
154 + "messages": [{ "type": "text", "text": "[놀거리] 키워드를 선택해주세요\n1. 노래방\n2. 피시방\n3. 기타\n(숫자만 입력해주세요)" }]
155 + }
156 + }, (error, response, body) => {
157 + console.log(body)
158 + });
159 + destCar = "play"
160 + }
161 + chooseFile();
162 + second = true;
163 +
164 + }
165 +
166 + else if (first == true && second == true) {
167 + if (destCar == "meal") {
168 + if (eventObj.message.text == 1) { var randpick = randomNum(0, 6); destination = results[randpick] }
169 + else if (eventObj.message.text == 2) { var randpick = randomNum(7, 19); destination = results[randpick] }
170 + else if (eventObj.message.text == 3) { var randpick = randomNum(20, 25); destination = results[randpick] }
171 + else if (eventObj.message.text == 4) { var randpick = randomNum(26, 36); destination = results[randpick] }
172 + else if (eventObj.message.text == 5) { var randpick = randomNum(37, 46); destination = results[randpick] }
173 + }
174 + else if (destCar == "cafe") {
175 + if (eventObj.message.text == 1) { var randpick = randomNum(0, 5); destination = results[randpick] }
176 + else if (eventObj.message.text == 2) { var randpick = randomNum(6, 12); destination = results[randpick] }
177 + else if (eventObj.message.text == 3) { var randpick = randomNum(13, 17); destination = results[randpick] }
178 + }
179 + else if (destCar == "bar") {
180 + if (eventObj.message.text == 1) { var randpick = randomNum(0, 5); destination = results[randpick] }
181 + else if (eventObj.message.text == 2) { var randpick = randomNum(6, 11); destination = results[randpick] }
182 + else if (eventObj.message.text == 3) { var randpick = randomNum(12, 15); destination = results[randpick] }
183 + else if (eventObj.message.text == 4) { var randpick = randomNum(16, 20); destination = results[randpick] }
184 + }
185 + else if (destCar == "play") {
186 + if (eventObj.message.text == 1) { var randpick = randomNum(0, 3); destination = results[randpick] }
187 + else if (eventObj.message.text == 2) { var randpick = randomNum(4, 8); destination = results[randpick] }
188 + else if (eventObj.message.text == 3) { var randpick = randomNum(9, 13); destination = results[randpick] }
189 + }
190 +
191 +
192 + }
90 res.sendStatus(200); 193 res.sendStatus(200);
91 }); 194 });
92 195
93 try { 196 try {
94 - const option = { 197 + // const option = {
95 - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'), 198 + // ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'),
96 - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(), 199 + // key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/privkey.pem'), 'utf8').toString(),
97 - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(), 200 + // cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain + '/cert.pem'), 'utf8').toString(),
98 - }; 201 + // };
99 202
100 - HTTPS.createServer(option, app).listen(sslport, () => { 203 + HTTPS.createServer(app).listen(sslport, () => {
101 console.log(`[HTTPS] Server is started on port ${sslport}`); 204 console.log(`[HTTPS] Server is started on port ${sslport}`);
102 }); 205 });
103 } catch (error) { 206 } catch (error) {
...@@ -105,3 +208,61 @@ try { ...@@ -105,3 +208,61 @@ try {
105 console.log(error); 208 console.log(error);
106 } 209 }
107 210
211 +
212 +const csv = require('csv-parser')
213 +const results = [];
214 +
215 +chooseFile = function () {
216 + if (destCar == "cafe") {
217 + fs.createReadStream('cafe_list.csv')
218 + .pipe(csv())
219 + .on('data', (data) => results.push(data))
220 + .on('end', () => {
221 + console.log(results);
222 + });
223 + }
224 + else if (destCar == "meal") {
225 + fs.createReadStream('meal_list.csv')
226 + .pipe(csv())
227 + .on('data', (data) => results.push(data))
228 + .on('end', () => {
229 + console.log(results);
230 + });
231 + }
232 + else if (destCar == "play") {
233 + fs.createReadStream('play_list.csv')
234 + .pipe(csv())
235 + .on('data', (data) => results.push(data))
236 + .on('end', () => {
237 + console.log(results);
238 + });
239 + }
240 + else if (destCar == "bar") {
241 + fs.createReadStream('bar_list.csv')
242 + .pipe(csv())
243 + .on('data', (data) => results.push(data))
244 + .on('end', () => {
245 + console.log(results);
246 + });
247 + }
248 +}
249 +
250 +function randomNum(min, max) {
251 + var randNum = Math.floor(Math.random() * (max - min + 1)) + min;
252 + return randNum;
253 +}
254 +
255 +function getX() {
256 + return destination.x;
257 +}
258 +function getY() {
259 + return destination.y;
260 +}
261 +function getName() {
262 + return destination.name;
263 +}
264 +function getAddress() {
265 + return destination.address;
266 +}
267 +
268 +//export { getX, getY, getName, getAddress }
...\ No newline at end of file ...\ No newline at end of file
......