신일섭

js구조 세분화

1 -var express = require('express'); 1 +var express = require('express');
2 -
3 const request = require('request'); 2 const request = require('request');
4 3
5 //--------------- Line messanger modules ------------------- 4 //--------------- Line messanger modules -------------------
6 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 5 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
7 const TARGET_URL_2 = 'https://api.line.me/v2/bot/message/push' 6 const TARGET_URL_2 = 'https://api.line.me/v2/bot/message/push'
8 var USER_ID = ' '; 7 var USER_ID = ' ';
9 -const TOKEN = 'LFi+FvOOFOydz0EKN22WRnQyiilnEiyxe2HVV1WrI54Jnght6+Gga07m7pxqSQltBJG2AV2lYnYOAk4TqxzZFKW9R36CJcQs+NaNEiNKJtgBfT/t3tEta/J6GtXxGz/DqdHnfZqSSfLFpXzun/ITTwdB04t89/1O/w1cDnyilFU=' 8 +const token = require('./config/key');
9 +
10 +const TOKEN = token.token; // 사용자 토큰 값
10 //---------------------------------------------------------- 11 //----------------------------------------------------------
11 12
12 //--------------- Translating moudules ---------------------- 13 //--------------- Translating moudules ----------------------
...@@ -22,19 +23,13 @@ var puppeteer = require('puppeteer'); ...@@ -22,19 +23,13 @@ var puppeteer = require('puppeteer');
22 var gmarket = require('./crawling/gmarket'); 23 var gmarket = require('./crawling/gmarket');
23 var coupang = require('./crawling/coupang'); 24 var coupang = require('./crawling/coupang');
24 var auction = require('./crawling/auction'); 25 var auction = require('./crawling/auction');
26 +var route = require('./route/route');
25 var shopping = 0; // in shopping menu identifier 0 = idle, 1 = shop selecting, 2 = select complete, 3 = shop method selecting 27 var shopping = 0; // in shopping menu identifier 0 = idle, 1 = shop selecting, 2 = select complete, 3 = shop method selecting
26 var shop_select = undefined // shopping-site identifier 28 var shop_select = undefined // shopping-site identifier
27 var method_action = 0; // typing count; 29 var method_action = 0; // typing count;
28 var user_info = new Array(); // 유저 정보 저장용 30 var user_info = new Array(); // 유저 정보 저장용
29 //------------------------------------------ 31 //------------------------------------------
30 32
31 -//----------------low db-----------------------------------
32 -const low = require('lowdb');
33 -const FileSync = require('lowdb/adapters/FileSync')
34 -const adapter = new FileSync('db.json');
35 -const db = low(adapter);
36 -const shortid = require('shortid');
37 -//---------------------------------------------------------
38 33
39 const fs = require('fs'); 34 const fs = require('fs');
40 const path = require('path'); 35 const path = require('path');
...@@ -67,17 +62,11 @@ app.post('/hook', async function (req, res) { ...@@ -67,17 +62,11 @@ app.post('/hook', async function (req, res) {
67 console.log('[request message]', eventObj.message); 62 console.log('[request message]', eventObj.message);
68 //---------------------------------------------------------------------------- 63 //----------------------------------------------------------------------------
69 64
70 - //--------------- Cancel ---------------------------------------------- 65 + // 취소판정
71 - if(message.text.includes('/취소')){ 66 + route.cancel(message.text, eventObj.replyToken);
72 - replying(eventObj.replyToken, '진행중인 기능 및 메뉴를 종료합니다.'); 67 + // 쇼핑몰 선택
73 - shopping = 0; 68 + route.shoppingroute(message.text, eventObj.source.userId, eventObj.replyToken);
74 - shop_select = undefined 69 +
75 - trans_counter = 0;
76 - method_action = 0;
77 - user_info.length = 0;
78 - }
79 - //---------------------------------------------------------------------
80 -
81 //----------------------- Translation which includes language-selector ----------------------------------------------- 70 //----------------------- Translation which includes language-selector -----------------------------------------------
82 if(eventObj.message.text.includes('/번역')){ 71 if(eventObj.message.text.includes('/번역')){
83 console.log('번역캐치: ', eventObj.message.text); 72 console.log('번역캐치: ', eventObj.message.text);
...@@ -88,59 +77,7 @@ app.post('/hook', async function (req, res) { ...@@ -88,59 +77,7 @@ app.post('/hook', async function (req, res) {
88 } 77 }
89 //-------------------------------------------------------------------------------------------------------------------- 78 //--------------------------------------------------------------------------------------------------------------------
90 79
91 - //---------------------- linking Shoppingcart crawler ---------------------------------------------------------------- 80 +
92 - if(message.text == '/쇼핑' ){
93 - replying(eventObj.replyToken, '쇼핑을 선택하셨습니다.\n원하시는 쇼핑몰 사이트를 선택하세요.\n현재 제공되는 사이트는 \n\n/g마켓\n/옥션\n/쿠팡\n\n입니다.\n/취소 로 해당 메뉴에서 퇴장이 가능합니다.');
94 - shopping = 1; // 다음 메세지에 선택하지 못할 경우 0으로 되돌릴 필요가있음
95 - }
96 -
97 - if(shopping == 1){
98 - if(message.text == '/g마켓'){
99 - replying(eventObj.replyToken, 'g마켓을 선택하셨습니다.\n원하시는 항목을 선택하세요.\n\n/장바구니동기화\n/장바구니조회\n/장바구니추가\n/장바구니삭제');
100 - shopping = 2; shop_select = 'g마켓';
101 - }else if(message.text == '/옥션'){
102 -
103 - }else if(message.text == '쿠팡'){
104 -
105 - }
106 - }
107 -
108 - if(shopping == 2 || shopping == 3){
109 - if(shop_select=='g마켓'){
110 - if(shopping == 2 && message.text == '/장바구니동기화')
111 - {
112 - replying(eventObj.replyToken, '====장바구니동기화====\n\n장바구니를 동기화 하기 위해 쇼핑몰에 연결합니다.\n\n아이디와 패스워드를 입력해 주세요:');
113 - method_action = 2;
114 - shopping = 3;
115 - }
116 - else if(shopping == 3 && method_action != 0)
117 - {
118 - replying(eventObj.replyToken, '===아이디/패스워드 입력중===');
119 - user_info[method_action] = message.text; // 아이디 패스워드를 user_info[2], user_info[1]에 저장
120 - method_action--;
121 - if(method_action==0){
122 - //장바구니 크롤링하는 함수
123 - USER_ID = eventObj.source.userId; // 상대방 ID 획득
124 - await gmarket.gmarket_c(USER_ID, user_info[2], user_info[1]);
125 - // replying(eventObj.replyToken, '리스트를 출력중입니다... 출력전까지 기다려주세요\n\n\n/장바구니동기화\n장바구니조회\n/장바구니추가\n/장바구니삭제');
126 - shopping = 2;
127 - }
128 - }
129 - if(message.text == '/장바구니조회')
130 - {
131 -
132 - }
133 - if(message.text == '/장바구니추가')
134 - {
135 -
136 - }
137 - if(message.text == '/장바구니삭제')
138 - {
139 -
140 - }
141 - }
142 - }
143 - //--------------------------------------------------------------------------------------------------------------------
144 81
145 82
146 res.sendStatus(200); 83 res.sendStatus(200);
......
1 +// Line Massenger Token Variables
2 +// 원작자 이외에 이용시 키 값을 바꿔줄것
3 +const token = 'LFi+FvOOFOydz0EKN22WRnQyiilnEiyxe2HVV1WrI54Jnght6+Gga07m7pxqSQltBJG2AV2lYnYOAk4TqxzZFKW9R36CJcQs+NaNEiNKJtgBfT/t3tEta/J6GtXxGz/DqdHnfZqSSfLFpXzun/ITTwdB04t89/1O/w1cDnyilFU='
4 +
5 +module.exports.token = token;
...\ No newline at end of file ...\ No newline at end of file
...@@ -11,7 +11,8 @@ const bodyParser = require('body-parser'); ...@@ -11,7 +11,8 @@ const bodyParser = require('body-parser');
11 const TARGET_URL = 'https://api.line.me/v2/bot/message/reply' 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' 12 const TARGET_URL_2 = 'https://api.line.me/v2/bot/message/push'
13 var USER_ID = ' '; 13 var USER_ID = ' ';
14 -const TOKEN = 'LFi+FvOOFOydz0EKN22WRnQyiilnEiyxe2HVV1WrI54Jnght6+Gga07m7pxqSQltBJG2AV2lYnYOAk4TqxzZFKW9R36CJcQs+NaNEiNKJtgBfT/t3tEta/J6GtXxGz/DqdHnfZqSSfLFpXzun/ITTwdB04t89/1O/w1cDnyilFU=' 14 +const token = require('../config/key');
15 +const TOKEN = token.token; //사용자 토큰
15 //---------------------------------------------------------- 16 //----------------------------------------------------------
16 17
17 //----------------low db----------------------------------- 18 //----------------low db-----------------------------------
...@@ -21,8 +22,6 @@ const adapter = new FileSync('db.json'); ...@@ -21,8 +22,6 @@ const adapter = new FileSync('db.json');
21 const db = low(adapter); 22 const db = low(adapter);
22 //--------------------------------------------------------- 23 //---------------------------------------------------------
23 24
24 -//gmarket_c();
25 -
26 // Gmarket Cart Crawling 25 // Gmarket Cart Crawling
27 async function gmarket_c(user_id, g_id, g_pw){ 26 async function gmarket_c(user_id, g_id, g_pw){
28 27
...@@ -68,6 +67,7 @@ async function gmarket_c(user_id, g_id, g_pw){ ...@@ -68,6 +67,7 @@ async function gmarket_c(user_id, g_id, g_pw){
68 } 67 }
69 68
70 await browser.close(); 69 await browser.close();
70 +
71 71
72 } 72 }
73 73
......
1 +//----------------low db-----------------------------------
2 +const low = require('lowdb');
3 +const FileSync = require('lowdb/adapters/FileSync')
4 +const adapter = new FileSync('db.json');
5 +const db = low(adapter);
6 +//---------------------------------------------------------
...\ No newline at end of file ...\ No newline at end of file
1 +// 쇼핑사이트 분기점 제공용
2 +
3 +const request = require('request');
4 +const fs = require('fs');
5 +const path = require('path');
6 +const HTTPS = require('https');
7 +
8 +//--------------- Line messanger modules -------------------
9 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
10 +const TARGET_URL_2 = 'https://api.line.me/v2/bot/message/push'
11 +var USER_ID = ' '; // pushing 용 유저아이디
12 +const token = require('../config/key');
13 +const TOKEN = token.token; //사용자 토큰
14 +//----------------------------------------------------------
15 +
16 +
17 +// ---------- Shopping modules -------------
18 +var gmarket = require('../crawling/gmarket');
19 +var coupang = require('../crawling/coupang');
20 +var auction = require('../crawling/auction');
21 +var shopping = 0; // in shopping menu identifier 0 = idle, 1 = shop selecting, 2 = select complete, 3 = shop method selecting
22 +var shop_select = undefined // shopping-site identifier
23 +var method_action = 0; // typing count;
24 +var user_info = new Array(); // 유저 정보 저장용
25 +//------------------------------------------
26 +
27 +// ========================================= 쇼핑몰 연결 중계 =========================================
28 +async function shoppingroute(user_message, user_id, replyToken){
29 +
30 + USER_ID = user_id;
31 +
32 + if(user_message == '/쇼핑' ){
33 + replying(replyToken, '쇼핑을 선택하셨습니다.\n원하시는 쇼핑몰 사이트를 선택하세요.\n현재 제공되는 사이트는 \n\n/g마켓\n/옥션\n/쿠팡\n\n입니다.\n/취소 로 해당 메뉴에서 퇴장이 가능합니다.');
34 + shopping = 1; // 다음 메세지에 선택하지 못할 경우 0으로 되돌릴 필요가있음
35 + }
36 +
37 + if(shopping == 1){
38 + if(user_message == '/g마켓'){
39 + replying(replyToken, 'g마켓을 선택하셨습니다.\n원하시는 항목을 선택하세요.\n\n/장바구니동기화\n/장바구니조회\n/장바구니추가\n/장바구니삭제');
40 + shopping = 2; shop_select = 'g마켓';
41 + }else if(user_message == '/옥션'){
42 +
43 + }else if(user_message == '쿠팡'){
44 +
45 + }
46 + }
47 +
48 + if(shopping == 2 || shopping == 3){
49 + if(shop_select=='g마켓'){
50 + if(shopping == 2 && user_message == '/장바구니동기화')
51 + {
52 + replying(replyToken, '====장바구니동기화====\n\n장바구니를 동기화 하기 위해 쇼핑몰에 연결합니다.\n\n아이디와 패스워드를 입력해 주세요:');
53 + method_action = 2;
54 + shopping = 3;
55 + }
56 + else if(shopping == 3 && method_action != 0)
57 + {
58 + replying(replyToken, '===아이디/패스워드 입력중===');
59 + user_info[method_action] = user_message; // 아이디 패스워드를 user_info[2], user_info[1]에 저장
60 + method_action--;
61 + if(method_action==0){
62 + //장바구니 크롤링하는 함수
63 + USER_ID = user_id; // 상대방 ID 획득
64 + await gmarket.gmarket_c(USER_ID, user_info[2], user_info[1]);
65 + // replying(eventObj.replyToken, '리스트를 출력중입니다... 출력전까지 기다려주세요\n\n\n/장바구니동기화\n장바구니조회\n/장바구니추가\n/장바구니삭제');
66 + shopping = 2;
67 + }
68 + }
69 + if(user_message == '/장바구니조회')
70 + {
71 +
72 + }
73 + if(user_message == '/장바구니추가')
74 + {
75 +
76 + }
77 + if(user_message == '/장바구니삭제')
78 + {
79 +
80 + }
81 + }
82 + }
83 +
84 +
85 +
86 +}
87 +
88 +// ============================ /취소 입력시 =================================
89 +async function cancel(user_message,replyToken){
90 +
91 + if(user_message.includes('/취소')){
92 + replying(replyToken, '진행중인 기능 및 메뉴를 종료합니다.');
93 + shopping = 0;
94 + shop_select = undefined
95 + method_action = 0;
96 + user_info.length = 0;
97 + }
98 +}
99 +
100 +//================== Just replying specific messages needed ===================================
101 +async function replying(replyToken, sp_message){ // sp_message is message(string) that depends on the situation that user selects
102 + request.post(
103 + {
104 + url: TARGET_URL,
105 + headers: {
106 + 'Authorization': `Bearer ${TOKEN}`
107 + },
108 + json: {
109 + "replyToken":replyToken,
110 + "messages":[
111 + {
112 + "type":"text",
113 + "text":sp_message // replying message
114 + }
115 + ]
116 + }
117 + },(error, response, body) => {
118 + // console.log(body)
119 + });
120 +}
121 +
122 +//================== Just pushing specific messages needed ===================================
123 +async function pushing(sp_message){ //push function
124 + request.post(
125 + {
126 + url: TARGET_URL_2,
127 + headers: {
128 + 'Authorization': `Bearer ${TOKEN}`
129 + },
130 + json: {
131 + "to": `${USER_ID}`,
132 + "messages":[
133 + {
134 + "type": "text",
135 + "text": sp_message // replying message
136 + }
137 + ]
138 + }
139 + },(error, response, body) => {
140 + //console.log(body)
141 + });
142 + }
143 +
144 +module.exports.shoppingroute = shoppingroute;
145 +module.exports.cancel = cancel;