kykint

Fix and improve translate()

Previous way translate() determined its source and target language was inflexible and inconvenient.
For example a user always had to change target language according to whether he wants to translate
into other language or his own.

Improve this by assuming that user wants to translate his own language into other language when
source language matches the user's locale, and the opposite if it doesn't.

Also make some other improvements to logic:
- translate() now doesn't send a message to user directly, and instead returns a Promise.
- Also it takes a full user info instead of userId as a parameter, allowing it to detect
  user's locale as well.
Showing 1 changed file with 103 additions and 78 deletions
......@@ -31,95 +31,108 @@ bot.onText(/\/echo (.+)/, (msg, match) => {
});
/**
* Translate given message and send it to the specified chatroom.
* Translate given message and return the result as a parameter of Promise.
*
* @param {*} userId Id of the user that wants to translate
* @param {*} user Info of user that sent the message (msg.from)
* @param {*} message Message to translate
* @param {*} chatId Id of the chatroom to send translated message to
*/
function translate(userId, message, chatId) {
// Language detection options
var lang_detect_options = {
url: languagedetect_api_url,
form: { 'query': message },
headers: {
'X-Naver-Client-Id': config.papago.client_id,
'X-Naver-Client-Secret': config.papago.client_secret
}
};
// Papago language detection
request.post(lang_detect_options, function (error, response, body) {
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
var detect_body = JSON.parse(response.body);
var source = '';
var target = '';
var result = { type: 'text', text: '' };
// Check if detection was successful
console.log('Language detected: ' + detect_body.langCode);
// Translate using papago
// Try to grab source lang and target lang from user's preference first.
// If preference is not found, source default to detected language, and
// target defaults to English for Korean source, or Korean for all other langs.
if (detect_body.langCode != 'unk') {
db.findPref(userId, "pref", (dbResult) => {
// Choosing source language not implemented as of now
if (dbResult != undefined) {
source = dbResult.source != undefined ? dbResult.source : detect_body.langCode;
target = dbResult.target != undefined ? dbResult.target : (source == 'ko' ? 'en' : 'ko');
} else {
function translate(user, message) {
return new Promise(function (resolve, reject) {
const userId = user.id;
const userLang = user.language_code;
// Language detection options
var lang_detect_options = {
url: languagedetect_api_url,
form: { 'query': message },
headers: {
'X-Naver-Client-Id': config.papago.client_id,
'X-Naver-Client-Secret': config.papago.client_secret
}
};
// Papago language detection
request.post(lang_detect_options, function (error, response, body) {
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
var detect_body = JSON.parse(response.body);
var source = '';
var target = '';
var result = { type: 'text', text: '' };
// Check if detection was successful
console.log('Language detected: ' + detect_body.langCode);
// Translate using papago
// Try to grab source lang and target lang from user's preference first.
// If preference is not found, source default to detected language, and
// target defaults to English for Korean source, or Korean for all other langs.
if (detect_body.langCode != 'unk') {
db.findPref(userId, "pref", (dbResult) => {
source = detect_body.langCode;
target = source == 'ko' ? 'en' : 'ko';
}
console.log(source + ' => ' + target);
// Papago translation options
var translate_options = {
url: translate_api_url,
form: {
'source': source, // Before translation
'target': target, // After translation
'text': message // Message to translate
},
headers: {
'X-Naver-Client-Id': config.papago.client_id,
'X-Naver-Client-Secret': config.papago.client_secret
}
};
// Send translatation request
request.post(translate_options, function (error, response, body) {
var objBody = JSON.parse(response.body);
if (!error && response.statusCode == 200) {
// On success
result.text = objBody.message.result.translatedText;
if (source == userLang) {
// User wants to translate his language into another
// Leave target untouched, which is his desired destination language
console.log('Before: ' + message);
console.log('After: ' + result.text);
// Check if user target language exists in db, if not default to English
console.log('Translating to target');
target = (dbResult != undefined && dbResult.target != undefined)
? dbResult.target : 'en';
} else {
// On failure
result.text = '[' + objBody.errorCode + '] ' + objBody.errorMessage;
// source != userLang
// User wants to translate another language into his
// Then destination should be his language
console.log('Translating to userLang');
target = userLang;
}
// Send translated message
bot.sendMessage(chatId, result.text);
console.log(source + ' => ' + target);
// Papago translation options
var translate_options = {
url: translate_api_url,
form: {
'source': source, // Before translation
'target': target, // After translation
'text': message // Message to translate
},
headers: {
'X-Naver-Client-Id': config.papago.client_id,
'X-Naver-Client-Secret': config.papago.client_secret
}
};
// Send translatation request
request.post(translate_options, function (error, response, body) {
var objBody = JSON.parse(response.body);
if (!error && response.statusCode == 200) {
// On success
result.text = objBody.message.result.translatedText;
console.log('Before: ' + message);
console.log('After: ' + result.text);
} else {
// On failure
result.text = '[' + objBody.errorCode + '] ' + objBody.errorMessage;
}
resolve(result.text);
});
});
});
}
// Language not detected
else {
result.text = '언어를 감지할 수 없습니다.';
reject(result.text);
}
}
// Language not detected
else {
result.text = '언어를 감지할 수 없습니다.';
bot.sendMessage(chatId, result.text);
}
}
});
});
}
// [Any normal message which is not a command (not starting with '/')]
bot.onText(/^(?!\/)((.|\n)+)/, (msg, match) => {
const userId = msg.from.id;
const user = msg.from;
const chatId = msg.chat.id;
const chatType = msg.chat.type;
const received_msg = match[1];
......@@ -130,7 +143,11 @@ bot.onText(/^(?!\/)((.|\n)+)/, (msg, match) => {
return;
}
translate(userId, received_msg, chatId);
translate(user, received_msg).then(function (result) {
bot.sendMessage(chatId, result);
}).catch(function (error) {
console.log(error);
});
});
// /t(ranslate) [Whatever]
......@@ -138,7 +155,7 @@ bot.onText(/^(?!\/)((.|\n)+)/, (msg, match) => {
// Also, if the given '/t' message is a reply to another message,
// translate the reply target message as well.
bot.onText(/^\/(t|translate)($| ((.|\n)+))/, (msg, match) => {
const userId = msg.from.id;
const user = msg.from;
const chatId = msg.chat.id;
const chatType = msg.chat.type;
const received_msg = match[3];
......@@ -150,12 +167,20 @@ bot.onText(/^\/(t|translate)($| ((.|\n)+))/, (msg, match) => {
// Translate the reply's target message
if (isReply) {
const replyMsg = msg.reply_to_message.text;
translate(userId, replyMsg, chatId);
translate(user, replyMsg).then(function (result) {
bot.sendMessage(chatId, result);
}).catch(function (error) {
console.log(error);
});
}
// Translate the message after '/t' if exists
if (msgExists) {
translate(userId, received_msg, chatId);
translate(user, received_msg).then(function (result) {
bot.sendMessage(chatId, result);
}).catch(function (error) {
console.log(error);
});
}
});
......