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 43 additions and 18 deletions
......@@ -31,13 +31,16 @@ 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) {
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,
......@@ -66,14 +69,24 @@ function translate(userId, message, chatId) {
// 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 {
source = detect_body.langCode;
target = source == 'ko' ? 'en' : 'ko';
if (source == userLang) {
// User wants to translate his language into another
// Leave target untouched, which is his desired destination language
// 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 {
// source != userLang
// User wants to translate another language into his
// Then destination should be his language
console.log('Translating to userLang');
target = userLang;
}
console.log(source + ' => ' + target);
// Papago translation options
......@@ -103,23 +116,23 @@ function translate(userId, message, chatId) {
// On failure
result.text = '[' + objBody.errorCode + '] ' + objBody.errorMessage;
}
// Send translated message
bot.sendMessage(chatId, result.text);
resolve(result.text);
});
});
}
// Language not detected
else {
result.text = '언어를 감지할 수 없습니다.';
bot.sendMessage(chatId, result.text);
reject(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);
});
}
});
......