Ubuntu

Adjust function

Showing 1 changed file with 45 additions and 17 deletions
...@@ -31,7 +31,7 @@ app.post('/hook', function (req, res) { ...@@ -31,7 +31,7 @@ app.post('/hook', function (req, res) {
31 return 31 return
32 } 32 }
33 var tempstockList = data.toString().split("\n"); // ex) stockList = [[012312 ~~],[123012 ~~],[192312 ~~],[192310 ~~],[...]] 33 var tempstockList = data.toString().split("\n"); // ex) stockList = [[012312 ~~],[123012 ~~],[192312 ~~],[192310 ~~],[...]]
34 - for (x in tempstockList){ 34 + for (i in tempstockList){
35 var stockList = tempstockList[i].toString().split(" "); // ex) stockList = [[012312,~~],[1230012,~~],[1923,~~],[192310,~~],[...]] 35 var stockList = tempstockList[i].toString().split(" "); // ex) stockList = [[012312,~~],[1230012,~~],[1923,~~],[192310,~~],[...]]
36 } 36 }
37 // for (i in stockList) { 37 // for (i in stockList) {
...@@ -46,10 +46,11 @@ app.post('/hook', function (req, res) { ...@@ -46,10 +46,11 @@ app.post('/hook', function (req, res) {
46 console.log('[request message]', eventObj.message); 46 console.log('[request message]', eventObj.message);
47 47
48 var s_code = get_stock_code(stockList,stock_code,eventObj.replyToken,eventObj.message.text); 48 var s_code = get_stock_code(stockList,stock_code,eventObj.replyToken,eventObj.message.text);
49 - var wanted_price = get_wanted_price(eventObj.replyToken,eventObj.message.text); 49 + //var wanted_price = get_wanted_price(eventObj.replyToken,eventObj.message.text);
50 50
51 cron.schedule('*/5 * * * *', () => { 51 cron.schedule('*/5 * * * *', () => {
52 compare(s_code, wanted_price); 52 compare(s_code, wanted_price);
53 + console.log('It works')
53 }); 54 });
54 55
55 56
...@@ -96,12 +97,11 @@ app.post('/hook', function (req, res) { ...@@ -96,12 +97,11 @@ app.post('/hook', function (req, res) {
96 }); 97 });
97 98
98 function get_stock_code(stockList,stock_code,replyToken,message){ 99 function get_stock_code(stockList,stock_code,replyToken,message){
99 - for (let i = 0; i<stockList.length; i++) { 100 + var temp = message.toString().split(" ");
100 - if (eventObj.message.text == stockList[i][1]){ // 입력한거 나오면 코드로 바꿔줌 101 + var wanted_stock_name = temp[0];
101 - stock_code = stockList[i][0] 102 + var wanted_stock_price = temp[1];
102 - return stock_code 103 +
103 - } 104 + if (wanted_stock_price < 0){
104 - else{ // stockList에 없으면 없다고 보냄
105 request.post( 105 request.post(
106 { 106 {
107 url: TARGET_URL, 107 url: TARGET_URL,
...@@ -109,24 +109,27 @@ function get_stock_code(stockList,stock_code,replyToken,message){ ...@@ -109,24 +109,27 @@ function get_stock_code(stockList,stock_code,replyToken,message){
109 'Authorization': `Bearer ${TOKEN}` 109 'Authorization': `Bearer ${TOKEN}`
110 }, 110 },
111 json: { 111 json: {
112 - "replyToken":eventObj.replyToken, 112 + "replyToken":replyToken,
113 "messages":[ 113 "messages":[
114 { 114 {
115 "type":"text", 115 "type":"text",
116 - "text":"Not in list" 116 + "text":"Price must be positive number"
117 }, 117 },
118 ] 118 ]
119 } 119 }
120 },(error, response, body) => { 120 },(error, response, body) => {
121 console.log(body) 121 console.log(body)
122 }); 122 });
123 - }
124 123
125 } 124 }
126 -}
127 125
128 -function get_wanted_price(replyToken,message){ 126 +
129 - if (message < 0){ 127 + for (let i = 0; i<stockList.length; i++) {
128 + if (wanted_stock_name == stockList[i][1]){ // 입력한거 나오면 코드로 바꿔줌
129 + stock_code = stockList[i][0]
130 + return stock_code
131 + }
132 + else{ // stockList에 없으면 없다고 보냄
130 request.post( 133 request.post(
131 { 134 {
132 url: TARGET_URL, 135 url: TARGET_URL,
...@@ -134,11 +137,11 @@ function get_wanted_price(replyToken,message){ ...@@ -134,11 +137,11 @@ function get_wanted_price(replyToken,message){
134 'Authorization': `Bearer ${TOKEN}` 137 'Authorization': `Bearer ${TOKEN}`
135 }, 138 },
136 json: { 139 json: {
137 - "replyToken":eventObj.replyToken, 140 + "replyToken":replyToken,
138 "messages":[ 141 "messages":[
139 { 142 {
140 "type":"text", 143 "type":"text",
141 - "text":"Price must be positive number" 144 + "text":"Not in list"
142 }, 145 },
143 ] 146 ]
144 } 147 }
...@@ -147,8 +150,33 @@ function get_wanted_price(replyToken,message){ ...@@ -147,8 +150,33 @@ function get_wanted_price(replyToken,message){
147 }); 150 });
148 } 151 }
149 152
153 + }
150 } 154 }
151 155
156 +// function get_wanted_price(replyToken,message){ // 원하는 가격 입력받음 (0 이상만)
157 +// if (message < 0){
158 +// request.post(
159 +// {
160 +// url: TARGET_URL,
161 +// headers: {
162 +// 'Authorization': `Bearer ${TOKEN}`
163 +// },
164 +// json: {
165 +// "replyToken":eventObj.replyToken,
166 +// "messages":[
167 +// {
168 +// "type":"text",
169 +// "text":"Price must be positive number"
170 +// },
171 +// ]
172 +// }
173 +// },(error, response, body) => {
174 +// console.log(body)
175 +// });
176 +// }
177 +
178 +// }
179 +
152 function compare(s_code, wanted_price){ 180 function compare(s_code, wanted_price){
153 const temp = axios.get(`https://finance.naver.com/item/main.nhn?code=${s_code}%22`).then( 181 const temp = axios.get(`https://finance.naver.com/item/main.nhn?code=${s_code}%22`).then(
154 res => { 182 res => {
...@@ -168,7 +196,7 @@ function compare(s_code, wanted_price){ ...@@ -168,7 +196,7 @@ function compare(s_code, wanted_price){
168 'Authorization': `Bearer ${TOKEN}` 196 'Authorization': `Bearer ${TOKEN}`
169 }, 197 },
170 json: { 198 json: {
171 - "replyToken":eventObj.replyToken, 199 + "replyToken":replyToken,
172 "messages":[ 200 "messages":[
173 { 201 {
174 "type":"text", 202 "type":"text",
......