app.js
19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
var express = require('express');
const request = require('request');
const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
const TOKEN = 'MSngLCdbA6HR1dLGguzo4dyPW2qPpHXCbJEmysylnGm2yG+xA+FDr/dcxQeJD/T4jde9PLQr+gFM7HBP804o2cJM6p5ueJt3OWZZ1o5GcDZR2cgx0QTN0iZwHG7/2+5KsGaTtdDjl1XbsPZqO+G6iQdB04t89/1O/w1cDnyilFU='
const fs = require('fs');
const path = require('path');
const HTTPS = require('https');
const domain = "2015104206.oss-2021.tk"
const sslport = 23023;
const SERACH_URL = 'https://openapi.naver.com/v1/search/local.xml?query=%EC%A3%BC%EC%8B%9D&display=10&start=1&sort=random'
const SEARCH_ID = 'A9UHUOhOXDtai4Rszdjw'
const SEARCH_SECRET = 'ptmB9hKvCw'
const bodyParser = require('body-parser');
var app = express();
var stack = 0;
var foodArray = [
{
"meal": "morning",
"name": "라면",
"country": "한식",
"spicy": "yes",
"cold": "hot"
},
{
"meal": "morning",
"name": "열무김치",
"country": "한식",
"spicy": "yes",
"cold": "cold"
},
{
"meal": "morning",
"name": "미역국",
"country": "한식",
"spicy": "no",
"cold": "hot"
},
{
"meal": "morning",
"name": "동치미",
"country": "한식",
"spicy": "no",
"cold": "cold"
},
{
"meal": "morning",
"name" : "김치만두",
"country": "중식",
"spicy": "yes",
"cold": "hot"
},
{
"meal": "morning",
"name" : "냉짬뽕",
"country": "중식",
"spicy": "yes",
"cold": "cold"
},
{
"meal": "morning",
"name" : "물만두",
"country": "중식",
"spicy": "no",
"cold": "hot"
},
{
"meal": "morning",
"name" : "녹차",
"country": "중식",
"spicy": "no",
"cold": "cold"
},
{
"meal": "morning",
"name" : "카레",
"country": "일식",
"spicy": "yes",
"cold": "hot"
},
{
"meal": "morning",
"name" : "냉우동",
"country": "일식",
"spicy": "yes",
"cold": "cold"
},
{
"meal": "morning",
"name" : "된장국",
"country": "일식",
"spicy": "no",
"cold": "hot"
},
{
"meal": "morning",
"name" : "모찌",
"country": "일식",
"spicy": "no",
"cold": "cold"
},
{
"meal": "morning",
"name" : "파스타",
"country": "양식",
"spicy": "yes",
"cold": "hot"
},
{
"meal": "morning",
"name" : "냉파스타",
"country": "양식",
"spicy": "yes",
"cold": "cold"
},
{
"meal": "morning",
"name" : "스프",
"country": "양식",
"spicy": "no",
"cold": "hot"
},
{
"meal": "morning",
"name" : "빵",
"country": "양식",
"spicy": "no",
"cold": "cold"
},
{
"meal": "lunch",
"name" : "순두부",
"country": "한식",
"spicy": "yes",
"cold": "hot"
},
{
"meal": "lunch",
"name" : "비빔냉면",
"country": "한식",
"spicy": "yes",
"cold": "cold"
},
{
"meal": "lunch",
"name" : "된장찌개",
"country": "한식",
"spicy": "no",
"cold": "hot"
},
{
"meal": "lunch",
"name" : "물냉면",
"country": "한식",
"spicy": "no",
"cold": "cold"
},
{
"meal": "lunch",
"name" : "짬뽕",
"country": "중식",
"spicy": "yes",
"cold": "hot"
},
{
"meal": "lunch",
"name" : "중국냉면",
"country": "중식",
"spicy": "yes",
"cold": "cold"
},
{
"meal": "lunch",
"name" : "짜장면",
"country": "중식",
"spicy": "no",
"cold": "hot"
},
{
"meal": "lunch",
"name" : "기스면",
"country": "중식",
"spicy": "no",
"cold": "cold"
},
{
"meal": "lunch",
"name" : "매운라멘",
"country": "일식",
"spicy": "yes",
"cold": "hot"
},
{
"meal": "lunch",
"name" : "매운모밀",
"country": "일식",
"spicy": "yes",
"cold": "cold"
},
{
"meal": "lunch",
"name" : "돈까스",
"country": "일식",
"spicy": "no",
"cold": "hot"
},
{
"meal": "lunch",
"name" : "냉모밀",
"country": "일식",
"spicy": "no",
"cold": "cold"
},
{
"meal": "lunch",
"name" : "스튜",
"country": "양식",
"spicy": "yes",
"cold": "hot"
},
{
"meal": "lunch",
"name" : "샌드위치",
"country": "양식",
"spicy": "yes",
"cold": "cold"
},
{
"meal": "lunch",
"name" : "알리오올리오",
"country": "양식",
"spicy": "no",
"cold": "hot"
},
{
"meal": "lunch",
"name" : "오믈렛",
"country": "양식",
"spicy": "no",
"cold": "cold"
},
{
"meal": "dinner",
"name" : "닭갈비",
"country": "한식",
"spicy": "yes",
"cold": "hot"
},
{
"meal": "dinner",
"name" : "비빔밥",
"country": "한식",
"spicy": "yes",
"cold": "cold"
},
{
"meal": "dinner",
"name" : "불고기",
"country": "한식",
"spicy": "no",
"cold": "hot"
},
{
"meal": "dinner",
"name" : "곤드레밥",
"country": "한식",
"spicy": "no",
"cold": "cold"
},
{
"meal": "dinner",
"name" : "마라탕",
"country": "중식",
"spicy": "yes",
"cold": "hot"
},
{
"meal": "dinner",
"name" : "난자완스",
"country": "중식",
"spicy": "yes",
"cold": "cold"
},
{
"meal": "dinner",
"name" : "탕수육",
"country": "중식",
"spicy": "no",
"cold": "hot"
},
{
"meal": "dinner",
"name" : "꽃빵",
"country": "중식",
"spicy": "no",
"cold": "cold"
},
{
"meal": "dinner",
"name" : "라멘",
"country": "일식",
"spicy": "yes",
"cold": "hot"
},
{
"meal": "dinner",
"name" : "불초밥",
"country": "일식",
"spicy": "yes",
"cold": "cold"
},
{
"meal": "dinner",
"name" : "튀김",
"country": "일식",
"spicy": "no",
"cold": "hot"
},
{
"meal": "dinner",
"name" : "초밥",
"country": "일식",
"spicy": "no",
"cold": "cold"
},
{
"meal": "dinner",
"name" : "파에야",
"country": "양식",
"spicy": "yes",
"cold": "hot"
},
{
"meal": "dinner",
"name" : "해물 파스타",
"country": "양식",
"spicy": "yes",
"cold": "cold"
},
{
"meal": "dinner",
"name" : "치킨",
"country": "양식",
"spicy": "no",
"cold": "hot"
},
{
"meal": "dinner",
"name" : "샐러드",
"country": "양식",
"spicy": "no",
"cold": "cold"
}
]
var meal, country, cold, spicy, name;
var teststring;
app.use(bodyParser.json());
app.post('/hook', function (req, res) {
var eventObj = req.body.events[0];
var source = eventObj.source;
var message = eventObj.message;
/*
아침 뭐먹지?
-> 한식, 중식, 일식, 양식 어떤게 좋으세요?(한,중,일,양,아무거나)
-> 따뜻한게 좋으세요, 차가운게 좋으세요?(따뜻한거, 차가운거, 아무거나)
-> 매운게 좋으세요, 안매운게 좋으세요?(매운거, 안매운거, 아무거나)
-> 그렇다면...이걸 드셔보세요! (사진 첨부)
JSON 구성요소
name : 음식 이름
meal : 아침/점심/저녁
contury : 한식/일식/양식/중식
cold : hot/cold
spicy : yes/no
image : 해당 음식 이미지 넣을 방법 찾아주세요
네이버 검색 API
https://developers.naver.com/docs/search/local/
검색해서 이미지 띄워주는건 어떨까?
*/
// request log
console.log('======================', new Date() ,'======================');
console.log('[request]', req.body);
console.log('[request source] ', eventObj.source);
console.log('[request message]', eventObj.message);
if(eventObj.message.text.indexOf("아침") !== -1 ||eventObj.message.text.indexOf("점심") !== -1 ||eventObj.message.text.indexOf("저녁") !== -1 ) // "아침" 단어가 포함되어있으면
{
if(eventObj.message.text.indexOf("아침") !== -1) { meal = "morning"}
else if(eventObj.message.text.indexOf("점심") !== -1) { meal = "lunch"}
else if(eventObj.message.text.indexOf("저녁") !== -1) { meal = "dinner"}
stack = 1;
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":eventObj.replyToken,
"messages":[
{
"type":"text",
"text":"한식, 중식, 일식, 양식 중 어떤게 좋으세요?"
}
]
}
},(error, response, body) => {
console.log(body)
}
);
console.log('[request message]', stack);
console.log('[request message]', meal);
}
else if(stack == 1 && (eventObj.message.text.indexOf("한식") !== -1
|| eventObj.message.text.indexOf("중식") !== -1
|| eventObj.message.text.indexOf("일식") !== -1
|| eventObj.message.text.indexOf("양식") !== -1))
{
{
if(eventObj.message.text.indexOf("한식") !== -1) { country = "한식"}
else if(eventObj.message.text.indexOf("중식") !== -1) {country = "중식"}
else if(eventObj.message.text.indexOf("일식") !== -1) { country = "일식"}
else if(eventObj.message.text.indexOf("양식") !== -1) { country = "양식"}
stack = 2;
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":eventObj.replyToken,
"messages":[
{
"type":"text",
"text":"매운거, 담백한거 어떤게 좋으세요?"
}
]
}
},(error, response, body) => {
console.log(body)
}
);
}
console.log('[request message]', country);
}
else if(stack == 2 && (eventObj.message.text.indexOf("매운") !== -1
|| eventObj.message.text.indexOf("담백한") !== -1))
{
{
if(eventObj.message.text.indexOf("매운") !== -1) { spicy = "yes"}
else if(eventObj.message.text.indexOf("담백한") !== -1) {spicy = "no"}
stack = 3;
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":eventObj.replyToken,
"messages":[
{
"type":"text",
"text":"찬거, 뜨거운거 어떤게 나으세요?"
}
]
}
},(error, response, body) => {
console.log(body)
}
);
}
console.log('[request message]', spicy);
}
else if(stack == 3 && (eventObj.message.text.indexOf("찬") !== -1
|| eventObj.message.text.indexOf("뜨거운") !== -1))
{
{
if(eventObj.message.text.indexOf("찬") !== -1) { cold = "cold"}
else if(eventObj.message.text.indexOf("뜨거운") !== -1) {cold = "hot"}
stack = 4;
if(foodArray)
var parseObject = foodArray.filter(function(e){
return e.meal === meal && e.country === country && e.spicy === spicy && e.cold === cold;
});
console.log('[request message]', cold);
name = parseObject[0].name;
const option = {
query: name,
start: 1,
display: 3,
sort: 'sim',
filter: 'small'
}
request.get({
uri:'https://openapi.naver.com/v1/search/local',
qs :option,
headers:{
'X-Naver-Client-Id':SEARCH_ID,
'X-Naver-Client-Secret':SEARCH_SECRET
}
}, function(err,res,body){
const json = JSON.parse(body);
console.log(json);
console.log(json['items'][1]['link']);
tempstring = json['items'][1]['link'];
if(json['items'][1]['link'] != '')
{
console.log(tempstring);
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":eventObj.replyToken,
"messages":[
{
"type":"text",
"text":name // 결과 도출
},
{
"type":"text",
"text": "드셔보세요!" // 결과 도출
},
{
"type":"text",
"text": json['items'][1]['link'] // 링크 발송
},
{
"type":"text",
"text": "이런 음식점은 어떠세요?"
}
]
}
},(error, response, body) => {
console.log(body)
}
);
}
else
{
console.log(tempstring);
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":eventObj.replyToken,
"messages":[
{
"type":"text",
"text":name // 결과 도출
},
{
"type":"text",
"text": "드셔보세요!" // 결과 도출
},
{
"type":"text",
"text": json['items'][1]['title'] // 링크 발송
},
{
"type":"text",
"text": "이런 음식점은 어떠세요?"
},
{
"type":"text",
"text": "이 식당은 링크가 없어요!"
}
]
}
},(error, response, body) => {
console.log(body)
}
);
}
})
// arr은 해당 JSON값을 가진 객체를 출력함
console.log('[request message]', meal);
console.log('[request message]', country);
console.log('[request message]', cold);
console.log('[request message]', spicy);
console.log('[request message]', name);
}
}
else if(eventObj.message.text.indexOf("처음부터") !== -1) // 초기화 알고리즘
{
stack = 0;
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":eventObj.replyToken,
"messages":[
{
"type":"text",
"text":"초기화되었습니다. '아침, 점심, 저녁' 중 하나를 입력해주세요!"
}
]
}
},(error, response, body) => {
console.log(body)
}
);
}
else // 예외 파트
{
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":eventObj.replyToken,
"messages":[
{
"type":"text",
"text":"잘못 입력하셨습니다. 다시 입력해주세요!"
}
],
"messages":[
{
"type":"text",
"text":"처음부터 다시 하고 싶으시면, '처음부터'라고 입력해주세요!"
}
]
}
},(error, response, body) => {
console.log(body)
}
);
}
res.sendStatus(200);
});
try {
const option = {
ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
};
HTTPS.createServer(option, app).listen(sslport, () => {
console.log(`[HTTPS] Server is started on port ${sslport}`);
});
} catch (error) {
console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
console.log(error);
}