Solhee

appjs_send_foodaddinfo_to_line

1 const express = require('express'); 1 const express = require('express');
2 -const path = require('path'); 2 +const request = require('request');
3 -const foodapi = require('./foodapi'); 3 +const convert = require('xml-js');
4 4
5 -//module - 변수 전달 5 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
6 -const {variable1, variable2} = require('./moduleEx'); 6 +const TOKEN = 'z5iy5sMU1W4xZAlwvn0/5x4U+4ZsqI0hKO1ZZNFxUGlNzGBjFg2D1u6/Ij5C/Sbkncx3hyYg7Nfz5JnMD8BG/9Z3TEEHPvy1A2XhkPKs04v0/n6TjH1A3e9X23zYdYmNSGyPn2hDGglgm2p3YmtLSwdB04t89/1O/w1cDnyilFU='
7 -//module - 함수 전달 7 +const FOODAPI_URL = 'http://apis.data.go.kr/1470000/FoodAdtvInfoService/getFoodAdtvInfoList'
8 -const md = require("./moduleEx2"); 8 +const FOODAPI_KEY = 'ofY2ppOq5kBqT5jYPaGsW%2BEy7OR5a1bf5Z9PHvqNKvwO5DSCaU2x2qCj%2FoXnuB1YVbMTlErkHWSMEsR5b7isrw%3D%3D';
9 +var queryParams = '?' + encodeURIComponent('ServiceKey') + '=' +key;
10 +//queryParams += '&' + encodeURIComponent('prdlst_cd') + '=' + encodeURIComponent('C0118010300000');
11 +queryParams += '&' + encodeURIComponent('pc_kor_nm') + '=' + encodeURIComponent('과.채음료');
12 +//queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('3');
13 +//queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('100');
14 +const fs = require('fs');
15 +const path = require('path');
16 +const HTTPS = require('https');
17 +const domain = "foodbot2020.ml"
18 +const sslport = 23023;
19 +const bodyParser = require('body-parser');
20 +var app = express();
21 +app.use(bodyParser.json());
22 +app.post('/hook', function (req, res) {
9 23
10 -const app = express(); 24 + var eventObj = req.body.events[0];
25 + var source = eventObj.source;
26 + var message = eventObj.message;
11 27
12 -app.set('port',process.env.PORT||3000); 28 + // request log
29 + console.log('======================', new Date() ,'======================');
30 + console.log('[request]', req.body);
31 + console.log('[request source] ', eventObj.source);
32 + console.log('[request message]', eventObj.message);
13 33
14 -app.get('/',(req,res)=>{ 34 + getfoodinfo(eventObj.replyToken, eventObj.message.text); // eventObj.message.text 로 pc_kor_nm 받기
15 - // node js가 응답 35 +
16 - //res.send("Food Adtv Info Service");
17 36
18 - // HTML이 응답 37 + res.sendStatus(200);
19 - res.sendFile(path.join(__dirname,'/main.html'));
20 }); 38 });
21 39
22 -app.listen(app.get('port'),()=>{
23 - console.log(app.get('port'),'번 포트에서 대기 중');
24 -});
...\ No newline at end of file ...\ No newline at end of file
40 +function getfoodinfo(replyToken, kor_name) {
41 +
42 + request.post(
43 + {
44 + url: FOODAPI_URL + queryParams,
45 + },(error, response, body) => {
46 + if(error){
47 + console.log('에러입니다.')
48 + }
49 + else{
50 + if(response.statusCode ==200)
51 + {
52 + var result =body;
53 + var xmltojson = convert.xml2json(result,{compact:true,spaces:4});
54 + //console.log(xmltojson);
55 + var resObj = eval("("+xmltojson+")");
56 + var resultItems = resObj.response.body.items;
57 + var responseMessage ='[ ' + resultItems.item[0].PC_KOR_NM._text + ' ]\n';
58 +
59 +
60 + for(var i=0 ; i < resultItems.item.length; i ++)
61 + {
62 + var addictive = resultItems.item[i].T_KOR_NM._text;
63 + var specVal = resultItems.item[i].SPEC_VAL_SUMUP._text;
64 + responseMessage += addictive + ' : ' + specVal + '\n';
65 + }
66 + console.log('[responese message]',responseMessage);
67 +
68 + request.post(
69 + {
70 + url: TARGET_URL,
71 + headers: {
72 + 'Authorization': `Bearer ${TOKEN}`
73 + },
74 + json: {
75 + "replyToken":replyToken,
76 + "messages":[
77 + {
78 + "type":"text",
79 + "text":responseMessage
80 + }
81 + ]
82 + }
83 + },(error, response, body) => {
84 + console.log(body)
85 + });
86 + }
87 + }
88 + });
89 +
90 +}
91 +
92 +try {
93 + const option = {
94 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
95 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
96 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
97 + };
98 +
99 + HTTPS.createServer(option, app).listen(sslport, () => {
100 + console.log(`[HTTPS] Server is started on port ${sslport}`);
101 + });
102 + } catch (error) {
103 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
104 + console.log(error);
105 + }
106 +
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -8,9 +8,9 @@ var url = 'http://apis.data.go.kr/1470000/FoodAdtvInfoService/getFoodAdtvInfoLis ...@@ -8,9 +8,9 @@ var url = 'http://apis.data.go.kr/1470000/FoodAdtvInfoService/getFoodAdtvInfoLis
8 var key ='ofY2ppOq5kBqT5jYPaGsW%2BEy7OR5a1bf5Z9PHvqNKvwO5DSCaU2x2qCj%2FoXnuB1YVbMTlErkHWSMEsR5b7isrw%3D%3D'; 8 var key ='ofY2ppOq5kBqT5jYPaGsW%2BEy7OR5a1bf5Z9PHvqNKvwO5DSCaU2x2qCj%2FoXnuB1YVbMTlErkHWSMEsR5b7isrw%3D%3D';
9 var queryParams = '?' + encodeURIComponent('ServiceKey') + '=' +key; 9 var queryParams = '?' + encodeURIComponent('ServiceKey') + '=' +key;
10 //queryParams += '&' + encodeURIComponent('prdlst_cd') + '=' + encodeURIComponent('C0118010300000'); 10 //queryParams += '&' + encodeURIComponent('prdlst_cd') + '=' + encodeURIComponent('C0118010300000');
11 -//queryParams += '&' + encodeURIComponent('pc_kor_nm') + '=' + encodeURIComponent('과.채음료'); 11 +queryParams += '&' + encodeURIComponent('pc_kor_nm') + '=' + encodeURIComponent('스위트초콜릿');
12 //queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('3'); 12 //queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('3');
13 -queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('100'); 13 +//queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('100');
14 14
15 global.foods = new Array(); 15 global.foods = new Array();
16 16
...@@ -23,32 +23,25 @@ request({ ...@@ -23,32 +23,25 @@ request({
23 console.log('에러입니다.') 23 console.log('에러입니다.')
24 } 24 }
25 else{ 25 else{
26 - if(response.statusCode ==200) 26 + if(response.statusCode ==200){
27 var result =body; 27 var result =body;
28 var xmltojson = convert.xml2json(result,{compact:true,spaces:4}); 28 var xmltojson = convert.xml2json(result,{compact:true,spaces:4});
29 //console.log(xmltojson); 29 //console.log(xmltojson);
30 var resObj = eval("("+xmltojson+")"); 30 var resObj = eval("("+xmltojson+")");
31 - 31 + var resultItems = resObj.response.body.items;
32 - for(var i=0;i<100;i++){ 32 + console.log(xmltojson);
33 - function fo(){ 33 + var responseMessage ='[ ' + resultItems.item[0].PC_KOR_NM._text + ' ]\n';
34 - foods.push(food); 34 +
35 - } 35 +
36 - function foo(){ 36 + for(var i=0 ; i < resultItems.item.length; i ++){
37 - var food =resObj.response.body.items.item[i].PC_KOR_NM._text; 37 + var addictive = resultItems.item[i].T_KOR_NM._text;
38 - fo(); 38 + var specVal = resultItems.item[i].SPEC_VAL_SUMUP._text;
39 - } 39 + responseMessage += addictive + ' : ' + specVal + '\n';
40 - var food = resObj.response.body.items.item[i].PC_KOR_NM._text;
41 - global.foods.push(food);
42 - //foods.food = resObj.response.body.items.item[i].PC_KOR_NM._text;
43 - //foods.push(resObj.response.body.items.item[i].PC_KOR_NM._text);
44 - //console.log(resObj.response.body.items.item[i].PC_KOR_NM._text);
45 } 40 }
46 - //console.log(xmltojson); 41 + console.log('[responese message]',responseMessage);
47 } 42 }
43 + }
48 //console.log('Status', response.statusCode); 44 //console.log('Status', response.statusCode);
49 //console.log('Headers', JSON.stringify(response.headers)); 45 //console.log('Headers', JSON.stringify(response.headers));
50 //console.log('Reponse received', body); 46 //console.log('Reponse received', body);
51 - console.log(foods[99]);
52 }); 47 });
53 -
54 -console.log(foods);
...\ No newline at end of file ...\ No newline at end of file
......