kykint

Separate translation code into a function

Showing 1 changed file with 25 additions and 15 deletions
......@@ -31,22 +31,17 @@ bot.onText(/\/echo (.+)/, (msg, match) => {
bot.sendMessage(chatId, resp);
});
// [Any normal message which is not a command (not starting with '/')]
bot.onText(/(?!\/)(.+)/, (msg, match) => {
const chatId = msg.chat.id;
const chatType = msg.chat.type;
const received_msg = match[1];
// Ignore if we are not on a private chat,
// since direct translation is to be used only on private chats.
if (chatType != 'private') {
return;
}
/**
* Translate given message and send it to the specified chatroom.
*
* @param {*} message Message to translate
* @param {*} chatId Id of the chatroom to send translated message to
*/
function translate(message, chatId) {
// Language detection options
var lang_detect_options = {
url: languagedetect_api_url,
form: { 'query': received_msg },
form: { 'query': message },
headers: {
'X-Naver-Client-Id': papago_client_id,
'X-Naver-Client-Secret': papago_client_secret
......@@ -77,7 +72,7 @@ bot.onText(/(?!\/)(.+)/, (msg, match) => {
form: {
'source': source, // Before translation
'target': target, // After translation
'text': received_msg // Message to translate
'text': message // Message to translate
},
headers: {
'X-Naver-Client-Id': papago_client_id,
......@@ -93,7 +88,7 @@ bot.onText(/(?!\/)(.+)/, (msg, match) => {
result.text = objBody.message.result.translatedText;
// Send translated message
console.log('Before: ' + received_msg);
console.log('Before: ' + message);
console.log('After: ' + result.text);
bot.sendMessage(chatId, result.text);
}
......@@ -106,4 +101,19 @@ bot.onText(/(?!\/)(.+)/, (msg, match) => {
}
}
});
}
// [Any normal message which is not a command (not starting with '/')]
bot.onText(/(?!\/)(.+)/, (msg, match) => {
const chatId = msg.chat.id;
const chatType = msg.chat.type;
const received_msg = match[1];
// Ignore if we are not on a private chat,
// since direct translation is to be used only on private chats.
if (chatType != 'private') {
return;
}
translate(received_msg, chatId);
});
......