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) => { ...@@ -31,22 +31,17 @@ bot.onText(/\/echo (.+)/, (msg, match) => {
31 bot.sendMessage(chatId, resp); 31 bot.sendMessage(chatId, resp);
32 }); 32 });
33 33
34 -// [Any normal message which is not a command (not starting with '/')] 34 +/**
35 -bot.onText(/(?!\/)(.+)/, (msg, match) => { 35 + * Translate given message and send it to the specified chatroom.
36 - const chatId = msg.chat.id; 36 + *
37 - const chatType = msg.chat.type; 37 + * @param {*} message Message to translate
38 - const received_msg = match[1]; 38 + * @param {*} chatId Id of the chatroom to send translated message to
39 - 39 + */
40 - // Ignore if we are not on a private chat, 40 +function translate(message, chatId) {
41 - // since direct translation is to be used only on private chats.
42 - if (chatType != 'private') {
43 - return;
44 - }
45 -
46 // Language detection options 41 // Language detection options
47 var lang_detect_options = { 42 var lang_detect_options = {
48 url: languagedetect_api_url, 43 url: languagedetect_api_url,
49 - form: { 'query': received_msg }, 44 + form: { 'query': message },
50 headers: { 45 headers: {
51 'X-Naver-Client-Id': papago_client_id, 46 'X-Naver-Client-Id': papago_client_id,
52 'X-Naver-Client-Secret': papago_client_secret 47 'X-Naver-Client-Secret': papago_client_secret
...@@ -77,7 +72,7 @@ bot.onText(/(?!\/)(.+)/, (msg, match) => { ...@@ -77,7 +72,7 @@ bot.onText(/(?!\/)(.+)/, (msg, match) => {
77 form: { 72 form: {
78 'source': source, // Before translation 73 'source': source, // Before translation
79 'target': target, // After translation 74 'target': target, // After translation
80 - 'text': received_msg // Message to translate 75 + 'text': message // Message to translate
81 }, 76 },
82 headers: { 77 headers: {
83 'X-Naver-Client-Id': papago_client_id, 78 'X-Naver-Client-Id': papago_client_id,
...@@ -93,7 +88,7 @@ bot.onText(/(?!\/)(.+)/, (msg, match) => { ...@@ -93,7 +88,7 @@ bot.onText(/(?!\/)(.+)/, (msg, match) => {
93 result.text = objBody.message.result.translatedText; 88 result.text = objBody.message.result.translatedText;
94 89
95 // Send translated message 90 // Send translated message
96 - console.log('Before: ' + received_msg); 91 + console.log('Before: ' + message);
97 console.log('After: ' + result.text); 92 console.log('After: ' + result.text);
98 bot.sendMessage(chatId, result.text); 93 bot.sendMessage(chatId, result.text);
99 } 94 }
...@@ -106,4 +101,19 @@ bot.onText(/(?!\/)(.+)/, (msg, match) => { ...@@ -106,4 +101,19 @@ bot.onText(/(?!\/)(.+)/, (msg, match) => {
106 } 101 }
107 } 102 }
108 }); 103 });
104 +}
105 +
106 +// [Any normal message which is not a command (not starting with '/')]
107 +bot.onText(/(?!\/)(.+)/, (msg, match) => {
108 + const chatId = msg.chat.id;
109 + const chatType = msg.chat.type;
110 + const received_msg = match[1];
111 +
112 + // Ignore if we are not on a private chat,
113 + // since direct translation is to be used only on private chats.
114 + if (chatType != 'private') {
115 + return;
116 + }
117 +
118 + translate(received_msg, chatId);
109 }); 119 });
......