-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
222 lines (192 loc) · 7.31 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Copyright 2017 Unify Software and Solutions GmbH & Co.KG.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*jshint node:true */
/*global require, Promise */
'use strict';
// load configuration
var config = require('./config.json');
console.log(config);
// logger
var bunyan = require('bunyan');
// SDK logger
var sdkLogger = bunyan.createLogger({
name: 'sdk',
stream: process.stdout,
level: config.sdkLogLevel
});
// Application logger
var logger = bunyan.createLogger({
name: 'app',
stream: process.stdout,
level: 'debug'
});
// node utils
var util = require('util');
var htmlToText = require('html-to-text');
var googleTranslate = require('google-translate')(config.apiKey);
// Circuit SDK
logger.info('[APP]: get Circuit instance');
var Circuit = require('circuit-sdk');
logger.info('[APP]: Circuit set bunyan logger');
Circuit.setLogger(sdkLogger);
//*********************************************************************
//* XlatorBot
//*********************************************************************
var XlatorBot = function(){
var self = this;
var client = null;
var langMap = new Map(require('./lang.json'));
//*********************************************************************
//* logon
//*********************************************************************
this.logon = function logon(){
logger.info('[APP]: logon');
return new Promise( function (resolve, reject) {
logger.info('[APP]: createClient');
client = new Circuit.Client({
client_id: config.client_id,
client_secret: config.client_secret,
domain: config.domain,
autoRenewToken: true
});
self.addEventListeners(client); //register evt listeners
client.logon()
.then(function loggedOn(user) {
logger.info('[APP]: loggedOn', user);
return client.setPresence({state: Circuit.Enums.PresenceState.AVAILABLE});
})
.then(() => {
console.log('Presence updated');
resolve();
})
.catch(reject);
});
};
//*********************************************************************
//* addEventListeners
//*********************************************************************
this.addEventListeners = function addEventListeners(client){
logger.info('[APP]: addEventListeners');
//set event callbacks for this client
client.addEventListener('connectionStateChanged', function (evt) {
self.logEvent(evt);
});
client.addEventListener('registrationStateChanged', function (evt) {
self.logEvent(evt);
});
client.addEventListener('reconnectFailed', function (evt) {
self.logEvent(evt);
});
client.addEventListener('itemAdded', function (evt) {
self.logEvent(evt);
self.xlateItem(evt.item);
});
client.addEventListener('itemUpdated', function (evt) {
self.logEvent(evt);
self.xlateItem(evt.item);
});
};
//*********************************************************************
//* logEvent -- helper
//*********************************************************************
this.logEvent = function logEvent(evt){
logger.info('[APP]:', evt.type, 'event received');
logger.debug('[APP]:', util.inspect(evt, { showHidden: true, depth: null }));
};
//*********************************************************************
//* sentByMe -- helper
//*********************************************************************
this.sentByMe = function sentByMe (item){
return (client.loggedOnUser.userId === item.creatorId);
};
//*********************************************************************
//* getLanguage -- helper
//*********************************************************************
this.getLanguage = function getLanguage (text){
logger.info('[APP]: getLanguage');
var lang = 'en';
// check if the first word in text
// matches one of the languages
// in the language map loaded from lang.json
var pattern = /^\s*([\u00BF-\u1FFF\u2C00-\uD7FF\w]{2,})\s*(:|-|>|\.|,|;)*/;
var matches = text.match(pattern);
if (matches){
var match = matches[1].toLowerCase();
if (langMap.get(match)) {
lang = langMap.get(match);
text = text.replace(matches[0], '');
}
}
return {lang: lang, text: text};
};
//*********************************************************************
//* xlateText -- helper
//*********************************************************************
this.xlateText = function xlateText(text){
logger.info('[APP]: xlateText', text);
return new Promise( function (resolve, reject) {
var res = self.getLanguage(text);
googleTranslate.translate(res.text, res.lang, function (err, result) {
if (err){
reject(err);
}
logger.info('[APP]: xlated text', result);
resolve(result.translatedText);
});
});
};
//*********************************************************************
//* xlateItem
//*********************************************************************
this.xlateItem = function xlateItem(item) {
logger.info('[APP]: xlateItem');
if (item.type !== 'TEXT' || self.sentByMe(item)) {
logger.debug('[APP]: skip it is not text or I sent it');
return;
}
if (!item.text || !item.text.content) {
logger.info('[APP]: skip it does not have text');
return;
}
self.xlateText(htmlToText.fromString(item.text.content))
.then (function addXlatedItem(xlatedText){
logger.info('[APP]: addXlatedItem');
var comment = {
convId: item.convId,
parentId: (item.parentItemId) ? item.parentItemId : item.itemId,
content: xlatedText
};
return client.addTextItem(item.convId, comment);
})
.catch(function(e){
logger.error('[APP]:', e);
});
};
};
//*********************************************************************
//* run
//*********************************************************************
function run() {
var xlatorBot = new XlatorBot();
xlatorBot.logon()
.catch (function(e){
logger.error('[APP]:', e);
});
}
//*********************************************************************
//* main
//*********************************************************************
run();