ZuseongZIN

Merge branch 'server' of http://khuhub.khu.ac.kr/2017103989/stock_chatbot into develop

1 -//app.js
2 -
3 var express = require('express'); 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 = 'OVIxKODBqM8Pn2dpFtFeSLsDbBvApfTu88rh8wFGOzfvgvPjmBH0A4XKii97VxIDO9shYyTix4qGq32vwvL895Rbss5VSVEiU/XG5lOdyTLgECkSQKOdObSetZwcVHbGmzZJ+0Cz5vZrB5KuImOwrwdB04t89/1O/w1cDnyilFU='
5 +const fs = require('fs');
6 +const path = require('path');
7 +const HTTPS = require('https');
8 +const domain = "2017103989.oss2021.tk"
9 +const sslport = 23023;
10 +
11 +const bodyParser = require('body-parser');
4 var app = express(); 12 var app = express();
13 +app.use(bodyParser.json());
14 +app.post('/hook', function (req, res) {
5 15
6 -app.use(express.urlencoded({ extended: false })); 16 + var eventObj = req.body.events[0];
7 -app.use(express.json()); 17 + var source = eventObj.source;
8 18
9 -app.get('/keyboard', (req, res) => { 19 + var message = eventObj.message;
10 - var data = {'type': 'text'} 20 + var pystring;
11 - res.json(data);
12 -});
13 21
14 -app.post('/message', (req, res) => { 22 + const spawn = require("child_process").spawn;
15 - var question = req.body.userRequest.utterance; 23 + const process = spawn("python", ["basic.py", eventObj.message.text]);
16 - var goMain = '처음으로'; 24 + const Callback = (data) => {
17 - 25 + console.log("Data :", data.toString());
18 - if (question === '테스트') { 26 + pystring = data.toString();
19 - var data = { 27 + // request log
20 - 'version': '2.0', 28 + console.log('======================', new Date() ,'======================');
21 - 'template': { 29 + console.log('[request]', req.body);
22 - 'outputs': [{ 30 + console.log('[request source] ', eventObj.source);
23 - 'simpleText': { 31 + console.log('[request message]', eventObj.message);
24 - 'text': '테스트' 32 +
25 - } 33 + request.post(
26 - }], 34 + {
27 - 'quickReplies': [{ 35 + url: TARGET_URL,
28 - 'label': goMain, 36 + headers: {
29 - 'action': 'message', 37 + 'Authorization': `Bearer ${TOKEN}`
30 - 'messageText': goMain 38 + },
31 - }] 39 + json: {
32 - } 40 + "replyToken":eventObj.replyToken,
41 + "messages":[
42 + {
43 + "type":"text",
44 + "text":pystring
45 + },
46 + {
47 + "type":"text",
48 + "text":"May I help you?"
33 } 49 }
50 + ]
34 } 51 }
35 - res.json(data); 52 + },(error, response, body) => {
53 + console.log(body)
54 + });
55 +
56 +
57 + res.sendStatus(200);
58 +
59 + };
60 + process.stdout.on("data", Callback);
61 +
62 +
63 +
36 }); 64 });
37 65
38 -app.listen(3000, () => console.log('node on 3000'));
...\ No newline at end of file ...\ No newline at end of file
66 +try {
67 + const option = {
68 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
69 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
70 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
71 + };
72 +
73 + HTTPS.createServer(option, app).listen(sslport, () => {
74 + console.log(`[HTTPS] Server is started on port ${sslport}`);
75 + });
76 + } catch (error) {
77 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
78 + console.log(error);
79 + }
80 +
......
1 +import FinanceDataReader as fdr
2 +import pandas as pd
3 +import sys
4 +
5 +
6 +def basicinform(input):
7 + stocks = pd.read_csv('stockcodename.csv', index_col=0)
8 + symbol = ''
9 + for i in enumerate(stocks.Name):
10 + if i[1] == input:
11 + symbol = (stocks.iloc[i[0]].Symbol)
12 + break
13 +
14 + df = fdr.DataReader(symbol)
15 + ror_df = df.Close.pct_change()
16 + volume = df.Volume.iloc[-1]
17 + price = df.Close.iloc[-1]
18 + ror = ror_df[-1]
19 +
20 + value = {
21 + "현재가": price,
22 + "거래랑": volume,
23 + "전일 대비 수익률:": ror
24 + }
25 + return value
26 +
27 +
28 +# print(basicinform('삼성전자'))
29 +
30 +args = sys.argv
31 +print(basicinform(args[1]))