전언석

Merge branch 'reply' into 'master'

Reply



See merge request !5
1 var express = require('express'); 1 var express = require('express');
2 const request = require('request'); 2 const request = require('request');
3 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 3 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
4 -const TOKEN = 'zVAczqoJ+L9oykqhEj7HoP7f6Nyb+R3T1TntHXQhYihI+KIxH4SZDGpKu0jKsMXMHKBVXpmbybA+oaV8u/dfLppKe3NHXU9AdBVypy9NgfWiFPLlcwm3GdkVPAZGoS4nvOCXWDKUb+ixPKWjlbnChAdB04t89/1O/w1cDnyilFU=' 4 +const TOKEN = ''
5 const fs = require('fs'); 5 const fs = require('fs');
6 const path = require('path'); 6 const path = require('path');
7 const HTTPS = require('https'); 7 const HTTPS = require('https');
...@@ -21,21 +21,12 @@ app.post('/hook', function (req, res) { ...@@ -21,21 +21,12 @@ app.post('/hook', function (req, res) {
21 21
22 // request log 22 // request log
23 console.log('======================', new Date(), '======================'); 23 console.log('======================', new Date(), '======================');
24 - console.log('[request]', req.body);
25 - console.log('[request source] ', eventObj.source);
26 - console.log('[request message]', eventObj.message);
27 -
28 - var incredients_list = message.text.split(" ");
29 -
30 - function findingredients(item) { return item === "김치"; }
31 -
32 // mwsql 24 // mwsql
33 - // mysql -u chatbot -p -h chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com
34 var mysql = require('mysql'); 25 var mysql = require('mysql');
35 var db = mysql.createConnection({ 26 var db = mysql.createConnection({
36 host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com', 27 host: 'chatbot.c7fzgftc3yrm.us-east-1.rds.amazonaws.com',
37 user: 'chatbot', 28 user: 'chatbot',
38 - password: '11111111', 29 + password: '',
39 database: 'chatbot', 30 database: 'chatbot',
40 port: '3306' 31 port: '3306'
41 }); 32 });
...@@ -46,32 +37,63 @@ app.post('/hook', function (req, res) { ...@@ -46,32 +37,63 @@ app.post('/hook', function (req, res) {
46 if (error) { 37 if (error) {
47 console.log(error); 38 console.log(error);
48 } 39 }
49 - for (var i = 0; i < results.length; i++) 40 + var arr = new Array();
50 - if (results[i].ingredients === message.text) { 41 + var input_ingredients_list = message.text.split(" ");
51 - console.log(`메뉴 : ${results[i].menu}, 레시피 : ${results[i].recipe}`); 42 + for (var i = 0; i < results.length; i++) {
52 - request.post( 43 + var ingredients_list = results[i].ingredients.split(", ");
53 - { 44 + if (ingredients_list.filter(x => input_ingredients_list.includes(x)).length === input_ingredients_list.length) {
54 - url: TARGET_URL, 45 + arr.push(i);
55 - headers: {
56 - 'Authorization': `Bearer ${TOKEN}`
57 - },
58 - json: {
59 - "replyToken": eventObj.replyToken,
60 - "messages": [
61 - {
62 - "type": "text",
63 - "text": `${results[i].menu}`
64 - },
65 - {
66 - "type": "text",
67 - "text": `${results[i].recipe}`
68 - }
69 - ]
70 - }
71 - }, (error, response, body) => {
72 - console.log(body)
73 - });
74 } 46 }
47 + }
48 + if (arr.length != 0) {
49 + var num = Math.floor(Math.random() * arr.length);
50 + request.post(
51 + {
52 + url: TARGET_URL,
53 + headers: {
54 + 'Authorization': `Bearer ${TOKEN}`
55 + },
56 + json: {
57 + "replyToken": eventObj.replyToken,
58 + "messages": [
59 + {
60 + "type": "text",
61 + "text": `메뉴는 "${results[num].menu}" 입니다.`
62 + },
63 + {
64 + "type": "text",
65 + "text": `필요한 재료는\n${results[num].ingredients} 입니다.`
66 + },
67 + {
68 + "type": "text",
69 + "text": `레시피\n${results[num].recipe}`
70 + }
71 + ]
72 + }
73 + }, (error, response, body) => {
74 + console.log(body)
75 + });
76 + }
77 + else {
78 + request.post(
79 + {
80 + url: TARGET_URL,
81 + headers: {
82 + 'Authorization': `Bearer ${TOKEN}`
83 + },
84 + json: {
85 + "replyToken": eventObj.replyToken,
86 + "messages": [
87 + {
88 + "type": "text",
89 + "text": `재료를 다시 입력해주세요.`
90 + }
91 + ]
92 + }
93 + }, (error, response, body) => {
94 + console.log(body)
95 + });
96 + }
75 }); 97 });
76 98
77 db.end(); 99 db.end();
......
1 +// var xhr = new XMLHttpRequest();
2 +// var url = 'http://apis.data.go.kr/1390802/AgriFood/FdCkry/getKoreanFoodFdCkryList'; /*URL*/
3 +// var queryParams = '?' + encodeURIComponent('serviceKey') + '='+'R7bFhjvvAMmxJxzcrL8NWkYHVa227zfpwvpwgXxcixNdMY0EbdbsbCboj3zXEsXniKNHyqu2dEllJCRk1LsdxA%3D%3D'; /*Service Key*/
4 +// queryParams += '&' + encodeURIComponent('service_Type') + '=' + encodeURIComponent('xml'); /**/
5 +// queryParams += '&' + encodeURIComponent('Page_No') + '=' + encodeURIComponent('1'); /**/
6 +// queryParams += '&' + encodeURIComponent('Page_Size') + '=' + encodeURIComponent('20'); /**/
7 +// queryParams += '&' + encodeURIComponent('food_Name') + '=' + encodeURIComponent('밥'); /**/
8 +// queryParams += '&' + encodeURIComponent('ckry_Name') + '=' + encodeURIComponent('조리'); /**/
9 +// xhr.open('GET', url + queryParams);
10 +// xhr.onreadystatechange = function () {
11 +// if (this.readyState == 4) {
12 +// alert('Status: '+this.status+'nHeaders: '+JSON.stringify(this.getAllResponseHeaders())+'nBody: '+this.responseText);
13 +// }
14 +// };
15 +
16 +// xhr.send('');
17 +
18 +var XMLHttpRequest = require('xhr2');
1 var xhr = new XMLHttpRequest(); 19 var xhr = new XMLHttpRequest();
2 var url = 'http://apis.data.go.kr/1390802/AgriFood/FdCkry/getKoreanFoodFdCkryList'; /*URL*/ 20 var url = 'http://apis.data.go.kr/1390802/AgriFood/FdCkry/getKoreanFoodFdCkryList'; /*URL*/
3 var queryParams = '?' + encodeURIComponent('serviceKey') + '='+'R7bFhjvvAMmxJxzcrL8NWkYHVa227zfpwvpwgXxcixNdMY0EbdbsbCboj3zXEsXniKNHyqu2dEllJCRk1LsdxA%3D%3D'; /*Service Key*/ 21 var queryParams = '?' + encodeURIComponent('serviceKey') + '='+'R7bFhjvvAMmxJxzcrL8NWkYHVa227zfpwvpwgXxcixNdMY0EbdbsbCboj3zXEsXniKNHyqu2dEllJCRk1LsdxA%3D%3D'; /*Service Key*/
...@@ -9,7 +27,11 @@ queryParams += '&' + encodeURIComponent('ckry_Name') + '=' + encodeURIComponent( ...@@ -9,7 +27,11 @@ queryParams += '&' + encodeURIComponent('ckry_Name') + '=' + encodeURIComponent(
9 xhr.open('GET', url + queryParams); 27 xhr.open('GET', url + queryParams);
10 xhr.onreadystatechange = function () { 28 xhr.onreadystatechange = function () {
11 if (this.readyState == 4) { 29 if (this.readyState == 4) {
12 - alert('Status: '+this.status+'nHeaders: '+JSON.stringify(this.getAllResponseHeaders())+'nBody: '+this.responseText); 30 + //console.log('Status: '+this.status+'nHeaders: '+JSON.stringify(this.getAllResponseHeaders())+'nBody: '+this.responseText);
31 + //console.log(this.responseText);
32 + console.log(this.responseText);
33 + console.log("hehe\n");
34 + console.log(queryParams);
13 } 35 }
14 }; 36 };
15 37
......
...@@ -10,7 +10,7 @@ import pymysql ...@@ -10,7 +10,7 @@ import pymysql
10 conn = pymysql.connect(host="localhost", user="root",password='abcde12345abcde',db='db_recipe', charset='utf8') 10 conn = pymysql.connect(host="localhost", user="root",password='abcde12345abcde',db='db_recipe', charset='utf8')
11 curs = conn.cursor(pymysql.cursors.DictCursor) 11 curs = conn.cursor(pymysql.cursors.DictCursor)
12 12
13 -for pagenum in range(1,35): #크롤링 할 상위 페이지 개수. 약 40개정도의 레시피 링크가 한 페이지에 이어져 있음 13 +for pagenum in range(1,2): #크롤링 할 상위 페이지 개수. 약 40개정도의 레시피 링크가 한 페이지에 이어져 있음
14 print(pagenum) 14 print(pagenum)
15 page = requests.get('https://www.10000recipe.com/recipe/list.html?order=reco&page='+str(pagenum)) 15 page = requests.get('https://www.10000recipe.com/recipe/list.html?order=reco&page='+str(pagenum))
16 16
......