-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
161 lines (144 loc) · 4.95 KB
/
app.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict';
var express = require('express');
var app = express();
var path = require('path');
var SmoochCore = require('smooch-core');
var jwt = require('jsonwebtoken');
var bodyParser = require('body-parser');
var request = require('request');
var Q = require('q');
import { KEY_ID, SECRET, YANDEX_KEY, APP_MAKER_LANGUAGE } from 'src/config';
// Initializing Smooch
const smooch = new SmoochCore({
keyId: KEY_ID,
secret: SECRET,
scope: 'app'
});
const getAppUserLanguage = text => {
var deferred = Q.defer();
const req = `https://translate.yandex.net/api/v1.5/tr.json/detect?key=${YANDEX_KEY}&text=${text}`;
request.get(req, (err, response) => {
if (err) {
deferred.reject(err);
}
deferred.resolve(response.body);
});
return deferred.promise;
};
const getTranslatedText = (text, fromLanguage, toLanguage) => {
var deferred = Q.defer();
const req = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${YANDEX_KEY}&text=${text}&lang=${fromLanguage}-${toLanguage}`;
request.get(req, (err, response) => {
if (err) {
deferred.reject(err);
}
deferred.resolve(JSON.parse(response.body));
});
return deferred.promise;
};
// Express Middleware for serving static files
app.use(express.static(path.join(__dirname, '/public')));
// Express Middleware to handle webhooks
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', function(req, res) {
res.redirect('index.html');
});
app.post('/fromAppUser', function(req, res) {
const payload = req.body;
const message = payload.messages[0];
const text = message.text;
if (text.indexOf('Translation') > -1) {
res.status(200);
res.send();
} else {
const appUserId = payload.appUser._id;
console.log(`[APPUSER] Message from app user: ${text}`);
getAppUserLanguage(text)
.then((result) => {
const data = JSON.parse(result);
if (data && data.lang) {
// const resp = {
// language: data.lang,
// appUserId: appUserId
// };
smooch.appUsers.update(appUserId, {
properties: {
language: data.lang
}
}).then((response) => {
console.log('updating APP_USER language');
const resp = {
language: response.appUser.properties.language,
appUserId: response.appUser._id
};
return resp;
}).then((data) => {
const app_user_language = data.language;
const app_user_id = data.appUserId;
console.log(`[APPUSER] App User language: ${app_user_language}`);
getTranslatedText(text, app_user_language, APP_MAKER_LANGUAGE).then((translatedText) => {
const message = translatedText.text[0];
console.log(`[APPUSER] Translated app user message: ${message}`);
return message;
})
.then((translatedMessage) => {
console.log(`[APPUSER] App User id: ${app_user_id}`);
if (translatedMessage) {
console.log(`[APPUSER] Posting translated message to app maker`);
smooch.conversations.sendMessage(app_user_id, {
text: `Translation (${app_user_language}-->${APP_MAKER_LANGUAGE}): ${translatedMessage}`,
role: 'appUser'
}).then(() => {
return null;
});
}
})
.end();
});;
}
throw new Error();
})
res.status(200);
res.send();
}
});
app.post('/fromAppMaker', function(req, res) {
const payload = req.body;
const message = payload.messages[0];
const text = message.text;
if (text.indexOf('Translation') > -1) {
res.status(200);
res.send();
} else {
console.log(`[APPMAKER] Message from app maker: ${text}`);
console.log(`[APPMAKER] AppMaker thinks that AppUser language is ${app_user_language}`);
const appUserId = payload.appUser._id;
smooch.appUsers.get(appUserId).then((response) => {
const appUserLanguage = response.appUser.properties.language;
getTranslatedText(text, APP_MAKER_LANGUAGE, appUserLanguage).then((translatedText) => {
const message = translatedText.text[0];
console.log(`[APPMAKER] Translated app maker message: ${message}`);
return message;
})
.then((translatedMessage) => {
if (translatedMessage) {
console.log(`[APPMAKER] Posting translated message to app user`);
smooch.conversations.sendMessage(appUserId, {
text: `Translation (${APP_MAKER_LANGUAGE}-->${app_user_language}): ${translatedMessage}`,
role: 'appMaker'
}).then(() => {
return null;
});
}
});
res.status(200);
res.send();
});
}
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});