-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
76 lines (69 loc) · 2.29 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const express = require('express');
const translate = require("deepl");
require('dotenv').config();
const deeplKey = String(process.env.DEEPL_KEY);
const chaplusKey = String(process.env.CHAPLUS_KEY);
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
let xhr = new XMLHttpRequest();
const app = express();
app.use(express.static(__dirname + "/dist/"));
/*
app.get('/api', function(req, res) {
var {PythonShell} = require('python-shell');
var pyshell = new PythonShell('sample.py');
//console.log("②./api: 「I get request from ./」")
pyshell.send(req.query.dat);
//console.log("③./api: 「I send a params of request to sample.py」")
pyshell.on('message', function (data) {
//console.log("⑤./api: 「I receive message from sample.py」")
res.send({
msg: data
})
//console.log("⑥./api: 「I return response to ./」")
})
})
*/
app.get("/api2",function(req,res){
var utterance=""
// en => ja
translate({
text: req.query.dat,
source_lan: "EN",
target_lang: 'JA',
auth_key: deeplKey,
free_api: true
})
.then(result => {
// AI
//console.log(result.data.translations[0].text)
var data = {"utterance":result.data.translations[0].text}
xhr.open('post', 'https://www.chaplus.jp/v1/chat?apikey='+chaplusKey);
xhr.setRequestHeader('Content-Type', 'text/json');
xhr.send(JSON.stringify(data));
xhr.onreadystatechange = function() {
if( xhr.readyState === 4 && xhr.status === 200 ) {
var aiResponse = JSON.parse(xhr.responseText)
// ja => en
translate({
text: aiResponse.bestResponse.utterance,
source_lan: "JP",
target_lang: 'EN',
auth_key: deeplKey ,
free_api: true
})
.then(result => {
//console.log(result.data.translations[0].text);
res.send({
msg: result.data.translations[0].text
})
})
.catch(error => {
console.error(error)
});
}
}
})
.catch(error => {
console.error(error)});
})
app.listen(process.env.PORT || 8080);