전현진

Delete test3.js

Showing 1 changed file with 0 additions and 134 deletions
1 -var express = require('express');
2 -const request = require('request');
3 -const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
4 -const TOKEN = '6lwsXwectIjTZ1NWp6c1t5l8nXaOjWgfeYfTOWNRRTVftaXMb4eSMj2zeficMr4/FWtyPPBuUB120kxZp58bwDilssdU5Uhc4mWz4kCPfcgiEcVwzXUktbYRE3xKAJU1zdYAsZZvI+87KY8qZvaRngdB04t89/1O/w1cDnyilFU='
5 -const fs = require('fs');
6 -const path = require('path');
7 -const HTTPS = require('https');
8 -const domain = "2018102235.oss2021.tk"
9 -const sslport = 23023;
10 -
11 -const fetch = require('node-fetch');
12 -const bodyParser = require('body-parser');
13 -var app = express();
14 -app.use(bodyParser.json());
15 -
16 -app.post('/hook', function(req, res) {
17 - var eventObj = req.body.events[0];
18 - var source = eventObj.source;
19 - var message = eventObj.message;
20 - // request log
21 - console.log('======================', new Date() ,'======================');
22 - console.log('[request]', req.body);
23 - console.log('[request source] ', source);
24 - console.log('[request message]', message);
25 -
26 - fs.readFile('coin_name.txt', 'utf8', function(err,data){
27 - arr = data.split(",");
28 - // console.log(arr);
29 - var responseList = new Array();
30 - function print_coin(){
31 - var promise = new Promise(function(resolve,reject){
32 - for(j=0;j<arr.length/10;j++){
33 - for(i=j*10;i<(j+1)*10;i++){
34 - (function(i){
35 - setTimeout(function(){
36 - const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1';
37 - const options = {method: 'GET', headers: {Accept: 'application/json'}};
38 - var coinJson = new Object();
39 - fetch(url, options)
40 - .then(res => res.json())
41 - .then(json => {
42 - coinJson.coin = arr[i];
43 - coinJson.price = json[0].trade_price;
44 - responseList.push(coinJson);
45 - console.log(i)
46 - if(responseList.length>116){
47 - resolve(responseList);
48 - }
49 - })
50 - .catch(err => console.error('error:' + err));
51 - },i*110);
52 - })(i);
53 - }
54 - }
55 -
56 - });
57 - return promise;
58 - }
59 -
60 - request.post(
61 - {
62 - url: TARGET_URL,
63 - headers: {
64 - 'Authorization': `Bearer ${TOKEN}`
65 - },
66 - json: {
67 - "replyToken":eventObj.replyToken,
68 - "messages":[
69 - {
70 - "type":"text",
71 - "text":resList[0].coin
72 - }
73 - ]
74 - }
75 - },(error, response, body) => {
76 - console.log(body)
77 - });
78 - res.sendStatus(200);
79 - })
80 -})
81 -
82 -app.post('/hook', function (req, res) {
83 -
84 - var eventObj = req.body.events[0];
85 - var source = eventObj.source;
86 - var message = eventObj.message;
87 - // request log
88 - console.log('======================', new Date() ,'======================');
89 - console.log('[request]', req.body);
90 - console.log('[request source] ', eventObj.source);
91 - console.log('[request message]', eventObj.message);
92 -
93 -
94 - request.post(
95 - {
96 - url: TARGET_URL,
97 - headers: {
98 - 'Authorization': `Bearer ${TOKEN}`
99 - },
100 - json: {
101 - "replyToken":eventObj.replyToken,
102 - "messages":[
103 - {
104 - "type":"text",
105 - "text":"Hello, user"
106 - },
107 - {
108 -
109 - }
110 - ]
111 - }
112 - },(error, response, body) => {
113 - console.log(body)
114 - });
115 -
116 -
117 - res.sendStatus(200);
118 -});
119 -
120 -try {
121 - const option = {
122 - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
123 - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
124 - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
125 - };
126 -
127 - HTTPS.createServer(option, app).listen(sslport, () => {
128 - console.log(`[HTTPS] Server is started on port ${sslport}`);
129 - });
130 - } catch (error) {
131 - console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
132 - console.log(error);
133 - }
134 -