신일섭

크롤링 챗봇 연동중_1

...@@ -4,6 +4,8 @@ const request = require('request'); ...@@ -4,6 +4,8 @@ const request = require('request');
4 4
5 //--------------- Line messanger modules ------------------- 5 //--------------- Line messanger modules -------------------
6 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 6 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
7 +const TARGET_URL_2 = 'https://api.line.me/v2/bot/message/push'
8 +var USER_ID = ' ';
7 const TOKEN = 'LFi+FvOOFOydz0EKN22WRnQyiilnEiyxe2HVV1WrI54Jnght6+Gga07m7pxqSQltBJG2AV2lYnYOAk4TqxzZFKW9R36CJcQs+NaNEiNKJtgBfT/t3tEta/J6GtXxGz/DqdHnfZqSSfLFpXzun/ITTwdB04t89/1O/w1cDnyilFU=' 9 const TOKEN = 'LFi+FvOOFOydz0EKN22WRnQyiilnEiyxe2HVV1WrI54Jnght6+Gga07m7pxqSQltBJG2AV2lYnYOAk4TqxzZFKW9R36CJcQs+NaNEiNKJtgBfT/t3tEta/J6GtXxGz/DqdHnfZqSSfLFpXzun/ITTwdB04t89/1O/w1cDnyilFU='
8 //---------------------------------------------------------- 10 //----------------------------------------------------------
9 11
...@@ -20,8 +22,10 @@ var puppeteer = require('puppeteer'); ...@@ -20,8 +22,10 @@ var puppeteer = require('puppeteer');
20 var gmarket = require('./crawling/gmarket'); 22 var gmarket = require('./crawling/gmarket');
21 var coupang = require('./crawling/coupang'); 23 var coupang = require('./crawling/coupang');
22 var auction = require('./crawling/auction'); 24 var auction = require('./crawling/auction');
23 -var shopping = 0; // in shopping menu identifier 0 = idle, 1 = shop selecting, 2 = select complete 25 +var shopping = 0; // in shopping menu identifier 0 = idle, 1 = shop selecting, 2 = select complete, 3 = shop method selecting
24 var shop_select = undefined // shopping-site identifier 26 var shop_select = undefined // shopping-site identifier
27 +var method_action = 0; // typing count;
28 +var user_info = new Array(); // 유저 정보 저장용
25 //------------------------------------------ 29 //------------------------------------------
26 30
27 const fs = require('fs'); 31 const fs = require('fs');
...@@ -40,7 +44,7 @@ app.use(bodyParser.json()); ...@@ -40,7 +44,7 @@ app.use(bodyParser.json());
40 44
41 45
42 //============== Utility Selector ========================================= 46 //============== Utility Selector =========================================
43 -app.post('/hook', function (req, res) { 47 +app.post('/hook', async function (req, res) {
44 48
45 //-------- request values --------- 49 //-------- request values ---------
46 var eventObj = req.body.events[0]; 50 var eventObj = req.body.events[0];
...@@ -48,7 +52,7 @@ app.post('/hook', function (req, res) { ...@@ -48,7 +52,7 @@ app.post('/hook', function (req, res) {
48 var message = eventObj.message; 52 var message = eventObj.message;
49 //--------------------------------- 53 //---------------------------------
50 54
51 - //---------------- request log------------------------------------------------ 55 + //---------------- request log------------------------------------------------ // 개인정보 보호를 위해 완성후에는 삭제될 예정
52 console.log('======================', new Date() ,'======================'); 56 console.log('======================', new Date() ,'======================');
53 console.log('[request]', req.body); 57 console.log('[request]', req.body);
54 console.log('[request source] ', eventObj.source); 58 console.log('[request source] ', eventObj.source);
...@@ -61,6 +65,8 @@ app.post('/hook', function (req, res) { ...@@ -61,6 +65,8 @@ app.post('/hook', function (req, res) {
61 shopping = 0; 65 shopping = 0;
62 shop_select = undefined 66 shop_select = undefined
63 trans_counter = 0; 67 trans_counter = 0;
68 + method_action = 0;
69 + user_info.length = 0;
64 } 70 }
65 //--------------------------------------------------------------------- 71 //---------------------------------------------------------------------
66 72
...@@ -79,12 +85,51 @@ app.post('/hook', function (req, res) { ...@@ -79,12 +85,51 @@ app.post('/hook', function (req, res) {
79 replying(eventObj.replyToken, '쇼핑을 선택하셨습니다.\n원하시는 쇼핑몰 사이트를 선택하세요.\n현재 제공되는 사이트는 \n\n/g마켓\n/옥션\n/쿠팡\n\n입니다.\n/취소 로 해당 메뉴에서 퇴장이 가능합니다.'); 85 replying(eventObj.replyToken, '쇼핑을 선택하셨습니다.\n원하시는 쇼핑몰 사이트를 선택하세요.\n현재 제공되는 사이트는 \n\n/g마켓\n/옥션\n/쿠팡\n\n입니다.\n/취소 로 해당 메뉴에서 퇴장이 가능합니다.');
80 shopping = 1; // 다음 메세지에 선택하지 못할 경우 0으로 되돌릴 필요가있음 86 shopping = 1; // 다음 메세지에 선택하지 못할 경우 0으로 되돌릴 필요가있음
81 } 87 }
88 +
82 if(shopping == 1){ 89 if(shopping == 1){
83 - switch(message.text){ 90 + if(message.text == '/g마켓'){
84 - case '/g마켓': break; 91 + replying(eventObj.replyToken, 'g마켓을 선택하셨습니다.\n원하시는 항목을 선택하세요.\n\n/장바구니동기화\n/장바구니조회\n/장바구니추가\n/장바구니삭제');
85 - case '/옥션': break; 92 + shopping = 2; shop_select = 'g마켓';
86 - case '/쿠팡': break; 93 + }else if(message.text == '/옥션'){
87 - default: shopping == 0; 94 +
95 + }else if(message.text == '쿠팡'){
96 +
97 + }
98 + }
99 +
100 + if(shopping == 2 || shopping == 3){
101 + if(shop_select=='g마켓'){
102 + if(shopping == 2 && message.text == '/장바구니동기화')
103 + {
104 + replying(eventObj.replyToken, '====장바구니동기화====\n\n장바구니를 동기화 하기 위해 쇼핑몰에 연결합니다.\n\n아이디와 패스워드를 입력해 주세요:');
105 + method_action = 2;
106 + shopping = 3;
107 + }
108 + else if(shopping == 3 && method_action != 0)
109 + {
110 + replying(eventObj.replyToken, '===아이디/패스워드 입력중===');
111 + user_info[method_action] = message.text; // 아이디 패스워드를 user_info[2], user_info[1]에 저장
112 + method_action--;
113 + if(method_action==0){
114 + //장바구니 크롤링하는 함수
115 + USER_ID = eventObj.source.userId; // 상대방 ID 획득
116 + await gmarket.gmarket_c(USER_ID, user_info[2], user_info[1]);
117 + // replying(eventObj.replyToken, '리스트를 출력중입니다... 출력전까지 기다려주세요\n\n\n/장바구니동기화\n장바구니조회\n/장바구니추가\n/장바구니삭제');
118 + shopping = 2;
119 + }
120 + }
121 + if(message.text == '/장바구니조회')
122 + {
123 +
124 + }
125 + if(message.text == '/장바구니추가')
126 + {
127 +
128 + }
129 + if(message.text == '/장바구니삭제')
130 + {
131 +
132 + }
88 } 133 }
89 } 134 }
90 //-------------------------------------------------------------------------------------------------------------------- 135 //--------------------------------------------------------------------------------------------------------------------
......
1 const puppeteer = require('puppeteer'); 1 const puppeteer = require('puppeteer');
2 -const readline = require('readline'); 2 +const request = require('request');
3 +const fs = require('fs');
4 +const path = require('path');
5 +const HTTPS = require('https');
6 +const domain = "www.chatbotshin.tk"
7 +const sslport = 23023;
8 +const bodyParser = require('body-parser');
9 +
10 +//--------------- Line messanger modules -------------------
11 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
12 +const TARGET_URL_2 = 'https://api.line.me/v2/bot/message/push'
13 +var USER_ID = ' ';
14 +const TOKEN = 'LFi+FvOOFOydz0EKN22WRnQyiilnEiyxe2HVV1WrI54Jnght6+Gga07m7pxqSQltBJG2AV2lYnYOAk4TqxzZFKW9R36CJcQs+NaNEiNKJtgBfT/t3tEta/J6GtXxGz/DqdHnfZqSSfLFpXzun/ITTwdB04t89/1O/w1cDnyilFU='
15 +//----------------------------------------------------------
3 16
4 //gmarket_c(); 17 //gmarket_c();
5 18
6 // Gmarket Cart Crawling 19 // Gmarket Cart Crawling
7 -async function gmarket_c(){ 20 +async function gmarket_c(user_id, g_id, g_pw){
21 +
22 + USER_ID = user_id;
8 23
9 // launching headless browser 24 // launching headless browser
10 const browser = await puppeteer.launch(); 25 const browser = await puppeteer.launch();
11 // making a new page 26 // making a new page
12 const page = await browser.newPage(); 27 const page = await browser.newPage();
13 28
14 - //console input id & password 29 + console.log(g_id + " " + g_pw); //정상입력 확인용, 개인정보 보호를 위해 완성 후 삭제 예정
15 - var args = process.argv;
16 -
17 - var g_id = args[2];
18 - var g_pw = args[3];
19 -
20 - console.log(args[2] + " " + args[3]);
21 30
22 // Gmarket login page 31 // Gmarket login page
23 await page.goto('https://signinssl.gmarket.co.kr/login/login?url=https://www.gmarket.co.kr/'); 32 await page.goto('https://signinssl.gmarket.co.kr/login/login?url=https://www.gmarket.co.kr/');
...@@ -35,17 +44,20 @@ async function gmarket_c(){ ...@@ -35,17 +44,20 @@ async function gmarket_c(){
35 44
36 // goto cart page 45 // goto cart page
37 await page.goto('https://cart.gmarket.co.kr/ko/cart'); 46 await page.goto('https://cart.gmarket.co.kr/ko/cart');
38 - //await page.screenshot({ path: 'gmarket.png', fullPage:true }); 47 +
39 48
40 // container which will hold crawled data [{},{}...] 49 // container which will hold crawled data [{},{}...]
41 let data = []; 50 let data = [];
42 51
43 // crawling start! (using getOne and getAll function) 52 // crawling start! (using getOne and getAll function)
44 data = await getAll(page); 53 data = await getAll(page);
45 - //data.push(await getAll(page)); // data[0][n] 54 +
55 +
46 56
47 //logging the result 57 //logging the result
48 for(let index = 0; index < data.length; index++){ 58 for(let index = 0; index < data.length; index++){
59 +
60 + await replying(data[index].prd_img, data[index].prd_name, data[index].prd_price, data[index].prd_link);
49 console.log(data[index]); 61 console.log(data[index]);
50 } 62 }
51 63
...@@ -65,7 +77,6 @@ async function gmarket_c(){ ...@@ -65,7 +77,6 @@ async function gmarket_c(){
65 for (let index = 0; index < number; index++) { 77 for (let index = 0; index < number; index++) {
66 data.push(await getOne(page, index + 1)); 78 data.push(await getOne(page, index + 1));
67 // pushing to the array 79 // pushing to the array
68 -
69 } 80 }
70 81
71 return Promise.resolve(data); 82 return Promise.resolve(data);
...@@ -93,6 +104,86 @@ async function getOne(page, index) { ...@@ -93,6 +104,86 @@ async function getOne(page, index) {
93 return Promise.resolve(data); 104 return Promise.resolve(data);
94 } 105 }
95 106
107 +async function replying(img_link, msg1, msg2, msg3){ //push function
108 + request.post(
109 + {
110 + url: TARGET_URL_2,
111 + headers: {
112 + 'Authorization': `Bearer ${TOKEN}`
113 + },
114 + json: {
115 + "to": `${USER_ID}`,
116 + "messages":[
117 + {
118 + "type":"image",
119 + "originalContentUrl": img_link,
120 + "previewImageUrl": img_link
121 + },
122 + {
123 + "type": "text",
124 + "text": "상품명: " + msg1 // replying message
125 + },
126 + {
127 + "type": "text",
128 + "text": "가격: " + msg2 + " 원" // replying message
129 + },
130 + {
131 + "type": "text",
132 + "text": "링크: " + msg3 // replying message
133 + }
134 +
135 + ]
136 + }
137 + },(error, response, body) => {
138 + //console.log(body)
139 + });
140 +}
141 +
142 +function replying2(replyToken, sp_message){ // sp_message is message(string) that depends on the situation that user selects
143 + request.post(
144 + {
145 + url: TARGET_URL,
146 + headers: {
147 + 'Authorization': `Bearer ${TOKEN}`
148 + },
149 + json: {
150 + "replyToken":replyToken,
151 + "messages":[
152 + {
153 + "type":"text",
154 + "text":sp_message // replying message
155 + }
156 + ]
157 + }
158 + },(error, response, body) => {
159 + //console.log(body)
160 + });
161 +}
162 +
163 +/*function replying_img(replyToken, img_link){
164 + request.post(
165 + {
166 + url: TARGET_URL,
167 + headers: {
168 + 'Authorization': `Bearer ${TOKEN}`
169 + },
170 + json: {
171 + "replyToken":replyToken,
172 + "messages":[
173 + {
174 + "type":"image",
175 + "originalContentUrl": img_link,
176 + "previewImageUrl": img_link
177 + }
178 + ]
179 + }
180 + },(error, response, body) => {
181 + console.log(body)
182 + });
183 +}*/
184 +
96 module.exports.gmarket_c = gmarket_c; 185 module.exports.gmarket_c = gmarket_c;
97 module.exports.getOne = getOne; 186 module.exports.getOne = getOne;
98 module.exports.getAll = getAll; 187 module.exports.getAll = getAll;
188 +module.exports.replying = replying;
189 +//module.exports.replying_img = replying_img;
...\ No newline at end of file ...\ No newline at end of file
......